PolyObject.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. class TiXmlElement;
  23. namespace Polycode {
  24. /**
  25. * Single entry in an Object. Object entries can be accessed as dictionaries or arrays.
  26. */
  27. class _PolyExport ObjectEntry {
  28. public:
  29. /**
  30. * Default constructor
  31. */
  32. ObjectEntry() { type = ObjectEntry::CONTAINER_ENTRY; }
  33. /**
  34. * Type of entry. Possible values are (FLOAT_ENTRY, INT_ENTRY, BOOL_ENTRY, ARRAY_ENTRY, STRING_ENTRY, CONTAINER_ENTRY).
  35. */
  36. int type;
  37. /**
  38. * Lookup key for this object entry.
  39. */
  40. String name;
  41. /**
  42. * Number value of this object entry.
  43. */
  44. Number NumberVal;
  45. /**
  46. * Integer value of this object entry.
  47. */
  48. int intVal;
  49. /**
  50. * String value of this object entry.
  51. */
  52. String stringVal;
  53. /**
  54. * Boolean value of this object entry.
  55. */
  56. bool boolVal;
  57. /**
  58. * Length of this object entry if its type is ARRAY_ENTRY.
  59. */
  60. int length;
  61. /**
  62. * Adds an empty child entry.
  63. * @param name Lookup key for the object entry.
  64. * @return The added object entry.
  65. */
  66. ObjectEntry *addChild(const String& name) {
  67. ObjectEntry *entry = new ObjectEntry();
  68. entry->type = ObjectEntry::CONTAINER_ENTRY;
  69. entry->name = name;
  70. children.push_back(entry);
  71. length = children.size();
  72. return entry;
  73. }
  74. /**
  75. * Adds an empty child entry with a number value.
  76. * @param name Lookup key for the object entry.
  77. * @param val Number to set as value in the object entry.
  78. * @return The added object entry.
  79. */
  80. ObjectEntry *addChild(const String& name, Number val) {
  81. ObjectEntry *entry = new ObjectEntry();
  82. entry->type = ObjectEntry::FLOAT_ENTRY;
  83. entry->NumberVal = val;
  84. entry->name = name;
  85. children.push_back(entry);
  86. length = children.size();
  87. return entry;
  88. }
  89. /**
  90. * Adds an empty child entry with an integer value.
  91. * @param name Lookup key for the object entry.
  92. * @param val Integer to set as value in the object entry.
  93. * @return The added object entry.
  94. */
  95. ObjectEntry *addChild(const String& name, int val) {
  96. ObjectEntry *entry = new ObjectEntry();
  97. entry->type = ObjectEntry::INT_ENTRY;
  98. entry->intVal = val;
  99. entry->name = name;
  100. children.push_back(entry);
  101. length = children.size();
  102. return entry;
  103. }
  104. /**
  105. * Adds an empty child entry with a string value.
  106. * @param name Lookup key for the object entry.
  107. * @param val String to set as value in the object entry.
  108. * @return The added object entry.
  109. */
  110. ObjectEntry *addChild(const String& name, const String& val) {
  111. ObjectEntry *entry = new ObjectEntry();
  112. entry->type = ObjectEntry::STRING_ENTRY;
  113. entry->stringVal = val;
  114. entry->name = name;
  115. children.push_back(entry);
  116. length = children.size();
  117. return entry;
  118. }
  119. /**
  120. * Adds an empty child entry with a boolean value.
  121. * @param name Lookup key for the object entry.
  122. * @param val Boolean to set as value in the object entry.
  123. * @return The added object entry.
  124. */
  125. ObjectEntry *addChild(const String& name, bool val) {
  126. ObjectEntry *entry = new ObjectEntry();
  127. entry->type = ObjectEntry::BOOL_ENTRY;
  128. entry->boolVal = val;
  129. entry->name = name;
  130. children.push_back(entry);
  131. length = children.size();
  132. return entry;
  133. }
  134. ObjectEntry *addChild(ObjectEntry *entry) {
  135. children.push_back(entry);
  136. length = children.size();
  137. return entry;
  138. }
  139. void Clear();
  140. static const int FLOAT_ENTRY = 0;
  141. static const int INT_ENTRY = 1;
  142. static const int BOOL_ENTRY = 2;
  143. static const int ARRAY_ENTRY = 3;
  144. static const int STRING_ENTRY = 4;
  145. static const int CONTAINER_ENTRY = 5;
  146. /**
  147. * Accesses an object entry as an array by an integer lookup.
  148. * @param index Lookup index to return value for.
  149. * @return Object entry corresponding to the lookup index or NULL if one doesn't exist.
  150. */
  151. inline ObjectEntry *operator [] ( int index) { return children[index];}
  152. /**
  153. * Accesses an object entry by a string lookup and returns the corresponding object entry.
  154. * @param key Lookup key to return value for.
  155. * @return Object entry corresponding to the string value or NULL if one doesn't exist.
  156. */
  157. inline ObjectEntry *operator [] ( const String& key) { for(int i=0; i < children.size(); i++) { if(children[i]->name == key) { return children[i]; } } return NULL; }
  158. std::vector<ObjectEntry*> children;
  159. };
  160. /**
  161. * Basic dictionary data object. Objects can store organized data and save and load it from disk. An object contains a hierarchy of ObjectEntry classes which hold the actual data.
  162. */
  163. class _PolyExport Object {
  164. public:
  165. /**
  166. * Default constructor
  167. */
  168. Object();
  169. virtual ~Object();
  170. /**
  171. * Loads data from XML file into the object.
  172. * @param fileName Path to the XML file to load.
  173. * @return Returns true is succesful, false if otherwise.
  174. */
  175. bool loadFromXML(const String& fileName);
  176. /**
  177. * Loads data from XML string into the object.
  178. * @param xmlString XML data in a string.
  179. * @return Returns true is succesful, false if otherwise.
  180. */
  181. bool loadFromXMLString(const String &xmlString);
  182. /**
  183. * Saves the object to an XML file.
  184. * @param fileName Path to the XML file to save to.
  185. */
  186. void saveToXML(const String& fileName);
  187. void createFromXMLElement(TiXmlElement *element, ObjectEntry *entry);
  188. TiXmlElement *createElementFromObjectEntry(ObjectEntry *entry);
  189. /**
  190. * Root object entry.
  191. */
  192. ObjectEntry root;
  193. };
  194. }