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