daeMetaElement.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_ELEMENT_H__
  9. #define __DAE_META_ELEMENT_H__
  10. #include <dae/daeTypes.h>
  11. #include <dae/daeArrayTypes.h>
  12. #include <dae/daeElement.h>
  13. #include <dae/daeMetaAttribute.h>
  14. #include <dae/daeRefCountedObj.h>
  15. class DAE;
  16. class daeMetaCMPolicy;
  17. class daeMetaElementArrayAttribute;
  18. typedef daeElementRef (*daeElementConstructFunctionPtr)(DAE& dae);
  19. /**
  20. * Each instance of the @c daeMetaElement class describes a C++ COLLADA dom
  21. * element type.
  22. * @par
  23. * The meta information in @c daeMetaElement is a combination of the information
  24. * required to create and maintain C++ object instances and
  25. * the information necessary to parse and construct a hierarchy of COLLADA
  26. * elements.
  27. * @par
  28. * @c daeMetaElement objects also act as factories for C++ COLLADA dom classes where
  29. * each @c daeElement is capable of creating an instance of the class it describes.
  30. * Further, each @c daeMetaElement contains references to other @c daeMetaElements
  31. * for potential XML children elements. This enables this system to easily
  32. * create @c daeElements of the appropriate type while navigating through XML
  33. * recursive parse.
  34. * @par
  35. * See @c daeElement for information about the functionality that every @c daeElement implements.
  36. */
  37. class daeMetaElement : public daeRefCountedObj
  38. {
  39. protected:
  40. daeStringRef _name;
  41. daeElementConstructFunctionPtr _createFunc;
  42. daeInt _elementSize;
  43. daeMetaAttributeRefArray _metaAttributes;
  44. daeMetaAttributeRef _metaValue;
  45. daeMetaElementArrayAttribute* _metaContents;
  46. daeMetaArrayAttribute* _metaContentsOrder;
  47. daeMetaAttributeRef _metaID;
  48. daeBool _isTrackableForQueries;
  49. daeBool _usesStringContents;
  50. daeBool _isTransparent;
  51. daeBool _isAbstract;
  52. daeBool _allowsAny;
  53. daeBool _innerClass;
  54. daeMetaCMPolicy* _contentModel;
  55. daeMetaArrayAttribute* _metaCMData;
  56. daeUInt _numMetaChoices;
  57. DAE& dae;
  58. public:
  59. /**
  60. * Constructor
  61. */
  62. DLLSPEC daeMetaElement(DAE& dae);
  63. /**
  64. * Destructor
  65. */
  66. DLLSPEC ~daeMetaElement();
  67. public: // public accessors
  68. /**
  69. * Gets the DAE object that owns this daeMetaElement.
  70. * @return Returns the owning DAE.
  71. */
  72. DAE* getDAE();
  73. /**
  74. * Determines if elements of this type is an inner class.
  75. * @return Returns true if this element type is an inner class.
  76. */
  77. daeBool getIsInnerClass() { return _innerClass; }
  78. /**
  79. * Sets if elements of this type are inner classes.
  80. * @param abstract True if this type is an inner class.
  81. */
  82. void setIsInnerClass( daeBool ic ) { _innerClass = ic; }
  83. /**
  84. * Determines if elements of this type can be placed in the object model.
  85. * @return Returns true if this element type is abstract, false otherwise.
  86. */
  87. daeBool getIsAbstract() { return _isAbstract; }
  88. /**
  89. * Determines if elements of this type should have an element tag printed when saving.
  90. * @return Returns true if this element type should not have a tag, false otherwise.
  91. */
  92. daeBool getIsTransparent() { return _isTransparent; }
  93. /**
  94. * Sets if elements of this type are abstract.
  95. * @param abstract True if this type is abstract.
  96. */
  97. void setIsAbstract( daeBool abstract ) { _isAbstract = abstract; }
  98. /**
  99. * Sets whether or not elements of this type should have an element tag printed when saving.
  100. * @param transparent True if this type is transparent.
  101. */
  102. void setIsTransparent( daeBool transparent ) { _isTransparent = transparent; }
  103. /**
  104. * Determines if elements of this type should be tracked
  105. * for daeDatabase queries.
  106. * @return Returns true if this element type should be tracked
  107. */
  108. daeBool getIsTrackableForQueries() { return _isTrackableForQueries; }
  109. /**
  110. * Sets whether elements of this type should be tracked
  111. * for @c daeDatabase queries.
  112. * @param trackable Indicates whether this element should be tracked.
  113. * A value of true indicates this element type should be tracked and be available for
  114. * database queries.
  115. */
  116. void setIsTrackableForQueries(daeBool trackable) {
  117. _isTrackableForQueries = trackable; }
  118. /**
  119. * Determines if elements of this type allow for any element as a child.
  120. * @return Returns true if this element can have any child element, false otherwise.
  121. */
  122. daeBool getAllowsAny() { return _allowsAny; }
  123. /**
  124. * Sets if elements of this type allow for any element as a child.
  125. * @param allows True if this element allows for any child element, false otherwise.
  126. */
  127. void setAllowsAny( daeBool allows ) { _allowsAny = allows; }
  128. /**
  129. * Gets the @c daeMetaAttribute for the non-element contents of a @c daeElement.
  130. * This corresponds to a @c daeMetaFloatAttribute, @c daeMetaFloatArrayAttribute,
  131. * et cetera.
  132. * @return Returns the @c daeMetaAttribute pointer for the non-element contents of
  133. * this element type.
  134. */
  135. daeMetaAttribute* getValueAttribute() { return _metaValue; }
  136. /**
  137. * Gets the @c daeMetaAttribute for the ID attribute of a @c daeElement.
  138. * @return Returns the ID @c daeMetaAttribute, or NULL if the element type
  139. * does not have an ID attribute.
  140. */
  141. daeMetaAttribute* getIDAttribute() { return _metaID; }
  142. /**
  143. * Gets the name of this element type.
  144. * @return Returns the name of this element type.
  145. */
  146. daeStringRef getName() { return _name; }
  147. /**
  148. * Sets the name of this element type.
  149. * @param s String name to set.
  150. */
  151. void setName(daeString s) { _name = s; }
  152. /**
  153. * Gets the array of all known attributes on this element type.
  154. * This includes all meta attributes except those describing child
  155. * elements. It does include the value element.
  156. * @return Returns the array of @c daeMetaAttributeRefs.
  157. */
  158. daeMetaAttributeRefArray& getMetaAttributes() {
  159. return _metaAttributes; }
  160. /**
  161. * Gets the attribute which has a name as provided by the <tt><i>s</i></tt> parameter.
  162. * @param s String containing the desired attribute's name.
  163. * @return Returns the corresponding @c daeMetaAttribute, or NULL if none found.
  164. */
  165. DLLSPEC daeMetaAttribute* getMetaAttribute(daeString s);
  166. /**
  167. * Sets the size in bytes of each instance of this element type.
  168. * Used for factory element creation.
  169. * @param size Number of bytes for each C++ element instance.
  170. */
  171. void setElementSize(daeInt size) {_elementSize = size;}
  172. /**
  173. * Gets the size in bytes of each instance of this element type.
  174. * Used for factory element creation.
  175. * @return Returns the number of bytes for each C++ element instance.
  176. */
  177. daeInt getElementSize() { return _elementSize;}
  178. public:
  179. /**
  180. * Registers with the reflective object system that the dom class described by this @c daeMetaElement
  181. * contains a <tt><i>_contents</i></tt> array. This method is @em only for @c daeMetaElement contstuction, and
  182. * should only be called by the system as it sets up the Reflective Object System.
  183. * @param offset Byte offset for the contents field in the C++ element class.
  184. */
  185. DLLSPEC void addContents(daeInt offset);
  186. /**
  187. * Registers with the reflective object system the array that stores the _contents ordering. This method is @em
  188. * only for @c daeMetaElement contstuction, and should only be called by the system as it sets up the Reflective
  189. * Object System.
  190. * @param offset Byte offset for the contents order array in the C++ element class.
  191. */
  192. DLLSPEC void addContentsOrder( daeInt offset );
  193. /**
  194. * Registers with the reflective object system that the dom class described by this @c daeMetaElement
  195. * contains at least one choice group in the content model for this element. This method is @em only
  196. * for @c daeMetaElement contstuction, and should only be called by the system as it sets up the
  197. * Reflective Object System.
  198. * @param offset Byte offset for the contents field in the C++ element class.
  199. * @param numChoices The number of choice content model blocks there are for this element type.
  200. */
  201. DLLSPEC void addCMDataArray( daeInt offset, daeUInt numChoices );
  202. /**
  203. * Gets the attribute associated with the contents meta information.
  204. * @see @c addContents()
  205. * @return Returns the @c daeMetaElementArrayAttribute.
  206. */
  207. daeMetaElementArrayAttribute* getContents() { return _metaContents; }
  208. /**
  209. * Gets the attribute associated with the CMData array meta information.
  210. * @see @c addCMDataArray()
  211. * @return Returns the @c daeMetaArrayAttribute for the CMData of an element.
  212. */
  213. daeMetaArrayAttribute* getMetaCMData() { return _metaCMData; }
  214. /**
  215. * Gets the number of choice content model blocks there are for this element type.
  216. * @return Returns the number of daeMetaChoice's there are in the content model.
  217. */
  218. daeUInt getNumChoices() const { return _numMetaChoices; }
  219. /**
  220. * Appends a @c daeMetaAttribute that represents a field corresponding to an
  221. * XML attribute to the C++ version of this element type.
  222. * @param attr Attribute to append to this element types list
  223. * of potential attributes.
  224. */
  225. DLLSPEC void appendAttribute(daeMetaAttribute* attr);
  226. /**
  227. * Registers the function that can construct a C++ instance of this class.
  228. * Necessary for the factory system such that C++ can still call @c new and the
  229. * @c vptr will still be initialized even when constructed via the factory system.
  230. * @param func Pointer to a function that does object construction.
  231. */
  232. void registerClass(daeElementConstructFunctionPtr func) {
  233. _createFunc = func; }
  234. /**
  235. * Validates this class to be used by the runtime c++ object model
  236. * including factory creation.
  237. */
  238. DLLSPEC void validate();
  239. /**
  240. * Places a child element into the <tt><i>parent</i></tt> element where the
  241. * calling object is the @c daeMetaElement for the parent element.
  242. * @param parent Element to act as the container.
  243. * @param child Child element to place in the parent.
  244. * @return Returns true if the operation was successful, false otherwise.
  245. */
  246. DLLSPEC daeBool place(daeElement *parent, daeElement *child, daeUInt *ordinal = NULL);
  247. /**
  248. * Places a child element into the <tt><i>parent</i></tt> element at a specific location
  249. * where the calling object is the @c daeMetaElement for the parent element.
  250. * @param index The location in the contents array to insert.
  251. * @param parent Element to act as the container.
  252. * @param child Child element to place in the parent.
  253. * @return Returns true if the operation was successful, false otherwise.
  254. * @note This should only be called on elements that have a _contents array. Elements without
  255. * a _contents array will be placed normally.
  256. */
  257. DLLSPEC daeBool placeAt( daeInt index, daeElement *parent, daeElement *child );
  258. /**
  259. * Places a child element into the <tt><i>parent</i></tt> element at a specific location which is right
  260. * before the marker element.
  261. * @param marker The element location in the contents array to insert before.
  262. * @param parent Element to act as the container.
  263. * @param child Child element to place in the parent.
  264. * @return Returns true if the operation was successful, false otherwise.
  265. */
  266. DLLSPEC daeBool placeBefore( daeElement* marker, daeElement *parent, daeElement *child, daeUInt *ordinal = NULL );
  267. /**
  268. * Places a child element into the <tt><i>parent</i></tt> element at a specific location which is right
  269. * after the marker element.
  270. * @param marker The element location in the contents array to insert after.
  271. * @param parent Element to act as the container.
  272. * @param child Child element to place in the parent.
  273. * @return Returns true if the operation was successful, false otherwise.
  274. */
  275. DLLSPEC daeBool placeAfter( daeElement* marker, daeElement *parent, daeElement *child, daeUInt *ordinal = NULL );
  276. /**
  277. * Removes a child element from its parent element.
  278. * @param parent Element That is the parent.
  279. * @param child Child element to remove.
  280. * @return Returns true if the operation was successful, false otherwise.
  281. */
  282. DLLSPEC daeBool remove( daeElement *parent, daeElement *child );
  283. /**
  284. * Gets all of the children from an element of this type.
  285. * @param parent The element that you want to get the children from.
  286. * @param array The return value. An elementref array to append this element's children to.
  287. */
  288. DLLSPEC void getChildren( daeElement* parent, daeElementRefArray &array );
  289. /**
  290. * Invokes the factory element creation routine set by @c registerConstructor()
  291. * to return a C++ COLLADA Object Model instance of this element type.
  292. * @return Returns a created @c daeElement of appropriate type via the
  293. * object creation function and the <tt> daeElement::setup() </tt> function.
  294. */
  295. DLLSPEC daeElementRef create();
  296. /**
  297. * Looks through the list of potential child elements
  298. * for this element type finding the corresponding element type; if a corresponding element type
  299. * is found, use that type as a factory and return an instance of that
  300. * child type. Typically @c place() is called after @c create(childelementname)
  301. * @param childElementTypeName Type name to create.
  302. * @return Returns the created element if the type was found as a potential child element.
  303. */
  304. DLLSPEC daeElementRef create(daeString childElementTypeName);
  305. /**
  306. * Gets the root of the content model policy tree.
  307. * @return Returns the root element of the tree of content model policy elements.
  308. */
  309. daeMetaCMPolicy *getCMRoot() { return _contentModel; }
  310. /**
  311. * Sets the root of the content model policy tree.
  312. * @param cm The root element of the tree of content model policy elements.
  313. */
  314. DLLSPEC void setCMRoot( daeMetaCMPolicy *cm );
  315. };
  316. typedef daeSmartRef<daeMetaElement> daeMetaElementRef;
  317. typedef daeTArray<daeMetaElementRef> daeMetaElementRefArray;
  318. #endif //__DAE_META_ELEMENT_H__