/*************************************************************************** * Copyright (C) gempa GmbH * * All rights reserved. * * Contact: gempa GmbH (seiscomp-dev@gempa.de) * * * * GNU Affero General Public License Usage * * This file may be used under the terms of the GNU Affero * * Public License version 3.0 as published by the Free Software Foundation * * and appearing in the file LICENSE included in the packaging of this * * file. Please review the following information to ensure the GNU Affero * * Public License version 3.0 requirements will be met: * * https://www.gnu.org/licenses/agpl-3.0.html. * * * * Other Usage * * Alternatively, this file may be used in accordance with the terms and * * conditions contained in a signed written agreement between you and * * gempa GmbH. * ***************************************************************************/ #ifndef SEISCOMP_CORE_ARCHIVE_H #define SEISCOMP_CORE_ARCHIVE_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DECLARE_ROOT_SERIALIZATION(RootClass) \ public: \ typedef Seiscomp::Core::Generic::Archive Archive; \ virtual void serialize(Archive&) {} #define DECLARE_SERIALIZATION \ public: \ virtual void serialize(Archive& ar) override namespace Seiscomp { namespace Core { namespace Generic { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> /** \brief A template archive interface An archive offers an interface to read from and write to datasources. */ template class Archive { // ------------------------------------------------------------------ // Traits // ------------------------------------------------------------------ public: typedef ROOT_TYPE RootType; typedef boost::variant PropertyValue; typedef std::map Properties; // ------------------------------------------------------------------ // Public Types // ------------------------------------------------------------------ public: //! Serialization hints enum { NONE = 0, STATIC_TYPE = 0x01, //! Objects should not serialize their child elements IGNORE_CHILDS = 0x02, //! Objects are stored as XML nodes not as XML attributes XML_ELEMENT = 0x04, //! Objects are stored as XML cdata not as XML attributes XML_CDATA = 0x08, //! This attribute is mandatory even if empty XML_MANDATORY = 0x10, //! Objects are stored in a seperate database table and //! not in columns of the parent object table DB_TABLE = 0x20, //! The time is stored in two records: time and microseconds SPLIT_TIME = 0x40, //! This is just an informational flag used for database //! access mostly. This flag is the only one that will //! be kept alive when serializing child objects. INDEX_ATTRIBUTE = 0x80 }; // ------------------------------------------------------------------ // Xstruction // ------------------------------------------------------------------ protected: //! Constructor Archive(); public: //! Destructor virtual ~Archive() {} // ------------------------------------------------------------------ // Public Interface // ------------------------------------------------------------------ public: static int PackVersion(int major, int minor) { return major << 16 | (minor & 0xFFFF); } /** Opens an archive. Every archive class interpretes the dataSource parameter in its own way. It can either point to a named dataSource (file) or to a block of memory containing the actual archive data. */ virtual bool open(const char* dataSource); //! Creates a new archive virtual bool create(const char* dataSource); virtual void close() = 0; /** * @brief Sets strict reading mode. In strict mode optional attributes * must be parsed correctly otherwise the archive is not valid. * If strict mode is disabled then invalid optional attributes * or objects are set to None or nullptr. * @param strict Enabled or disabled */ void setStrictMode(bool strict); bool isStrictMode() const; //! Queries whether the archive is in reading mode or not bool isReading() const; //! Returns whether the last operation was successfull or not bool success() const; //! Returns the serialization hints to propose a special //! behaviour to serializable objects. int hint() const; //! Sets the current serialization hint void setHint(int); //! Sets the validity during serialization if needed void setValidity(bool); void setVersion(Version v) { _version = v; } Version version() const { return _version; } int versionMajor() const { return _version.majorTag(); } int versionMinor() const { return _version.minorTag(); } template bool isLowerVersion() const { return _version.packed ::Value; } template bool isVersion() const { return _version.packed == VersionPacker::Value; } template bool isHigherVersion() const { return _version.packed > VersionPacker::Value; } template bool supportsVersion() const { return _version.packed >= VersionPacker::Value; } // ------------------------------------------------------------------ // Property interface // ------------------------------------------------------------------ public: //! Returns the number of user set properties size_t propertyCount() const; //! Sets a value for the named property. If the property does not //! yet exist, it will be added and false will be returned. If //! the property exists already, true is returned. The value is //! updated in both cases. bool setProperty(const char *name, const PropertyValue &v); //! Returns a property (if set) or nullptr pointer given a property //! name. const PropertyValue *property(const char *name) const; const int *propertyInt(const char *name) const; const double *propertyDouble(const char *name) const; const std::string *propertyString(const char *name) const; //! Removes all set properties void clearProperties(); // ------------------------------------------------------------------ // Read methods // ------------------------------------------------------------------ public: //! Reads an integer virtual void read(std::int8_t &value) = 0; virtual void read(std::int16_t &value) = 0; virtual void read(std::int32_t &value) = 0; virtual void read(std::int64_t &value) = 0; //! Reads a float virtual void read(float &value) = 0; //! Reads a double virtual void read(double &value) = 0; //! Reads a float complex virtual void read(std::complex &value) = 0; //! Reads a double complex virtual void read(std::complex &value) = 0; //! Reads a boolean virtual void read(bool &value) = 0; //! Reads a vector of chars virtual void read(std::vector &value) = 0; //! Reads a vector of ints virtual void read(std::vector &value) = 0; virtual void read(std::vector &value) = 0; virtual void read(std::vector &value) = 0; virtual void read(std::vector &value) = 0; //! Reads a vector of floats virtual void read(std::vector &value) = 0; //! Reads a vector of doubles virtual void read(std::vector &value) = 0; //! Reads a vector of complex doubles virtual void read(std::vector > &value) = 0; //! Reads a vector of strings virtual void read(std::vector &value) = 0; //! Reads a vector of time virtual void read(std::vector