daeMetaAttribute.h 11 KB

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