dae.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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__
  14. #define __DAE__
  15. // Need to include <memory> to use std::auto_ptr with gcc-4.3 - Andrew Galante, GG 8/2/2009
  16. #include <memory>
  17. #include <dae/daeTypes.h>
  18. #include <dae/daeError.h>
  19. #include <dae/daeDatabase.h>
  20. #include <dae/daeIOPlugin.h>
  21. #include <dae/daeAtomicType.h>
  22. #include <dae/daeMetaElement.h>
  23. #include <dae/daeIDRef.h>
  24. #include <dae/daeURI.h>
  25. #include <dae/daeUtils.h>
  26. #include <dae/daeRawResolver.h>
  27. #include <dae/daeSIDResolver.h>
  28. class domCOLLADA;
  29. typedef daeSmartRef<domCOLLADA> domCOLLADARef;
  30. class daeDatabase;
  31. // The DAE class is the core interface via which you interact with the DOM. It
  32. // has methods to load/save documents, get the root element of each document,
  33. // etc. Although internally the DOM works exclusively with URIs, the methods of
  34. // the DAE class that take document paths can take URIs or OS-specific file
  35. // paths.
  36. class DLLSPEC DAE
  37. {
  38. public:
  39. // Constructor. If no database or IO plugin are provided, a default database and
  40. // IO plugin will be used.
  41. DAE(daeDatabase* database = NULL, daeIOPlugin* ioPlugin = NULL)
  42. : atomicTypes(*this),
  43. baseUri(*this, cdom::getCurrentDirAsUri().c_str())
  44. {
  45. // See the end of the thread linked below for an explanation of why we have the DAE
  46. // constructor set up this way. Basically, I'm going to be changing the build output
  47. // location, and when this happens people sometimes continue to link against the old
  48. // libraries by accident (e.g. if they just do an svn update). By introducing a new
  49. // function that gets called from a function in a header file, I'm ensuring that someone
  50. // who tries linking against old libraries will get a link error. This may not sound
  51. // very nice, but it's certainly better than getting bizarre runtime crashes.
  52. // https://collada.org/public_forum/viewtopic.php?t=771&sid=f13c34f2d17ca720c5021bccbe5128b7
  53. init(database, ioPlugin);
  54. dummyFunction1();
  55. }
  56. virtual ~DAE();
  57. // Release all memory used by the DOM. You never need to call this explicitly. It's
  58. // called automatically when all DAE objects go out of scope.
  59. static void cleanup();
  60. public:
  61. // Database setup
  62. virtual daeDatabase* getDatabase();
  63. virtual daeInt setDatabase(daeDatabase* database);
  64. // IO Plugin setup
  65. virtual daeIOPlugin* getIOPlugin();
  66. virtual daeInt setIOPlugin(daeIOPlugin* plugin);
  67. // Creates a new document, returning null on failure.
  68. virtual domCOLLADA* add(const std::string& path);
  69. // Opens an existing document, returning null on failure.
  70. virtual domCOLLADA* open(const std::string& path);
  71. // Opens a document from memory, returning null on failure.
  72. virtual domCOLLADA* openFromMemory(const std::string& path, daeString buffer);
  73. // Write a document to the path specified by the document's URI, returning false on failure.
  74. virtual bool write(const std::string& path);
  75. // Write a document to the path specified in the second parameter, returning false on failure.
  76. virtual bool writeTo(const std::string& docPath, const std::string& pathToWriteTo);
  77. // Writes all documents, returning false if any document failed to write.
  78. virtual bool writeAll();
  79. // Close a specific document, unloading all memory used by the document. Returns false on failure.
  80. virtual void close(const std::string& path);
  81. // Remove all loaded documents. Always returns DAE_OK.
  82. virtual daeInt clear();
  83. // Returns the total number of documents.
  84. virtual int getDocCount();
  85. // Returns the i'th document .
  86. virtual daeDocument* getDoc(int i);
  87. // Returns a document matching the path.
  88. virtual daeDocument* getDoc(const std::string& path);
  89. // Get the root domCOLLADA object corresponding to a particular document.
  90. virtual domCOLLADA* getRoot(const std::string& path);
  91. // Set the root domCOLLADA object corresponding to a particular document, returning false on failure.
  92. virtual bool setRoot(const std::string& path, domCOLLADA* root);
  93. // Returns the Collada version, i.e. 1.4, 1.5, etc. Note that this _isn't_ the
  94. // same as the DOM version (1.3, 2.0, ...).
  95. virtual daeString getDomVersion();
  96. // Returns the (modifiable) list of atomic type objects.
  97. daeAtomicTypeList& getAtomicTypes();
  98. // Get/set a daeMetaElement object given the meta object's type ID.
  99. daeMetaElement* getMeta(daeInt typeID);
  100. void setMeta(daeInt typeID, daeMetaElement& meta);
  101. // Get all daeMetaElement objects.
  102. daeMetaElementRefArray& getAllMetas();
  103. // Returns the list of URI resolvers. You can modify the list to add new resolvers.
  104. daeURIResolverList& getURIResolvers();
  105. // The base URI used for resolving relative URI references.
  106. daeURI& getBaseURI();
  107. void setBaseURI(const daeURI& uri);
  108. void setBaseURI(const std::string& uri);
  109. // Returns the list of ID reference resolvers. You can modify the list to add new
  110. // resolvers.
  111. daeIDRefResolverList& getIDRefResolvers();
  112. // Meant for internal DOM use only.
  113. daeRawRefCache& getRawRefCache();
  114. daeSidRefCache& getSidRefCache();
  115. // These functions specify the client's character encoding for the DOM. The
  116. // default is Utf8, but if you specify Latin1 then the DOM will use libxml's
  117. // character conversion functions to convert to Utf8 when writing data and
  118. // convert to Latin1 when reading data. This can help with the handling of
  119. // non-ASCII characters on Windows. Only when using libxml for xml I/O does
  120. // any character conversion occur.
  121. //
  122. // Most people can probably just ignore this completely. If you have trouble
  123. // with non-ASCII characters on Windows, try setting the char encoding to
  124. // Latin1 to see if that helps.
  125. //
  126. // Frankly this certainly isn't the best way of handling non-ASCII character
  127. // support on Windows, so this interface is a likely target for significant
  128. // changes in the future.
  129. //
  130. // See this Sourceforge thread for more info:
  131. // http://sourceforge.net/tracker/index.php?func=detail&aid=1818473&group_id=157838&atid=805426
  132. //
  133. enum charEncoding {
  134. Utf8,
  135. Latin1
  136. };
  137. // Global encoding setting. Defaults to Utf8. Set this if you want to make a
  138. // char encoding change and apply it to all DAE objects.
  139. static charEncoding getGlobalCharEncoding();
  140. static void setGlobalCharEncoding(charEncoding encoding);
  141. // Local encoding setting. If set, overrides the global setting. Useful for setting
  142. // a specific char encoding for a single DAE object but not for all DAE objects.
  143. charEncoding getCharEncoding();
  144. void setCharEncoding(charEncoding encoding);
  145. // Deprecated. Alternative methods are given.
  146. virtual daeInt load(daeString uri, daeString docBuffer = NULL); // Use open
  147. virtual daeInt save(daeString uri, daeBool replace=true); // Use write
  148. virtual daeInt save(daeUInt documentIndex, daeBool replace=true); // Use write
  149. virtual daeInt saveAs(daeString uriToSaveTo, daeString docUri, daeBool replace=true); // Use writeTo
  150. virtual daeInt saveAs(daeString uriToSaveTo, daeUInt documentIndex=0, daeBool replace=true); // Use writeTo
  151. virtual daeInt unload(daeString uri); // Use close
  152. virtual domCOLLADA* getDom(daeString uri); // use getRoot
  153. virtual daeInt setDom(daeString uri, domCOLLADA* dom); // use setRoot
  154. private:
  155. void init(daeDatabase* database, daeIOPlugin* ioPlugin);
  156. void dummyFunction1();
  157. std::string makeFullUri(const std::string& path);
  158. domCOLLADA* openCommon(const std::string& path, daeString buffer);
  159. bool writeCommon(const std::string& docPath, const std::string& pathToWriteTo, bool replace);
  160. daeDatabase *database;
  161. daeIOPlugin *plugin;
  162. bool defaultDatabase;
  163. bool defaultPlugin;
  164. daeAtomicTypeList atomicTypes;
  165. daeMetaElementRefArray metas;
  166. daeURI baseUri;
  167. daeURIResolverList uriResolvers;
  168. daeIDRefResolverList idRefResolvers;
  169. daeRawRefCache rawRefCache;
  170. daeSidRefCache sidRefCache;
  171. std::unique_ptr<charEncoding> localCharEncoding;
  172. static charEncoding globalCharEncoding;
  173. };
  174. template <typename T>
  175. inline T *daeSafeCast(daeElement *element)
  176. {
  177. if (element && element->typeID() == T::ID())
  178. return (T*)element;
  179. return NULL;
  180. }
  181. #endif // __DAE_INTERFACE__