daeDatabase.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_DATABASE__
  9. #define __DAE_DATABASE__
  10. #include <memory>
  11. #include <vector>
  12. #include <dae.h>
  13. #include <dae/daeTypes.h>
  14. #include <dae/daeElement.h>
  15. #include <dae/daeURI.h>
  16. #include <dae/daeDocument.h>
  17. /**
  18. * The @c daeDatabase class defines the COLLADA runtime database interface.
  19. */
  20. class DLLSPEC daeDatabase
  21. {
  22. public:
  23. /**
  24. * Constructor
  25. */
  26. daeDatabase(DAE& dae);
  27. /**
  28. * Destructor
  29. */
  30. virtual ~daeDatabase() {}
  31. /**
  32. * Get the associated DAE object.
  33. * @return The associated DAE object.
  34. */
  35. virtual DAE* getDAE();
  36. /**
  37. * Creates a new document, defining its root as the <tt><i>dom</i></tt> object; returns an error if the document name already exists.
  38. * @param name Name of the new document, must be a valid URI.
  39. * @param dom Existing @c domCOLLADA root element of the document
  40. * @param document Pointer to a @c daeDocument pointer that receives the document created
  41. * @param zaeRootDocument Indicates if the new document is the root document of a ZAE archive.
  42. * @param extractedFileURI URI to extracted dae file.
  43. * @return Returns @c DAE_OK if the document was created successfully, otherwise returns a negative value as defined in daeError.h.
  44. * @note The @c daeElement passed in as <tt><i>dom</i></tt> should always be a @c domCOLLADA object, the API may enforce this in the future.
  45. * @deprecated This function will be removed in future versions. Please use createDocument.
  46. */
  47. virtual daeInt insertDocument(daeString name, daeElement* dom, daeDocument** document = NULL, bool zaeRootDocument = false, const std::string& extractedFileURI = "") = 0;
  48. /**
  49. * Creates a new @c domCOLLADA root element and a new document; returns an error if the document name already exists.
  50. * @param name Name of the new document, must be a valid URI.
  51. * @param document Pointer to a @c daeDocument pointer that receives the document created
  52. * @return Returns DAE_OK if the document was created successfully, otherwise returns a negative value as defined in daeError.h.
  53. * @deprecated This function will be removed in future versions. Please use createDocument.
  54. */
  55. virtual daeInt insertDocument(daeString name, daeDocument** document = NULL) = 0;
  56. /**
  57. * Creates a new document, defining its root as the <tt><i>dom</i></tt> object; returns an error if the document name already exists.
  58. * @param name Name of the new document, must be a valid URI.
  59. * @param dom Existing @c domCOLLADA root element of the document
  60. * @param document Pointer to a @c daeDocument pointer that receives the document created
  61. * @param zaeRootDocument Indicates if the new document is the root document of a ZAE archive.
  62. * @param extractedFileURI URI to extracted dae file.
  63. * @return Returns @c DAE_OK if the document was created successfully, otherwise returns a negative value as defined in daeError.h.
  64. * @note The @c daeElement passed in as <tt><i>dom</i></tt> should always be a @c domCOLLADA object, the API may enforce this in the future.
  65. */
  66. virtual daeInt createDocument(daeString name, daeElement* dom, daeDocument** document = NULL, bool zaeRootDocument = false, const std::string& extractedFileURI = "") = 0;
  67. /**
  68. * Creates a new @c domCOLLADA root element and a new document; returns an error if the document name already exists.
  69. * @param name Name of the new document, must be a valid URI.
  70. * @param document Pointer to a @c daeDocument pointer that receives the document created
  71. * @return Returns DAE_OK if the document was created successfully, otherwise returns a negative value as defined in daeError.h.
  72. */
  73. virtual daeInt createDocument(daeString name, daeDocument** document = NULL) = 0;
  74. /**
  75. * Inserts an already existing document into the database.
  76. * @param c The document to insert.
  77. * @return Returns DAE_OK if the document was inserted successfully, otherwise returns a negative value as defined in daeError.h.
  78. */
  79. virtual daeInt insertDocument( daeDocument *c ) = 0;
  80. /**
  81. * Removes a document from the database.
  82. * @param document Document to remove from the database
  83. * @return Returns DAE_OK if the document was successfully removed, otherwise returns a negative value as defined in daeError.h.
  84. */
  85. virtual daeInt removeDocument(daeDocument* document) = 0;
  86. /**
  87. * Gets the number of documents.
  88. * @return Returns the number of documents.
  89. */
  90. virtual daeUInt getDocumentCount() = 0;
  91. /**
  92. * Gets a document based on the document index.
  93. * @param index Index of the document to get.
  94. * @return Returns a pointer on the document, or NULL if not found.
  95. */
  96. virtual daeDocument* getDocument(daeUInt index) = 0;
  97. /**
  98. * Gets a document based on the document index.
  99. * @param index Index of the document to get.
  100. * @return Returns a pointer on the document, or NULL if not found.
  101. */
  102. daeDocument* getDoc(daeUInt index);
  103. /**
  104. * Gets a document based on the document name.
  105. * @param name The name of the document as a URI.
  106. * @param skipUriNormalization Use the document name as is; don't normalize it first.
  107. * This is mostly for improved performance.
  108. * @return Returns a pointer to the document, or NULL if not found.
  109. * @note If the URI contains a fragment, the fragment is stripped off.
  110. */
  111. virtual daeDocument* getDocument(daeString name, bool skipUriNormalization = false) = 0;
  112. /**
  113. * Gets a document name.
  114. * @param index Index of the document to get.
  115. * @return Returns the name of the document at the given index.
  116. */
  117. virtual daeString getDocumentName(daeUInt index) = 0;
  118. /**
  119. * Indicates if a document is loaded or not.
  120. * @param name Name of the document as a URI.
  121. * @return Returns true if the document is loaded, false otherwise.
  122. * @note If the URI contains a fragment, the fragment is stripped off.
  123. */
  124. virtual daeBool isDocumentLoaded(daeString name) = 0;
  125. /**
  126. * Inserts a @c daeElement into the runtime database.
  127. * @param document Document in which the @c daeElement lives.
  128. * @param element @c daeElement to insert in the database
  129. * @return Returns @c DAE_OK if element successfully inserted, otherwise returns a negative value as defined in daeError.h.
  130. */
  131. virtual daeInt insertElement(daeDocument* document,
  132. daeElement* element) = 0;
  133. /**
  134. * Removes a @c daeElement from the runtime database; not implemented in the reference STL implementation.
  135. * @param document Document in which the @c daeElement lives.
  136. * @param element Element to remove.
  137. * @return Returns @c DAE_OK if element successfully removed, otherwise returns a negative value as defined in daeError.h.
  138. */
  139. virtual daeInt removeElement(daeDocument* document,
  140. daeElement* element) = 0;
  141. /**
  142. * Updates the database to reflect a change to the ID of a @c daeElement.
  143. * @param element @c daeElement whose ID is going to change.
  144. * @param newID The ID that will be assigned to the element.
  145. * @return Returns @c DAE_OK if the database was successfully updated, otherwise returns a negative value as defined in daeError.h.
  146. * @note The database doesn't actually change the ID of the element, it
  147. * merely updates its internal structures to reflect the change. It's
  148. * expected that the ID will be assigned to the element by someone else.
  149. */
  150. virtual daeInt changeElementID(daeElement* element,
  151. daeString newID) = 0;
  152. /**
  153. * Updates the database to reflect a change to the sid of a @c daeElement.
  154. * @param element @c daeElement whose sid is going to change.
  155. * @param newSID The sid that will be assigned to the element.
  156. * @return Returns @c DAE_OK if the database was successfully updated, otherwise returns a negative value as defined in daeError.h.
  157. * @note The database doesn't actually change the sid of the element, it
  158. * merely updates its internal structures to reflect the change. It's
  159. * expected that the sid will be assigned to the element by someone else.
  160. * Note - This function currently isn't implemented in the default database.
  161. */
  162. virtual daeInt changeElementSID(daeElement* element,
  163. daeString newSID) = 0;
  164. /**
  165. * Unloads all of the documents of the runtime database.
  166. * This function frees all the @c dom* objects created so far,
  167. * except any objects on which you still have a smart pointer reference (@c daeSmartRef).
  168. * @return Returns @c DAE_OK if all documents successfully unloaded, otherwise returns a negative value as defined in daeError.h.
  169. */
  170. virtual daeInt clear() = 0;
  171. /**
  172. * Lookup elements by ID, searching through all documents.
  173. * @param id The ID to match on.
  174. * @return The array of matching elements.
  175. */
  176. virtual std::vector<daeElement*> idLookup(const std::string& id) = 0;
  177. /**
  178. * Find an element with the given ID in a specific document.
  179. * @param id The ID to match on.
  180. * @param doc The document to search in.
  181. * @return The matching element if one is found, NULL otherwise.
  182. */
  183. daeElement* idLookup(const std::string& id, daeDocument* doc);
  184. /**
  185. * Lookup elements by type ID.
  186. * @param typeID The type to match on, e.g. domNode::ID().
  187. * @param doc The document to search in, or NULL to search in all documents.
  188. * @return The array of matching elements.
  189. */
  190. std::vector<daeElement*> typeLookup(daeInt typeID, daeDocument* doc = NULL);
  191. /**
  192. * Same as the previous method, but returns the array of matching elements via a
  193. * reference parameter for additional efficiency.
  194. * @param typeID The type to match on, e.g. domNode::ID().
  195. * @param matchingElements The array of matching elements.
  196. * @param doc The document to search in, or NULL to search in all documents.
  197. */
  198. virtual void typeLookup(daeInt typeID,
  199. std::vector<daeElement*>& matchingElements,
  200. daeDocument* doc = NULL) = 0;
  201. /**
  202. * Lookup elements by type ID.
  203. * @param doc The document to search in, or NULL to search in all documents.
  204. * @return The array of matching elements.
  205. */
  206. template<typename T>
  207. std::vector<T*> typeLookup(daeDocument* doc = NULL) {
  208. std::vector<T*> result;
  209. typeLookup(result, doc);
  210. return result;
  211. }
  212. /**
  213. * Same as the previous method, but returns the array of matching elements via a
  214. * reference parameter for additional efficiency.
  215. * @param matchingElements The array of matching elements.
  216. * @param doc The document to search in, or NULL to search in all documents.
  217. */
  218. template<typename T> void
  219. typeLookup(std::vector<T*>& matchingElements, daeDocument* doc = NULL) {
  220. std::vector<daeElement*> elts;
  221. typeLookup(T::ID(), elts, doc);
  222. matchingElements.clear();
  223. matchingElements.reserve(elts.size());
  224. for (size_t i = 0; i < elts.size(); i++)
  225. matchingElements.push_back((T*)elts[i]);
  226. }
  227. /**
  228. * Lookup elements by sid.
  229. * @param sid The sid to match on.
  230. * @param doc The document to search in, or NULL to search in all documents.
  231. * @return The array of matching elements.
  232. * Note - This function currently isn't implemented in the default database.
  233. */
  234. std::vector<daeElement*> sidLookup(const std::string& sid, daeDocument* doc = NULL);
  235. /**
  236. * Same as the previous method, but the results are returned via a parameter instead
  237. * of a return value, for extra efficiency.
  238. * @param sid The sid to match on.
  239. * @param matchingElements The array of matching elements.
  240. * @param doc The document to search in, or NULL to search in all documents.
  241. * Note - This function currently isn't implemented in the default database.
  242. */
  243. virtual void sidLookup(const std::string& sid,
  244. std::vector<daeElement*>& matchingElements,
  245. daeDocument* doc = NULL) = 0;
  246. /**
  247. * Sets the top meta object.
  248. * Called by @c dae::setDatabase() when the database changes. It passes to this function the
  249. * top meta object, which is the root of a
  250. * hierarchy of @c daeMetaElement objects. This top meta object is capable of creating
  251. * any of the root objects in the DOM tree.
  252. * @param _topMeta Top meta object to use to create objects to fill the database.
  253. * @return Returns DAE_OK if successful, otherwise returns a negative value defined in daeError.h.
  254. */
  255. virtual daeInt setMeta(daeMetaElement *_topMeta) = 0;
  256. public:
  257. // The following methods are deprecated, and it's recommended that you don't use them.
  258. // Where appropriate, alternative methods are specified.
  259. virtual daeUInt getTypeCount() = 0;
  260. virtual daeString getTypeName(daeUInt index) = 0;
  261. // Instead of the following two methods, use idLookup or typeLookup.
  262. virtual daeUInt getElementCount(daeString name = NULL,
  263. daeString type = NULL,
  264. daeString file = NULL) = 0;
  265. virtual daeInt getElement(daeElement** pElement,
  266. daeInt index,
  267. daeString name = NULL,
  268. daeString type = NULL,
  269. daeString file = NULL ) = 0;
  270. inline daeInt insertCollection(daeString name, daeElement* dom, daeDocument** document = NULL) {
  271. return insertDocument( name, dom, document );
  272. }
  273. inline daeInt insertCollection(daeString name, daeDocument** document = NULL) {
  274. return insertDocument( name, document );
  275. }
  276. inline daeInt createCollection(daeString name, daeElement* dom, daeDocument** document = NULL) {
  277. return createDocument( name, dom, document );
  278. }
  279. inline daeInt createCollection(daeString name, daeDocument** document = NULL) {
  280. return createDocument( name, document );
  281. }
  282. inline daeInt insertCollection( daeDocument *c ) {
  283. return insertDocument( c );
  284. }
  285. inline daeInt removeCollection(daeDocument* document) {
  286. return removeDocument( document );
  287. }
  288. inline daeUInt getCollectionCount() {
  289. return getDocumentCount();
  290. }
  291. inline daeDocument* getCollection(daeUInt index) {
  292. return getDocument( index );
  293. }
  294. inline daeDocument* getCollection(daeString name) {
  295. return getDocument( name );
  296. }
  297. inline daeString getCollectionName(daeUInt index) {
  298. return getDocumentName( index );
  299. }
  300. inline daeBool isCollectionLoaded(daeString name) {
  301. return isDocumentLoaded( name );
  302. }
  303. protected:
  304. DAE& dae;
  305. };
  306. #endif //__DAE_DATABASE__