XMLElement.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "Rect.h"
  26. #include "Ptr.h"
  27. #include "Variant.h"
  28. class TiXmlElement;
  29. class XMLFile;
  30. /// Element in an XML file.
  31. class XMLElement
  32. {
  33. public:
  34. /// Construct null element.
  35. XMLElement();
  36. /// Construct with document and element pointers.
  37. XMLElement(XMLFile* file, TiXmlElement* element);
  38. /// Copy-construct from another element.
  39. XMLElement(const XMLElement& rhs);
  40. /// Destruct.
  41. ~XMLElement();
  42. /// Create a child element.
  43. XMLElement CreateChild(const String& name);
  44. /// Remove a child element, either first or last of them if several exist.
  45. bool RemoveChild(const String& name = String(), bool last = true);
  46. /// Remove child elements of certain name, or all child elements if name is empty.
  47. bool RemoveChildren(const String& name = String());
  48. /// %Set an attribute.
  49. bool SetAttribute(const String& name, const String& value);
  50. /// %Set a bool attribute.
  51. bool SetBool(const String& name, bool value);
  52. /// %Set a BoundingBox attribute.
  53. bool SetBoundingBox(const BoundingBox& value);
  54. /// %Set a buffer attribute.
  55. bool SetBuffer(const String& name, const void* data, unsigned size);
  56. /// %Set a buffer attribute.
  57. bool SetBuffer(const String& name, const PODVector<unsigned char>& value);
  58. /// %Set a color attribute.
  59. bool SetColor(const String& name, const Color& value);
  60. /// %Set a float attribute.
  61. bool SetFloat(const String& name, float value);
  62. /// %Set an integer attribute.
  63. bool SetInt(const String& name, int value);
  64. /// %Set an IntRect attribute.
  65. bool SetIntRect(const String& name, const IntRect& value);
  66. /// %Set an IntVector2 attribute.
  67. bool SetIntVector2(const String& name, const IntVector2& value);
  68. /// %Set a Rect attribute.
  69. bool SetRect(const String& name, const Rect& value);
  70. /// %Set a quaternion attribute.
  71. bool SetQuaternion(const String& name, const Quaternion& value);
  72. /// %Set a string attribute.
  73. bool SetString(const String& name, const String& value);
  74. /// %Set a variant attribute.
  75. bool SetVariant(const Variant& value);
  76. /// %Set a resource reference attribute.
  77. bool SetResourceRef(const ResourceRef& value);
  78. /// %Set a resource referene list attribute.
  79. bool SetResourceRefList(const ResourceRefList& value);
  80. /// %Set a variant vector attribute. Creates child elements as necessary.
  81. bool SetVariantVector(const VariantVector& value);
  82. /// %Set a variant map attribute. Creates child elements as necessary.
  83. bool SetVariantMap(const VariantMap& value);
  84. /// %Set a Vector2 attribute.
  85. bool SetVector2(const String& name, const Vector2& value);
  86. /// %Set a Vector3 attribute.
  87. bool SetVector3(const String& name, const Vector3& value);
  88. /// %Set a Vector4 attribute.
  89. bool SetVector4(const String& name, const Vector4& value);
  90. /// Return whether does not refer to an element.
  91. bool IsNull() const { return element_ == 0; }
  92. /// Return whether refers to an element.
  93. bool NotNull() const { return element_ != 0; }
  94. /// Return true if refers to an element.
  95. operator bool () const { return element_ != 0; }
  96. /// Return element name.
  97. String GetName() const;
  98. /// Return element contents.
  99. String GetText() const;
  100. /// Return whether has a child element.
  101. bool HasChild(const String& name) const;
  102. /// Return child element, or null if missing.
  103. XMLElement GetChild(const String& name = String()) const;
  104. /// Return next sibling element.
  105. XMLElement GetNext(const String& name = String()) const;
  106. /// Return parent element.
  107. XMLElement GetParent() const;
  108. /// Return number of attributes.
  109. unsigned GetNumAttributes() const;
  110. /// Return whether has an attribute.
  111. bool HasAttribute(const String& name) const;
  112. /// Return attribute, or empty if missing.
  113. String GetAttribute(const String& name) const;
  114. /// Return names of all attributes.
  115. Vector<String> GetAttributeNames() const;
  116. /// Return bool attribute, or false if missing.
  117. bool GetBool(const String& name) const;
  118. /// Return buffer attribute, or empty if missing.
  119. PODVector<unsigned char> GetBuffer(const String& name) const;
  120. /// Copy buffer attribute into a supplied buffer. Return true if buffer was large enough.
  121. bool GetBuffer(const String& name, void* dest, unsigned size) const;
  122. /// Return bounding box attribute, or empty if missing.
  123. BoundingBox GetBoundingBox() const;
  124. /// Return a color attribute, or default if missing.
  125. Color GetColor(const String& name) const;
  126. /// Return a float attribute, or zero if missing.
  127. float GetFloat(const String& name) const;
  128. /// Return an integer attribute, or zero if missing.
  129. int GetInt(const String& name) const;
  130. /// Return an IntRect attribute, or default if missing.
  131. IntRect GetIntRect(const String& name) const;
  132. /// Return an IntVector2 attribute, or default if missing.
  133. IntVector2 GetIntVector2(const String& name) const;
  134. /// Return a Rect attribute, or default if missing.
  135. Rect GetRect(const String& name) const;
  136. /// Return a quaternion attribute, or default if missing.
  137. Quaternion GetQuaternion(const String& name) const;
  138. /// Return a string attribute, or empty if missing.
  139. String GetString(const String& name) const;
  140. /// Return a string attribute in lowercase, or empty if missing.
  141. String GetStringLower(const String& name) const;
  142. /// Return a string attribute in uppercase, or empty if missing.
  143. String GetStringUpper(const String& name) const;
  144. /// Return a variant attribute, or empty if missing.
  145. Variant GetVariant() const;
  146. /// Return a resource reference attribute, or empty if missing.
  147. ResourceRef GetResourceRef() const;
  148. /// Return a resource reference list attribute, or empty if missing.
  149. ResourceRefList GetResourceRefList() const;
  150. /// Return a variant vector attribute, or empty if missing.
  151. VariantVector GetVariantVector() const;
  152. /// Return a variant map attribute, or empty if missing.
  153. VariantMap GetVariantMap() const;
  154. /// Return a Vector2 attribute, or default if missing.
  155. Vector2 GetVector2(const String& name) const;
  156. /// Return a Vector3 attribute, or default if missing.
  157. Vector3 GetVector3(const String& name) const;
  158. /// Return a Vector4 attribute, or default if missing.
  159. Vector4 GetVector4(const String& name) const;
  160. /// Return any Vector attribute as Vector4. Missing coordinates will be zero.
  161. Vector4 GetVector(const String& name) const;
  162. /// Return XML file.
  163. XMLFile* GetFile() const;
  164. /// Return TinyXML element.
  165. TiXmlElement* GetElement() const { return element_; }
  166. private:
  167. /// XML file.
  168. WeakPtr<XMLFile> file_;
  169. /// TinyXML element.
  170. TiXmlElement* element_;
  171. };