daeMetaElement.h 14 KB

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