daeMetaAttribute.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. */
  8. #ifndef __DAE_META_ATTRIBUTE_H__
  9. #define __DAE_META_ATTRIBUTE_H__
  10. #include <string>
  11. #include <sstream>
  12. #include <dae/daeTypes.h>
  13. #include <dae/daeStringRef.h>
  14. #include <dae/daeAtomicType.h>
  15. #include <dae/daeElement.h>
  16. #include <dae/daeArray.h>
  17. class daeElement;
  18. class daeMetaElement;
  19. class daeMetaAttribute;
  20. class daeMetaElementAttribute;
  21. /**
  22. * The @c daeMetaAttribute class describes one attribute in a C++ COLLADA dom element.
  23. *
  24. * In the case of the C++ object model a conceptual attribute can be
  25. * either a dom attribute, a dom element, or a dom value.
  26. * Essentially, the meta attribute describes fields on the C++ class.
  27. * However these attributes are stored separately in the containing meta
  28. * @c daeMetaElement.
  29. * @c daeMetaAttributes always exist inside of @c daeMetaElements.
  30. * Each @c daeMetaAttribute has certain semantic operations it is capable of
  31. * including @c set(), @c get(), and @c print().
  32. * @c daeMetaAttributes use the @c daeAtomicType system as their underlying semantic
  33. * implementation, but contain additional information about the packaging
  34. * of the atomic types into the C++ dom classes such as offset, and
  35. * array information.
  36. */
  37. class DLLSPEC daeMetaAttribute : public daeRefCountedObj
  38. {
  39. protected:
  40. daeStringRef _name;
  41. daeInt _offset;
  42. daeAtomicType* _type;
  43. daeMetaElement* _container;
  44. std::string _defaultString;
  45. daeMemoryRef _defaultValue;
  46. daeBool _isRequired;
  47. public:
  48. /**
  49. * Constructor
  50. */
  51. daeMetaAttribute();
  52. /**
  53. * Destructor
  54. */
  55. virtual ~daeMetaAttribute();
  56. public:
  57. /**
  58. * Determines if the schema indicates that this is a required attribute.
  59. * @return Returns true if this is a required attribute, false if not.
  60. */
  61. daeBool getIsRequired() {return _isRequired; }
  62. /**
  63. * Sets the value that indicates that this attribute is required by the schema. If set, the attribute
  64. * will always be exported by the API regardless of its value.
  65. * @param isRequired Indicates if the schema says this attribute is required, true if it is, false if not.
  66. */
  67. void setIsRequired(daeBool isRequired) {_isRequired = isRequired;}
  68. /**
  69. * Sets the byte offset (from @c this) where this attribute's storage is
  70. * found in its container element class.
  71. * @param offset Integer byte offset from @c this pointer.
  72. */
  73. void setOffset(daeInt offset) { _offset = offset; }
  74. /**
  75. * Gets the byte offset (from @ this) where this attribute's storage is
  76. * found in its container element class.
  77. * @return Returns the integer byte offset from @c this pointer for this attribute.
  78. */
  79. daeInt getOffset() { return _offset; }
  80. /**
  81. * Sets the name of the attribute.
  82. * @param name @c daeString that is directly stored as a pointer
  83. * without being copied.
  84. */
  85. void setName(daeString name) { _name = name; }
  86. /**
  87. * Gets the name of this attribute.
  88. * @return Returnsthe name of this attribute.
  89. */
  90. daeStringRef getName() { return _name; }
  91. /**
  92. * Sets the type of the attribute.
  93. * @param type @c daeAtomicType to use for interacting with this
  94. * attribute in a containing @c daeElement.
  95. */
  96. void setType(daeAtomicType* type) { _type = type; }
  97. /**
  98. * Gets the @c daeAtomicType used by this attribute.
  99. * @return Returns the @c daeAtomicType that this attribute uses for its
  100. * implementation.
  101. */
  102. daeAtomicType* getType() { return _type; }
  103. /**
  104. * Sets the default for this attribute via a string.
  105. * @param defaultVal @c daeString representing the default value.
  106. */
  107. virtual void setDefaultString(daeString defaultVal);
  108. /**
  109. * Sets the default for this attribute via a memory pointer.
  110. * @param defaultVal @c daeMemoryRef representing the default value.
  111. */
  112. virtual void setDefaultValue(daeMemoryRef defaultVal);
  113. /**
  114. * Gets the default for this attribute as a string.
  115. * @return Returns a @c daeString representing the default value.
  116. */
  117. daeString getDefaultString();
  118. /**
  119. * Gets the default for this attribute as a memory value.
  120. * @return Returns a @c daeMemoryRef representing the default value.
  121. */
  122. daeMemoryRef getDefaultValue();
  123. /**
  124. * Sets the containing @c daeMetaElement for this attribute.
  125. * @param container Element on which this @c daeMetaAttribute belongs.
  126. */
  127. void setContainer(daeMetaElement* container) { _container = container; }
  128. /**
  129. * Gets the containing @c daeMetaElement for this attribute.
  130. * @return Returns the @c daeMetaElement to which this @c daeAttribute belongs.
  131. */
  132. daeMetaElement* getContainer() { return _container; }
  133. /**
  134. * Notifies an attribute when the containing document changes.
  135. */
  136. virtual void setDocument(daeElement* e, daeDocument* doc);
  137. /**
  138. * Converts an element's attribute value to a string.
  139. */
  140. virtual void memoryToString(daeElement* e, std::ostringstream& buffer);
  141. /**
  142. * Converts a string to a memory value in the specified element.
  143. */
  144. virtual void stringToMemory(daeElement* e, daeString s);
  145. /**
  146. * Gets the attribute's memory pointer from containing element <tt><i>e</i></tt>.
  147. * @param e Containing element from which to get the value.
  148. * @return Returns the memory pointer corresponding to this attribute out of parent element e.
  149. */
  150. virtual daeMemoryRef get(daeElement* e);
  151. /**
  152. * Gets if this attribute is an array attribute.
  153. * @return Returns true if this attribute is an array type.
  154. */
  155. virtual daeBool isArrayAttribute() { return false; }
  156. public:
  157. /**
  158. * Gets the number of bytes for this attribute.
  159. * @return Returns the number of bytes in the C++ COLLADA dom element for this
  160. * attribute.
  161. */
  162. virtual daeInt getSize();
  163. /**
  164. * Gets the alignment in bytes on the class of this meta attribute type.
  165. * @return Returns the alignment in bytes.
  166. */
  167. virtual daeInt getAlignment();
  168. /**
  169. * Copies the value of this attribute from fromElement into toElement.
  170. * @param toElement Pointer to a @c daeElement to copy this attribute to.
  171. * @param fromElement Pointer to a @c daeElement to copy this attribute from.
  172. */
  173. virtual void copy(daeElement* toElement, daeElement* fromElement);
  174. /**
  175. * Copies the default value of this attribute to the element
  176. * @param element Pointer to a @c daeElement to copy the default value to.
  177. */
  178. virtual void copyDefault(daeElement* element);
  179. /**
  180. * Compares the value of this attribute in the given elements.
  181. * @param elt1 The first element whose attribute value should be compared.
  182. * @param elt2 The second element whose attribute value should be compared.
  183. * @return Returns a positive integer if value1 > value2, a negative integer if
  184. * value1 < value2, and 0 if value1 == value2.
  185. */
  186. virtual daeInt compare(daeElement* elt1, daeElement* elt2);
  187. /**
  188. * Compares the value of this attribute from the given element to the default value
  189. * of this attribute (if one exists).
  190. * @param e The element whose value should be compared to the default value.
  191. * @return Returns a positive integer if value > default, a negative integer if
  192. * value < default, and 0 if value == default.
  193. */
  194. virtual daeInt compareToDefault(daeElement* e);
  195. public:
  196. // These methods are deprecated.
  197. virtual daeChar* getWritableMemory(daeElement* e); // Use get instead.
  198. virtual void set(daeElement* element, daeString s); // Use stringToMemory instead.
  199. };
  200. /**
  201. * The @c daeMetaArrayAttribute class is simple a wrapper that implements
  202. * an array of atomic types rather than a singleton.
  203. * The corresponding storage is an array
  204. * and the corresponding operations are implemented on the array
  205. * data structure rather than on inlined storage in elements.
  206. */
  207. class DLLSPEC daeMetaArrayAttribute : public daeMetaAttribute
  208. {
  209. public:
  210. virtual ~daeMetaArrayAttribute();
  211. /**
  212. * Defines the override version of this method from @c daeMetaAttribute.
  213. * @param toElement Pointer to a @c daeElement to copy this attribute to.
  214. * @param fromElement Pointer to a @c daeElement to copy this attribute from.
  215. */
  216. virtual void copy(daeElement* toElement, daeElement* fromElement);
  217. /**
  218. * Copies the default value of this attribute to the element
  219. * @param element Pointer to a @c daeElement to copy the default value to.
  220. */
  221. virtual void copyDefault(daeElement* element);
  222. /**
  223. * Compares the value of this attribute in the given elements.
  224. * @param elt1 The first element whose attribute value should be compared.
  225. * @param elt2 The second element whose attribute value should be compared.
  226. * @return Returns a positive integer if value1 > value2, a negative integer if
  227. * value1 < value2, and 0 if value1 == value2.
  228. */
  229. virtual daeInt compare(daeElement* elt1, daeElement* elt2);
  230. /**
  231. * Compares the value of this attribute from the given element to the default value
  232. * of this attribute (if one exists).
  233. * @param e The element whose value should be compared to the default value.
  234. * @return Returns a positive integer if value > default, a negative integer if
  235. * value < default, and 0 if value == default.
  236. */
  237. virtual daeInt compareToDefault(daeElement* e);
  238. /**
  239. * Converts an element's attribute value to a string.
  240. */
  241. virtual void memoryToString(daeElement* e, std::ostringstream& buffer);
  242. /**
  243. * Converts a string to a memory value in the specified element.
  244. */
  245. virtual void stringToMemory(daeElement* e, daeString s);
  246. /**
  247. * Sets the default for this attribute via a string.
  248. * @param defaultVal @c daeString representing the default value.
  249. */
  250. virtual void setDefaultString(daeString defaultVal);
  251. /**
  252. * Sets the default for this attribute via a memory pointer.
  253. * @param defaultVal @c daeMemoryRef representing the default value.
  254. */
  255. virtual void setDefaultValue(daeMemoryRef defaultVal);
  256. /**
  257. * Gets if this attribute is an array attribute.
  258. * @return Returns true if this attribute is an array type.
  259. */
  260. virtual daeBool isArrayAttribute() { return true; }
  261. /**
  262. * Notifies an attribute when the containing document changes.
  263. */
  264. virtual void setDocument(daeElement* e, daeDocument* doc);
  265. };
  266. typedef daeSmartRef<daeMetaAttribute> daeMetaAttributeRef;
  267. typedef daeTArray<daeMetaAttributeRef> daeMetaAttributeRefArray;
  268. typedef daeTArray<daeMetaAttribute*> daeMetaAttributePtrArray;
  269. #endif //__DAE_META_ATTRIBUTE_H__