XMLSerialization.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (C) 2014 Christian Schüller <schuellchr@gmail.com>
  3. //
  4. // This Source Code Form is subject to the terms of the Mozilla Public License
  5. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  6. // obtain one at http://mozilla.org/MPL/2.0/.
  7. #ifndef IGL_XML_SERIALIZATION_H
  8. #define IGL_XML_SERIALIZATION_H
  9. #include <igl/xml/serialize_xml.h>
  10. namespace igl
  11. {
  12. class XMLSerialization: public XMLSerializable
  13. {
  14. private:
  15. template <typename T>
  16. struct XMLSerializationObject: public XMLSerializable
  17. {
  18. bool Binary;
  19. std::string Name;
  20. T* Object;
  21. void Serialize(std::vector<char>& buffer) const {
  22. igl::serialize(*Object,Name,buffer);
  23. }
  24. void Deserialize(const std::vector<char>& buffer) {
  25. igl::deserialize(*Object,Name,buffer);
  26. }
  27. void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const {
  28. igl::serialize_xml(*Object,Name,doc,element,Binary);
  29. }
  30. void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element) {
  31. igl::deserialize_xml(*Object,Name,doc,element);
  32. }
  33. };
  34. mutable bool initialized;
  35. mutable std::vector<XMLSerializable*> objects;
  36. public:
  37. /**
  38. * Override this function to add your member variables which should be
  39. * serialized:
  40. *
  41. * this->Add(var1);
  42. * this->Add(var2);
  43. * ...
  44. */
  45. virtual void InitSerialization() = 0;
  46. /**
  47. * Following functions can be overridden to handle the specific events.
  48. * Return false to prevent the de-/serialization of an object.
  49. */
  50. virtual bool BeforeSerialization() const { return true; }
  51. virtual void AfterSerialization() const {}
  52. virtual bool BeforeDeserialization() { return true; }
  53. virtual void AfterDeserialization() {}
  54. /**
  55. * Default implementation of XMLSerializable interface
  56. */
  57. void Serialize(std::vector<char>& buffer) const
  58. {
  59. if(this->BeforeSerialization())
  60. {
  61. if(initialized == false)
  62. {
  63. objects.clear();
  64. (const_cast<XMLSerialization*>(this))->InitSerialization();
  65. initialized = true;
  66. }
  67. for(int i=0;i<objects.size();i++)
  68. objects[i]->Serialize(buffer);
  69. this->AfterSerialization();
  70. }
  71. }
  72. void Deserialize(const std::vector<char>& buffer)
  73. {
  74. if(this->BeforeDeserialization())
  75. {
  76. if(initialized == false)
  77. {
  78. objects.clear();
  79. (const_cast<XMLSerialization*>(this))->InitSerialization();
  80. initialized = true;
  81. }
  82. for(int i=0;i<objects.size();i++)
  83. objects[i]->Deserialize(buffer);
  84. this->AfterDeserialization();
  85. }
  86. }
  87. void Serialize(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* element) const
  88. {
  89. if(this->BeforeSerialization())
  90. {
  91. if(initialized == false)
  92. {
  93. objects.clear();
  94. (const_cast<XMLSerialization*>(this))->InitSerialization();
  95. initialized = true;
  96. }
  97. for(int i=0;i<objects.size();i++)
  98. objects[i]->Serialize(doc,element);
  99. this->AfterSerialization();
  100. }
  101. }
  102. void Deserialize(const tinyxml2::XMLDocument* doc,const tinyxml2::XMLElement* element)
  103. {
  104. if(this->BeforeDeserialization())
  105. {
  106. if(initialized == false)
  107. {
  108. objects.clear();
  109. (const_cast<XMLSerialization*>(this))->InitSerialization();
  110. initialized = true;
  111. }
  112. for(int i=0;i<objects.size();i++)
  113. objects[i]->Deserialize(doc,element);
  114. this->AfterDeserialization();
  115. }
  116. }
  117. /**
  118. * Default constructor, destructor, assignment and copy constructor
  119. */
  120. XMLSerialization()
  121. {
  122. initialized = false;
  123. }
  124. XMLSerialization(const XMLSerialization& obj)
  125. {
  126. initialized = false;
  127. objects.clear();
  128. }
  129. ~XMLSerialization()
  130. {
  131. initialized = false;
  132. objects.clear();
  133. }
  134. XMLSerialization& operator=(const XMLSerialization& obj)
  135. {
  136. if(this != &obj)
  137. {
  138. if(initialized)
  139. {
  140. initialized = false;
  141. objects.clear();
  142. }
  143. }
  144. return *this;
  145. }
  146. /**
  147. * Use this function to add your variables which should be serialized.
  148. */
  149. template <typename T>
  150. void Add(T& obj,std::string name, bool binary = false)
  151. {
  152. XMLSerializationObject<T>* object = new XMLSerializationObject<T>();
  153. object->Binary = binary;
  154. object->Name = name;
  155. object->Object = &obj;
  156. objects.push_back(object);
  157. }
  158. };
  159. }
  160. #endif