QGearsXMLSerializer.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <Ogre.h>
  17. #include <OgreAxisAlignedBox.h>
  18. #include <OgreColourValue.h>
  19. #include <OgreSerializer.h>
  20. #include <tinyxml.h>
  21. #include "common/TypeDefine.h"
  22. namespace QGears{
  23. #ifdef NDEBUG
  24. #define assertElement(node)((void)0)
  25. #else
  26. #define assertElement(node)\
  27. {\
  28. if(node.Type() != TiXmlNode::TINYXML_ELEMENT)\
  29. {\
  30. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS\
  31. ,"node has wrong type, needs to be TINYXML_ELEMENT"\
  32. ,"XMLSerializer::assertElement");\
  33. }\
  34. }
  35. #endif
  36. /**
  37. * Handles the serialization of XML files.
  38. */
  39. class XMLSerializer{
  40. public:
  41. /**
  42. * Constructor.
  43. */
  44. XMLSerializer();
  45. /**
  46. * Destructor.
  47. */
  48. virtual ~XMLSerializer();
  49. protected:
  50. /**
  51. * Reads and parses the XML file.
  52. *
  53. * @param stream[in] The contents of the XML file.
  54. * @param dest[out] The formed XML file.
  55. */
  56. virtual void Parse(
  57. Ogre::DataStreamPtr &stream, TiXmlDocument &dest
  58. );
  59. /**
  60. * Reads an XMl node attribute as a string.
  61. *
  62. * @param node[in] The XML node.
  63. * @param attribute[in] The name of the attribute to read.
  64. * @return The value of the specified attribute.
  65. */
  66. virtual const String* ReadAttribute(
  67. TiXmlNode &node, const String &attribute
  68. );
  69. /**
  70. * Reads an XMl node attribute as a boolean.
  71. *
  72. * @param node[in] The XML node.
  73. * @param attribute[in] The name of the attribute to read.
  74. * @param dest[out] The value of the specified attribute. If the
  75. * attribute doesn't exists, the value of def will be set here.
  76. * @return True if the attribute was actually read, false if it
  77. * didn't exist and the default value was loaded into dest.
  78. */
  79. virtual bool ReadAttribute(
  80. TiXmlNode &node, const String &attribute, bool &dest,
  81. const bool &def = false
  82. );
  83. /**
  84. * Reads an XMl node attribute as an integer.
  85. *
  86. * @param node[in] The XML node.
  87. * @param attribute[in] The name of the attribute to read.
  88. * @param dest[out] The value of the specified attribute. If the
  89. * attribute doesn't exists, the value of def will be set here.
  90. * @return True if the attribute was actually read, false if it
  91. * didn't exist and the default value was loaded into dest.
  92. */
  93. virtual bool ReadAttribute(
  94. TiXmlNode &node, const String &attribute, int &dest,
  95. const int &def = 0
  96. );
  97. /**
  98. * Reads an XMl node attribute as a string.
  99. *
  100. * @param node[in] The XML node.
  101. * @param attribute[in] The name of the attribute to read.
  102. * @param dest[out] The value of the specified attribute. If the
  103. * attribute doesn't exists, the value of def will be set here.
  104. * @return True if the attribute was actually read, false if it
  105. * didn't exist and the default value was loaded into dest.
  106. */
  107. virtual bool ReadAttribute(
  108. TiXmlNode &node, const String &attribute, String &dest,
  109. const String &def = ""
  110. );
  111. /**
  112. * Reads an XMl node attribute as a real.
  113. *
  114. * @param node[in] The XML node.
  115. * @param attribute[in] The name of the attribute to read.
  116. * @param dest[out] The value of the specified attribute. If the
  117. * attribute doesn't exists, the value of def will be set here.
  118. * @return True if the attribute was actually read, false if it
  119. * didn't exist and the default value was loaded into dest.
  120. */
  121. virtual bool ReadAttribute(
  122. TiXmlNode &node, const String &attribute, Ogre::Real &dest,
  123. const Ogre::Real &def = 0
  124. );
  125. /**
  126. * Reads an XMl node attribute as a 2-dimension vector.
  127. *
  128. * @param node[in] The XML node.
  129. * @param attribute[in] The name of the attribute to read.
  130. * @param dest[out] The value of the specified attribute. If the
  131. * attribute doesn't exists, the value of def will be set here.
  132. * @return True if the attribute was actually read, false if it
  133. * didn't exist and the default value was loaded into dest.
  134. */
  135. virtual bool ReadAttribute(
  136. TiXmlNode &node, const String &attribute, Ogre::Vector2 &dest,
  137. const Ogre::Vector2 &def = Ogre::Vector2::ZERO
  138. );
  139. /**
  140. * Reads an XMl node attribute as a 3-dimension vector.
  141. *
  142. * @param node[in] The XML node.
  143. * @param attribute[in] The name of the attribute to read.
  144. * @param dest[out] The value of the specified attribute. If the
  145. * attribute doesn't exists, the value of def will be set here.
  146. * @return True if the attribute was actually read, false if it
  147. * didn't exist and the default value was loaded into dest.
  148. */
  149. virtual bool ReadAttribute(
  150. TiXmlNode &node, const String &attribute, Ogre::Vector3 &dest,
  151. const Ogre::Vector3 &def = Ogre::Vector3::ZERO
  152. );
  153. /**
  154. * Reads an XMl node attribute as a 4-dimension vector.
  155. *
  156. * @param node[in] The XML node.
  157. * @param attribute[in] The name of the attribute to read.
  158. * @param dest[out] The value of the specified attribute. If the
  159. * attribute doesn't exists, the value of def will be set here.
  160. * @return True if the attribute was actually read, false if it
  161. * didn't exist and the default value was loaded into dest.
  162. */
  163. virtual bool ReadAttribute(
  164. TiXmlNode &node, const String &attribute, Ogre::Vector4 &dest,
  165. const Ogre::Vector4 &def = Ogre::Vector4::ZERO
  166. );
  167. /**
  168. * Reads an XMl node attribute as a quaternion.
  169. *
  170. * @param node[in] The XML node.
  171. * @param attribute[in] The name of the attribute to read.
  172. * @param dest[out] The value of the specified attribute. If the
  173. * attribute doesn't exists, the value of def will be set here.
  174. * @return True if the attribute was actually read, false if it
  175. * didn't exist and the default value was loaded into dest.
  176. */
  177. virtual bool ReadAttribute(
  178. TiXmlNode &node, const String &attribute, Ogre::Quaternion &dest,
  179. const Ogre::Quaternion &def = Ogre::Quaternion::IDENTITY
  180. );
  181. /**
  182. * Finds a child node of a XML node by name.
  183. *
  184. * It doesn't search recursively, just among the direct children.
  185. *
  186. * @param node[in] The XML node to search.
  187. * @param tag[in] Name of the child to search for.
  188. * @return The child XML node by the specified name, or nullprt if
  189. * there is no one that matches the name.
  190. */
  191. virtual TiXmlNode* FindChildNode(
  192. TiXmlNode &node, const String &tag
  193. );
  194. };
  195. }