#ifndef __IMAGE_STRING__ #define __IMAGE_STRING__ #include #include #include #include #include #include #include #include #include namespace image { //! @addtogroup image //@{ /*! @defgroup imageSerializer Serialisierung (Speicherung) der Images Um ein korrektes Image zu erstellen, kann man nicht die Boost Serialisierer benützen, da diese nebst den reinen Daten auch noch Zusatzinformation, speziell Versionsinformation, speichern, welche im Imageformat nicht vorhanden sein dürfen. Dieser Serialisierer ist sehr simpel und speichert Daten binär 1:1. */ //@} //! @addtogroup imageSerializer //@{ //! Speichert binäre Daten 1:1 in einem Boost Archiv. /*! @throw std::runtime_error bei Schreibfehler. */ class BinaryWriter: public boost::archive::detail::common_oarchive { //................................................................. methods public: BinaryWriter(std::ostream& os): boost::archive::detail::common_oarchive(0), _os(os) { LOG_METHOD; } void save_binary(const void *address, std::size_t size) { LOG_METHOD; if (!_os.write((const char*)address, size)) LOG_THROW(std::runtime_error("Cannot write binary image file!"), (boost::format ("Bad file stream. Cannot write %1% bytes:\n%2%") %size%logger::binary((const char*)address, size)) .str()); }; template void save_override(const T& t, int) { LOG_METHOD; boost::archive::save(*this, t); } void save_override(const boost::archive::version_type&, int) {} void save_override(const boost::archive::object_id_type&, int) {} void save_override(const boost::archive::class_id_type&, int) {} void save_override(const boost::archive::class_id_optional_type&, int) {} void save_override(const boost::archive::class_id_reference_type&, int) {} void save_override(const boost::archive::tracking_type&, int) {} //................................................................. methods private: template void save(const T &t) { LOG_METHOD; save_binary(&t, sizeof(T)); } friend class boost::archive::save_access; private: std::ostream& _os; }; //! Lädt binäre Daten 1:1 aus einem Boost Archiv. /*! @throw std::runtime_error bei Lesefehler (fehlende Daten). */ class BinaryReader: public boost::archive::detail::common_iarchive { //................................................................. methods public: BinaryReader(std::istream& is): boost::archive::detail::common_iarchive(0), _is(is) { LOG_METHOD; } template void load_override(T& t, int) { LOG_METHOD; boost::archive::load(*this, t); } void load_override(boost::archive::version_type&, int) {} void load_override(boost::archive::object_id_type&, int) {} void load_override(boost::archive::class_id_type&, int) {} void load_override(boost::archive::class_id_optional_type&, int) {} void load_override(boost::archive::class_id_reference_type&, int) {} void load_override(boost::archive::tracking_type&, int) {} void load_binary(void *address, std::size_t size) { LOG_METHOD; if (!_is.read((char*)address, size)) LOG_THROW(std::runtime_error("Cannot read binary image file!"), (boost::format("Bad file stream, cannot read %1% bytes") %size).str()); }; //................................................................. methods private: template void load(T &t) { LOG_METHOD; load_binary(&t, sizeof(T)); } friend class boost::archive::load_access; //............................................................... variables private: std::istream& _is; }; //@} } #endif