/** * XML Serialization * Simple and lightweight xml serialization class * * Original code by Lothar Perr * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any * damages arising from the use of this software. * * Permission is granted to anyone to use this software for any * purpose, including commercial applications, and to alter it and * redistribute it freely */ #pragma once #include "stdafx.h" #include #include #include #include #include "tinyxml2.h" /** XML Serialization */ namespace xmls { //using namespace std; #define RootClassName "XMLData" #define XMLClassEndTag std::string("" #define Empty_String std::string("") class Slo; /** serializable members base class */ class __declspec(dllexport) MemberBase { protected: std::string m_sValue; public: virtual ~MemberBase() {}; std::string toString(){return m_sValue;}; const char* c_str() {return m_sValue.c_str();}; std::string *getStringPtr() {return &m_sValue;}; }; /** serializable string */ class __declspec(dllexport) xString: public MemberBase { private: void AssignValue(const std::string value){m_sValue=value;}; public: xString(){}; xString(std::string value) {AssignValue(value);}; std::string value(){return m_sValue;}; xString operator=(const std::string value) {AssignValue(value);return *this;}; xString operator=(const char* value) {AssignValue(value);return *this;}; }; /** serializable int */ class __declspec(dllexport) xInt: public MemberBase { private: void AssignValue(const int value); public: xInt() {AssignValue(0);}; xInt(int value) {AssignValue(value);}; int value(); xInt operator=(const int value) {AssignValue(value);return *this;}; }; class __declspec(dllexport) xDouble : public MemberBase { private: void AssignValue(const double value); public: xDouble() { AssignValue(0); }; xDouble(double value) { AssignValue(value); }; double value(); xDouble operator=(const double value) { AssignValue(value); return *this; }; }; class __declspec(dllexport) xLong : public MemberBase { private: void AssignValue(const long value); public: xLong() { AssignValue(0); }; xLong(long value) { AssignValue(value); }; long value(); xLong operator=(const long value) { AssignValue(value); return *this; }; }; class __declspec(dllexport) xDWORD : public MemberBase { private: void AssignValue(const DWORD value); public: xDWORD() { AssignValue(0); }; xDWORD(DWORD value) { AssignValue(value); }; DWORD value(); xDWORD operator=(const DWORD value) { AssignValue(value); return *this; }; }; /** serializable bool */ class __declspec(dllexport) xBool: public MemberBase { private: void AssignValue(const bool value){m_sValue = value ? "true":"false";}; public: xBool() {AssignValue(false);}; xBool(bool value) {AssignValue(value);}; bool value(); xBool operator=(const bool value) {AssignValue(value);return *this;}; }; /** serializable time_t */ class __declspec(dllexport) xTime_t: public MemberBase { private: void AssignValue(const time_t value); public: xTime_t() {AssignValue(0);}; xTime_t(time_t value) {AssignValue(value);}; time_t value(); xTime_t operator=(const time_t value) {AssignValue(value);return *this;}; }; /** serializable OleDateTime */ class __declspec(dllexport) xOleDateTime : public MemberBase { private: void AssignValue(const COleDateTime value); public: xOleDateTime() { }; xOleDateTime(COleDateTime value) { AssignValue(value); }; COleDateTime value(); xOleDateTime operator=(const COleDateTime value) { AssignValue(value); return *this; }; }; class __declspec(dllexport) xOleDateTimeSpan : public MemberBase { private: void AssignValue(const COleDateTimeSpan value); public: xOleDateTimeSpan() { }; xOleDateTimeSpan(COleDateTimeSpan value) { AssignValue(value); }; COleDateTimeSpan value(); xOleDateTimeSpan operator=(const COleDateTimeSpan value) { AssignValue(value); return *this; }; }; /** serializable Rect */ class __declspec(dllexport) xRect : public MemberBase { private: void AssignValue(const CRect value, int shape = 1); public: xRect() { AssignValue(0); }; xRect(CRect value,int shape) { AssignValue(value,shape); }; CRect value(); xRect operator=(const CRect value) { AssignValue(value); return *this; }; }; /** serializable Point */ class __declspec(dllexport) xPoint : public MemberBase { private: void AssignValue(const CPoint value); public: xPoint() { AssignValue(0); }; xPoint(CPoint value) { AssignValue(value); }; CPoint value(); xPoint operator=(const CPoint value) { AssignValue(value); return *this; }; }; /** Class-collection base */ class CollectionBase; typedef std::map::iterator __declspec(dllexport) CollectionIterator; class __declspec(dllexport) CollectionBase { friend class ISlo; private: std::string m_sCollectionName; std::string m_sCollectionClassType; public: CollectionBase() { m_vCollection.clear(); m_mOwner.clear(); }; std::vector m_vCollection; std::map m_mOwner; void setCollectionName(std::string value) {m_sCollectionName=value;}; void setCollectionClassType(std::string value) {m_sCollectionClassType=value;}; virtual ISlo *newElement()=0; public: virtual ~CollectionBase() { m_sCollectionName.clear(); m_sCollectionClassType.clear(); m_vCollection.clear(); m_mOwner.clear(); }; std::string getCollectionName() { return m_sCollectionName; }; size_t size() {return m_vCollection.size();}; ISlo *getItem(int itemID) { return m_vCollection.at(itemID);}; void Clear(); }; /** Class-collection template */ template class __declspec(dllexport) Collection: public CollectionBase { //friend class Slo; public: ~Collection() {Clear(); }; T*newElement(); void addItem(T*item) { m_vCollection.push_back(item);/* m_mOwner[item] = false;*/ }; T*getItem(int itemID) { return (T*)m_vCollection.at(itemID); }; }; /** create new element of type T @return empty object of type T */ template T* Collection::newElement() { T* newItem = new T(); m_vCollection.push_back(newItem); //I change this value to be false forever(gsp).No matter what case there's no need to set the object's owner to the collection //after we created the object we'll put them to in a smartpointer.then the smartpointer will manage it. m_mOwner[newItem]=false;//m_mOwner[newItem]=true return newItem; }; /** Serializeable base class derive your serializable class from Serializable */ class __declspec(dllexport) ISlo { public: virtual void Serialize(bool isStoring, tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode) = 0; virtual ~ISlo() = default; }; typedef std::vector::iterator __declspec(dllexport) SerializableIterator; class __declspec(dllexport) Slo:public ISlo { friend class CollectionBase; private: Slo(Slo const &s) { } Slo operator=(Slo const &s) {return *this;}; static std::string strReplaceAll(std::string source, const std::string from, const std::string to); void toXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode); void fromXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode); public: std::string m_sXML; std::string m_sClassName; std::string m_sVersion; std::map m_AttributeMappings; std::map m_MemberMappings; std::map m_MemberCollections; void setClassName(std::string ClassName) { m_sClassName = ClassName; }; Slo(); virtual ~Slo(); public: void Register(std::string MemberName, MemberBase *Member, std::string DefaultValue = ""); void Register(std::string MemberName, ISlo *Member); void Register(std::string CollectionName, CollectionBase *SubclassCollection); virtual void Serialize(bool isStoring, tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode) { if (isStoring) { toXML(classDoc, rootNode); } else { fromXML(classDoc, rootNode); } }; static std::string IdentifyClass(std::string XMLSource); static std::string IdentifyClassVersion(std::string XMLSource); std::string getClassName(){return m_sClassName;}; void setVersion(std::string value) {m_sVersion=value;}; std::string getVersion() {return m_sVersion;}; void Clear(); }; void __declspec(dllexport) SplitString(const std::string& s, std::vector& v, const std::string& c); std::vector __declspec(dllexport) SplitString(const std::string& s, const std::string& c); void __declspec(dllexport) ReplaceAll(std::string& content, std::string searchFor, std::string replaceWith); void __declspec(dllexport) ReplaceFirst(std::string& content, std::string searchFor, std::string replaceWith); }