XMLSerializable.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef IGL_XML_XMLSERIALIZABLE_H
  2. #define IGL_XML_XMLSERIALIZABLE_H
  3. #include "../igl_inline.h"
  4. #include "../serialize.h"
  5. #include <tinyxml2.h>
  6. // Interface for xml-serializable class see serialize_xml.h
  7. // Pretty sure all of these IGL_INLINE should be inline
  8. namespace igl
  9. {
  10. namespace xml
  11. {
  12. // interface for user defined types
  13. struct XMLSerializableBase : public SerializableBase
  14. {
  15. virtual void Serialize(std::vector<char>& buffer) const = 0;
  16. virtual void Deserialize(const std::vector<char>& buffer) = 0;
  17. virtual void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const = 0;
  18. virtual void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) = 0;
  19. };
  20. // Convenient interface for user defined types
  21. class XMLSerializable: public XMLSerializableBase
  22. {
  23. private:
  24. template <typename T>
  25. struct XMLSerializationObject: public XMLSerializableBase
  26. {
  27. bool Binary;
  28. std::string Name;
  29. T* Object;
  30. void Serialize(std::vector<char>& buffer) const {
  31. serialize(*Object,Name,buffer);
  32. }
  33. void Deserialize(const std::vector<char>& buffer) {
  34. deserialize(*Object,Name,buffer);
  35. }
  36. void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const {
  37. serialize_xml(*Object,Name,doc,element,Binary);
  38. }
  39. void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) {
  40. deserialize_xml(*Object,Name,doc,element);
  41. }
  42. };
  43. mutable bool initialized;
  44. mutable std::vector<XMLSerializableBase*> objects;
  45. public:
  46. // Override this function to add your member variables which should be serialized
  47. IGL_INLINE virtual void InitSerialization() = 0;
  48. // Following functions can be overridden to handle the specific events.
  49. // Return false to prevent the de-/serialization of an object.
  50. IGL_INLINE virtual bool PreSerialization() const;
  51. IGL_INLINE virtual void PostSerialization() const;
  52. IGL_INLINE virtual bool PreDeserialization();
  53. IGL_INLINE virtual void PostDeserialization();
  54. // Default implementation of XMLSerializableBase interface
  55. IGL_INLINE void Serialize(std::vector<char>& buffer) const;
  56. IGL_INLINE void Deserialize(const std::vector<char>& buffer);
  57. IGL_INLINE void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const;
  58. IGL_INLINE void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element);
  59. // Default constructor, destructor, assignment and copy constructor
  60. IGL_INLINE XMLSerializable();
  61. IGL_INLINE XMLSerializable(const XMLSerializable& obj);
  62. IGL_INLINE ~XMLSerializable();
  63. IGL_INLINE XMLSerializable& operator=(const XMLSerializable& obj);
  64. // Use this function to add your variables which should be serialized
  65. template <typename T>
  66. IGL_INLINE void Add(T& obj,std::string name,bool binary = false);
  67. };
  68. // IMPLEMENTATION
  69. IGL_INLINE bool XMLSerializable::PreSerialization() const
  70. {
  71. return true;
  72. }
  73. IGL_INLINE void XMLSerializable::PostSerialization() const
  74. {
  75. }
  76. IGL_INLINE bool XMLSerializable::PreDeserialization()
  77. {
  78. return true;
  79. }
  80. IGL_INLINE void XMLSerializable::PostDeserialization()
  81. {
  82. }
  83. IGL_INLINE void XMLSerializable::Serialize(std::vector<char>& buffer) const
  84. {
  85. if(this->PreSerialization())
  86. {
  87. if(initialized == false)
  88. {
  89. objects.clear();
  90. (const_cast<XMLSerializable*>(this))->InitSerialization();
  91. initialized = true;
  92. }
  93. for(unsigned int i=0;i<objects.size();i++)
  94. objects[i]->Serialize(buffer);
  95. this->PostSerialization();
  96. }
  97. }
  98. IGL_INLINE void XMLSerializable::Deserialize(const std::vector<char>& buffer)
  99. {
  100. if(this->PreDeserialization())
  101. {
  102. if(initialized == false)
  103. {
  104. objects.clear();
  105. (const_cast<XMLSerializable*>(this))->InitSerialization();
  106. initialized = true;
  107. }
  108. for(unsigned int i=0;i<objects.size();i++)
  109. objects[i]->Deserialize(buffer);
  110. this->PostDeserialization();
  111. }
  112. }
  113. IGL_INLINE void XMLSerializable::Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const
  114. {
  115. if(this->PreSerialization())
  116. {
  117. if(initialized == false)
  118. {
  119. objects.clear();
  120. (const_cast<XMLSerializable*>(this))->InitSerialization();
  121. initialized = true;
  122. }
  123. for(unsigned int i=0;i<objects.size();i++)
  124. objects[i]->Serialize(doc,element);
  125. this->PostSerialization();
  126. }
  127. }
  128. IGL_INLINE void XMLSerializable::Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
  129. {
  130. if(this->PreDeserialization())
  131. {
  132. if(initialized == false)
  133. {
  134. objects.clear();
  135. (const_cast<XMLSerializable*>(this))->InitSerialization();
  136. initialized = true;
  137. }
  138. for(unsigned int i=0;i<objects.size();i++)
  139. objects[i]->Deserialize(doc,element);
  140. this->PostDeserialization();
  141. }
  142. }
  143. IGL_INLINE XMLSerializable::XMLSerializable()
  144. {
  145. initialized = false;
  146. }
  147. IGL_INLINE XMLSerializable::XMLSerializable(const XMLSerializable& obj)
  148. {
  149. initialized = false;
  150. objects.clear();
  151. }
  152. IGL_INLINE XMLSerializable::~XMLSerializable()
  153. {
  154. initialized = false;
  155. objects.clear();
  156. }
  157. IGL_INLINE XMLSerializable& XMLSerializable::operator=(const XMLSerializable& obj)
  158. {
  159. if(this != &obj)
  160. {
  161. if(initialized)
  162. {
  163. initialized = false;
  164. objects.clear();
  165. }
  166. }
  167. return *this;
  168. }
  169. template <typename T>
  170. IGL_INLINE void XMLSerializable::Add(T& obj,std::string name,bool binary)
  171. {
  172. XMLSerializationObject<T>* object = new XMLSerializationObject<T>();
  173. object->Binary = binary;
  174. object->Name = name;
  175. object->Object = &obj;
  176. objects.push_back(object);
  177. }
  178. }
  179. }
  180. #endif