diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-06-24 21:28:51 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-06-24 21:28:51 +0000 |
commit | 603712c38a127e297eddd23975fb950499a0c10c (patch) | |
tree | 1ec4a3f870f57ab72b0003399657654d91bf247b /libdimensionxx/dimensionxx | |
parent | 706acbb46a91adef8ed2ec1d255802e93c59b65a (diff) | |
download | dimension-603712c38a127e297eddd23975fb950499a0c10c.tar.xz |
New asynchronous PNG_Canvas interface.
Diffstat (limited to 'libdimensionxx/dimensionxx')
-rw-r--r-- | libdimensionxx/dimensionxx/png.hpp | 29 | ||||
-rw-r--r-- | libdimensionxx/dimensionxx/progress.hpp | 73 |
2 files changed, 90 insertions, 12 deletions
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 <istream> #include <ostream> +#include <memory> 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 <tr1/memory> // For tr1::shared_ptr +#include <list> // 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 <typename T> + 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 <typename T> + void persist(T* t); + + // Access the first persisted element + template <typename T> + Persist<T>& first(); + + private: + // Copy-assignment prohibited + Persister& operator=(const Persister&); + + std::list<std::tr1::shared_ptr<Persist_Base> > 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<dmnsn_progress*> m_progress; + Persister m_persister; }; + + template <typename T> + void + Persister::persist(T* t) + { + m_persists.push_back(std::tr1::shared_ptr<Persist_Base>(new Persist<T>(t))); + } + + template <typename T> + Persist<T>& + Persister::first() + { + return dynamic_cast<Persist<T>&>(*m_persists.front()); + } } #endif /* DIMENSIONXX_PROGRESS_HPP */ |