From 603712c38a127e297eddd23975fb950499a0c10c Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 24 Jun 2009 21:28:51 +0000 Subject: New asynchronous PNG_Canvas interface. --- libdimensionxx/dimensionxx/png.hpp | 29 +++++++------ libdimensionxx/dimensionxx/progress.hpp | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 12 deletions(-) (limited to 'libdimensionxx/dimensionxx') diff --git a/libdimensionxx/dimensionxx/png.hpp b/libdimensionxx/dimensionxx/png.hpp index 07d7c18..c2cdbd5 100644 --- a/libdimensionxx/dimensionxx/png.hpp +++ b/libdimensionxx/dimensionxx/png.hpp @@ -25,6 +25,7 @@ #include #include +#include namespace Dimension { @@ -35,18 +36,15 @@ namespace Dimension { public: // Input PNG_Canvas; read the Canvas from istr now - explicit PNG_Canvas(std::istream& istr) - : Canvas(), m_istr(&istr), m_ostr(0), m_written(false) { read(); } + explicit PNG_Canvas(std::istream& istr); // Output PNG_Canvas; write the Canvas to ostr at destruction, or when // write() is called. - PNG_Canvas(unsigned int x, unsigned int y, std::ostream& ostr) - : Canvas(x, y), m_istr(0), m_ostr(&ostr), m_written(false) { } + PNG_Canvas(unsigned int x, unsigned int y, std::ostream& ostr); // I/O PNG_Canvas; read the Canvas from istr now, and write to ostr at // destruction or then write() is called. - PNG_Canvas(std::istream& istr, std::ostream& ostr) - : Canvas(), m_istr(&istr), m_ostr(&ostr), m_written(false) { read(); } + PNG_Canvas(std::istream& istr, std::ostream& ostr); // Call write() if we're an output PNG_Canvas, but trap any exceptions and // report a dmnsn_error() instead. @@ -56,12 +54,23 @@ namespace Dimension // error. void write(); + // Write the Canvas to the output stream in the background + Progress write_async(); + + // Construct an input or I/O PNG_Canvas in the background + static Progress read_async(std::istream& istr); + explicit PNG_Canvas(Progress& progress); + explicit PNG_Canvas(Progress& progress, std::ostream& ostr); + protected: // In case a derived class needs to set m_canvas after we're constructed - PNG_Canvas(std::ostream* ostr) - : Canvas(), m_istr(0), m_ostr(ostr), m_written(false) { } + explicit PNG_Canvas(std::ostream& ostr); private: + // Copying prohibited + PNG_Canvas(const PNG_Canvas&); + PNG_Canvas& operator=(const PNG_Canvas&); + std::istream* m_istr; std::ostream* m_ostr; bool m_written; @@ -69,10 +78,6 @@ namespace Dimension // Read the Canvas from a PNG file, and throw a Dimension_Error upon // failure. void read(); - - // Copying prohibited - PNG_Canvas(const PNG_Canvas&); - PNG_Canvas& operator=(const PNG_Canvas&); }; } diff --git a/libdimensionxx/dimensionxx/progress.hpp b/libdimensionxx/dimensionxx/progress.hpp index 72099c4..b9df870 100644 --- a/libdimensionxx/dimensionxx/progress.hpp +++ b/libdimensionxx/dimensionxx/progress.hpp @@ -22,16 +22,71 @@ #define DIMENSIONXX_PROGRESS_HPP #include // For tr1::shared_ptr +#include // dmnsn_canvas* wrapper. namespace Dimension { + // Base class for persisting objects + class Persist_Base + { + public: + virtual ~Persist_Base() = 0; + + protected: + Persist_Base() { } + + private: + // Copying prohibited + Persist_Base(const Persist_Base&); + Persist_Base& operator=(const Persist_Base&); + }; + + // Class for persisting objects + template + class Persist : public Persist_Base + { + public: + Persist(T* t) : m_t(t) { } + virtual ~Persist() { delete m_t; } + + T* persisted() const { return m_t; } + + private: + T* m_t; + }; + + // Class for persisting many objects + class Persister + { + public: + // Persister(); + // Persister(const Persister& persister); + // ~Persister(); + + // Persister& operator=(const Persister& persister); + + template + void persist(T* t); + + // Access the first persisted element + template + Persist& first(); + + private: + // Copy-assignment prohibited + Persister& operator=(const Persister&); + + std::list > m_persists; + }; + // dmnsn_progress* wrapper class to represent an asynchronous worker thread class Progress { public: explicit Progress(dmnsn_progress* progress); + Progress(dmnsn_progress* progress, const Persister& persister); // Progress(const Progress& progress); // Finishes the job without throwing @@ -47,6 +102,9 @@ namespace Dimension // Wait for job to finish, throwing if the job failed void finish(); + // Access the set of persisted objects + Persister& persister(); + // Access the wrapped C object. dmnsn_progress* dmnsn(); const dmnsn_progress* dmnsn() const; @@ -56,7 +114,22 @@ namespace Dimension Progress& operator=(const Progress&); std::tr1::shared_ptr m_progress; + Persister m_persister; }; + + template + void + Persister::persist(T* t) + { + m_persists.push_back(std::tr1::shared_ptr(new Persist(t))); + } + + template + Persist& + Persister::first() + { + return dynamic_cast&>(*m_persists.front()); + } } #endif /* DIMENSIONXX_PROGRESS_HPP */ -- cgit v1.2.3