BlenderDNA.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file BlenderDNA.h
  34. * @brief Blender `DNA` (file format specification embedded in
  35. * blend file itself) loader.
  36. */
  37. #ifndef INCLUDED_AI_BLEND_DNA_H
  38. #define INCLUDED_AI_BLEND_DNA_H
  39. #include "BaseImporter.h"
  40. #include "TinyFormatter.h"
  41. // enable verbose log output. really verbose, so be careful.
  42. #ifdef _DEBUG
  43. # define ASSIMP_BUILD_BLENDER_DEBUG
  44. #endif
  45. // #define ASSIMP_BUILD_BLENDER_NO_STATS
  46. namespace Assimp {
  47. template <bool,bool> class StreamReader;
  48. typedef StreamReader<true,true> StreamReaderAny;
  49. namespace Blender {
  50. class FileDatabase;
  51. struct FileBlockHead;
  52. template <template <typename> class TOUT>
  53. class ObjectCache;
  54. // -------------------------------------------------------------------------------
  55. /** Exception class used by the blender loader to selectively catch exceptions
  56. * thrown in its own code (DeadlyImportErrors thrown in general utility
  57. * functions are untouched then). If such an exception is not caught by
  58. * the loader itself, it will still be caught by Assimp due to its
  59. * ancestry. */
  60. // -------------------------------------------------------------------------------
  61. struct Error : DeadlyImportError
  62. {
  63. Error (const std::string& s)
  64. : DeadlyImportError(s)
  65. {}
  66. };
  67. // -------------------------------------------------------------------------------
  68. /** The only purpose of this structure is to feed a virtual dtor into its
  69. * descendents. It serves as base class for all data structure fields. */
  70. // -------------------------------------------------------------------------------
  71. struct ElemBase
  72. {
  73. virtual ~ElemBase() {}
  74. /** Type name of the element. The type
  75. * string points is the `c_str` of the `name` attribute of the
  76. * corresponding `Structure`, that is, it is only valid as long
  77. * as the DNA is not modified. The dna_type is only set if the
  78. * data type is not static, i.e. a boost::shared_ptr<ElemBase>
  79. * in the scene description would have its type resolved
  80. * at runtime, so this member is always set. */
  81. const char* dna_type;
  82. };
  83. // -------------------------------------------------------------------------------
  84. /** Represents a generic pointer to a memory location, which can be either 32
  85. * or 64 bits. These pointers are loaded from the BLEND file and finally
  86. * fixed to point to the real, converted representation of the objects
  87. * they used to point to.*/
  88. // -------------------------------------------------------------------------------
  89. struct Pointer
  90. {
  91. Pointer() : val() {}
  92. uint64_t val;
  93. };
  94. // -------------------------------------------------------------------------------
  95. /** Represents a generic offset within a BLEND file */
  96. // -------------------------------------------------------------------------------
  97. struct FileOffset
  98. {
  99. FileOffset() : val() {}
  100. uint64_t val;
  101. };
  102. // -------------------------------------------------------------------------------
  103. /** Dummy derivate of std::vector to be able to use it in templates simultaenously
  104. * with boost::shared_ptr, which takes only one template argument
  105. * while std::vector takes three. Also we need to provide some special member
  106. * functions of shared_ptr */
  107. // -------------------------------------------------------------------------------
  108. template <typename T>
  109. class vector : public std::vector<T>
  110. {
  111. public:
  112. using std::vector<T>::resize;
  113. using std::vector<T>::empty;
  114. void reset() {
  115. resize(0);
  116. }
  117. operator bool () const {
  118. return !empty();
  119. }
  120. };
  121. // -------------------------------------------------------------------------------
  122. /** Mixed flags for use in #Field */
  123. // -------------------------------------------------------------------------------
  124. enum FieldFlags
  125. {
  126. FieldFlag_Pointer = 0x1,
  127. FieldFlag_Array = 0x2
  128. };
  129. // -------------------------------------------------------------------------------
  130. /** Represents a single member of a data structure in a BLEND file */
  131. // -------------------------------------------------------------------------------
  132. struct Field
  133. {
  134. std::string name;
  135. std::string type;
  136. size_t size;
  137. size_t offset;
  138. /** Size of each array dimension. For flat arrays,
  139. * the second dimension is set to 1. */
  140. size_t array_sizes[2];
  141. /** Any of the #FieldFlags enumerated values */
  142. unsigned int flags;
  143. };
  144. // -------------------------------------------------------------------------------
  145. /** Range of possible behaviours for fields absend in the input file. Some are
  146. * mission critical so we need them, while others can silently be default
  147. * initialized and no animations are harmed. */
  148. // -------------------------------------------------------------------------------
  149. enum ErrorPolicy
  150. {
  151. /** Substitute default value and ignore */
  152. ErrorPolicy_Igno,
  153. /** Substitute default value and write to log */
  154. ErrorPolicy_Warn,
  155. /** Substitute a massive error message and crash the whole matrix. Its time for another zion */
  156. ErrorPolicy_Fail
  157. };
  158. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  159. # define ErrorPolicy_Igno ErrorPolicy_Warn
  160. #endif
  161. // -------------------------------------------------------------------------------
  162. /** Represents a data structure in a BLEND file. A Structure defines n fields
  163. * and their locatios and encodings the input stream. Usually, every
  164. * Structure instance pertains to one equally-named data structure in the
  165. * BlenderScene.h header. This class defines various utilities to map a
  166. * binary `blob` read from the file to such a structure instance with
  167. * meaningful contents. */
  168. // -------------------------------------------------------------------------------
  169. class Structure
  170. {
  171. template <template <typename> class> friend class ObjectCache;
  172. public:
  173. Structure()
  174. : cache_idx(-1)
  175. {}
  176. public:
  177. // publicly accessible members
  178. std::string name;
  179. vector< Field > fields;
  180. std::map<std::string, size_t> indices;
  181. size_t size;
  182. public:
  183. // --------------------------------------------------------
  184. /** Access a field of the structure by its canonical name. The pointer version
  185. * returns NULL on failure while the reference version raises an import error. */
  186. inline const Field& operator [] (const std::string& ss) const;
  187. inline const Field* Get (const std::string& ss) const;
  188. // --------------------------------------------------------
  189. /** Access a field of the structure by its index */
  190. inline const Field& operator [] (const size_t i) const;
  191. // --------------------------------------------------------
  192. inline bool operator== (const Structure& other) const {
  193. return name == other.name; // name is meant to be an unique identifier
  194. }
  195. // --------------------------------------------------------
  196. inline bool operator!= (const Structure& other) const {
  197. return name != other.name;
  198. }
  199. public:
  200. // --------------------------------------------------------
  201. /** Try to read an instance of the structure from the stream
  202. * and attempt to convert to `T`. This is done by
  203. * an appropriate specialization. If none is available,
  204. * a compiler complain is the result.
  205. * @param dest Destination value to be written
  206. * @param db File database, including input stream. */
  207. template <typename T> inline void Convert (T& dest,
  208. const FileDatabase& db) const;
  209. // --------------------------------------------------------
  210. // generic converter
  211. template <typename T>
  212. void Convert(boost::shared_ptr<ElemBase> in,const FileDatabase& db) const;
  213. // --------------------------------------------------------
  214. // generic allocator
  215. template <typename T> boost::shared_ptr<ElemBase> Allocate() const;
  216. // --------------------------------------------------------
  217. // field parsing for 1d arrays
  218. template <int error_policy, typename T, size_t M>
  219. void ReadFieldArray(T (& out)[M], const char* name,
  220. const FileDatabase& db) const;
  221. // --------------------------------------------------------
  222. // field parsing for 2d arrays
  223. template <int error_policy, typename T, size_t M, size_t N>
  224. void ReadFieldArray2(T (& out)[M][N], const char* name,
  225. const FileDatabase& db) const;
  226. // --------------------------------------------------------
  227. // field parsing for pointer or dynamic array types
  228. // (boost::shared_ptr or boost::shared_array)
  229. template <int error_policy, template <typename> class TOUT, typename T>
  230. void ReadFieldPtr(TOUT<T>& out, const char* name,
  231. const FileDatabase& db) const;
  232. // --------------------------------------------------------
  233. // field parsing for static arrays of pointer or dynamic
  234. // array types (boost::shared_ptr[] or boost::shared_array[])
  235. template <int error_policy, template <typename> class TOUT, typename T, size_t N>
  236. void ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
  237. const FileDatabase& db) const;
  238. // --------------------------------------------------------
  239. // field parsing for `normal` values
  240. template <int error_policy, typename T>
  241. void ReadField(T& out, const char* name,
  242. const FileDatabase& db) const;
  243. private:
  244. // --------------------------------------------------------
  245. template <template <typename> class TOUT, typename T>
  246. void ResolvePointer(TOUT<T>& out, const Pointer & ptrval,
  247. const FileDatabase& db, const Field& f) const;
  248. // --------------------------------------------------------
  249. template <template <typename> class TOUT, typename T>
  250. void ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
  251. const FileDatabase& db, const Field& f) const;
  252. // --------------------------------------------------------
  253. void ResolvePointer( boost::shared_ptr< FileOffset >& out, const Pointer & ptrval,
  254. const FileDatabase& db, const Field& f) const;
  255. // --------------------------------------------------------
  256. inline const FileBlockHead* LocateFileBlockForAddress(
  257. const Pointer & ptrval,
  258. const FileDatabase& db) const;
  259. private:
  260. // ------------------------------------------------------------------------------
  261. template <typename T> T* _allocate(boost::shared_ptr<T>& out, size_t& s) const {
  262. out = boost::shared_ptr<T>(new T());
  263. s = 1;
  264. return out.get();
  265. }
  266. template <typename T> T* _allocate(vector<T>& out, size_t& s) const {
  267. out.resize(s);
  268. return s ? &out.front() : NULL;
  269. }
  270. // --------------------------------------------------------
  271. template <int error_policy>
  272. struct _defaultInitializer {
  273. template <typename T, unsigned int N>
  274. void operator ()(T (& out)[N], const char* = NULL) {
  275. for (unsigned int i = 0; i < N; ++i) {
  276. out[i] = T();
  277. }
  278. }
  279. template <typename T, unsigned int N, unsigned int M>
  280. void operator ()(T (& out)[N][M], const char* = NULL) {
  281. for (unsigned int i = 0; i < N; ++i) {
  282. for (unsigned int j = 0; j < M; ++j) {
  283. out[i][j] = T();
  284. }
  285. }
  286. }
  287. template <typename T>
  288. void operator ()(T& out, const char* = NULL) {
  289. out = T();
  290. }
  291. };
  292. private:
  293. mutable size_t cache_idx;
  294. };
  295. // --------------------------------------------------------
  296. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Warn> {
  297. template <typename T>
  298. void operator ()(T& out, const char* reason = "<add reason>") {
  299. DefaultLogger::get()->warn(reason);
  300. // ... and let the show go on
  301. _defaultInitializer<0 /*ErrorPolicy_Igno*/>()(out);
  302. }
  303. };
  304. template <> struct Structure :: _defaultInitializer<ErrorPolicy_Fail> {
  305. template <typename T>
  306. void operator ()(T& /*out*/,const char* = "") {
  307. // obviously, it is crucial that _DefaultInitializer is used
  308. // only from within a catch clause.
  309. throw;
  310. }
  311. };
  312. // -------------------------------------------------------------------------------------------------------
  313. template <> inline void Structure :: ResolvePointer<boost::shared_ptr,ElemBase>(boost::shared_ptr<ElemBase>& out,
  314. const Pointer & ptrval,
  315. const FileDatabase& db,
  316. const Field& f
  317. ) const;
  318. // -------------------------------------------------------------------------------
  319. /** Represents the full data structure information for a single BLEND file.
  320. * This data is extracted from the DNA1 chunk in the file.
  321. * #DNAParser does the reading and represents currently the only place where
  322. * DNA is altered.*/
  323. // -------------------------------------------------------------------------------
  324. class DNA
  325. {
  326. public:
  327. typedef void (Structure::*ConvertProcPtr) (
  328. boost::shared_ptr<ElemBase> in,
  329. const FileDatabase&
  330. ) const;
  331. typedef boost::shared_ptr<ElemBase> (
  332. Structure::*AllocProcPtr) () const;
  333. typedef std::pair< AllocProcPtr, ConvertProcPtr > FactoryPair;
  334. public:
  335. std::map<std::string, FactoryPair > converters;
  336. vector<Structure > structures;
  337. std::map<std::string, size_t> indices;
  338. public:
  339. // --------------------------------------------------------
  340. /** Access a structure by its canonical name, the pointer version returns NULL on failure
  341. * while the reference version raises an error. */
  342. inline const Structure& operator [] (const std::string& ss) const;
  343. inline const Structure* Get (const std::string& ss) const;
  344. // --------------------------------------------------------
  345. /** Access a structure by its index */
  346. inline const Structure& operator [] (const size_t i) const;
  347. public:
  348. // --------------------------------------------------------
  349. /** Add structure definitions for all the primitive types,
  350. * i.e. integer, short, char, float */
  351. void AddPrimitiveStructures();
  352. // --------------------------------------------------------
  353. /** Fill the @c converters member with converters for all
  354. * known data types. The implementation of this method is
  355. * in BlenderScene.cpp and is machine-generated.
  356. * Converters are used to quickly handle objects whose
  357. * exact data type is a runtime-property and not yet
  358. * known at compile time (consier Object::data).*/
  359. void RegisterConverters();
  360. // --------------------------------------------------------
  361. /** Take an input blob from the stream, interpret it according to
  362. * a its structure name and convert it to the intermediate
  363. * representation.
  364. * @param structure Destination structure definition
  365. * @param db File database.
  366. * @return A null pointer if no appropriate converter is available.*/
  367. boost::shared_ptr< ElemBase > ConvertBlobToStructure(
  368. const Structure& structure,
  369. const FileDatabase& db
  370. ) const;
  371. // --------------------------------------------------------
  372. /** Find a suitable conversion function for a given Structure.
  373. * Such a converter function takes a blob from the input
  374. * stream, reads as much as it needs, and builds up a
  375. * complete object in intermediate representation.
  376. * @param structure Destination structure definition
  377. * @param db File database.
  378. * @return A null pointer in .first if no appropriate converter is available.*/
  379. FactoryPair GetBlobToStructureConverter(
  380. const Structure& structure,
  381. const FileDatabase& db
  382. ) const;
  383. #ifdef ASSIMP_BUILD_BLENDER_DEBUG
  384. // --------------------------------------------------------
  385. /** Dump the DNA to a text file. This is for debugging purposes.
  386. * The output file is `dna.txt` in the current working folder*/
  387. void DumpToFile();
  388. #endif
  389. // --------------------------------------------------------
  390. /** Extract array dimensions from a C array declaration, such
  391. * as `...[4][6]`. Returned string would be `...[][]`.
  392. * @param out
  393. * @param array_sizes Receive maximally two array dimensions,
  394. * the second element is set to 1 if the array is flat.
  395. * Both are set to 1 if the input is not an array.
  396. * @throw DeadlyImportError if more than 2 dimensions are
  397. * encountered. */
  398. static void ExtractArraySize(
  399. const std::string& out,
  400. size_t array_sizes[2]
  401. );
  402. };
  403. // special converters for primitive types
  404. template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const;
  405. template <> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const;
  406. template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const;
  407. template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const;
  408. template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const;
  409. template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const;
  410. // -------------------------------------------------------------------------------
  411. /** Describes a master file block header. Each master file sections holds n
  412. * elements of a certain SDNA structure (or otherwise unspecified data). */
  413. // -------------------------------------------------------------------------------
  414. struct FileBlockHead
  415. {
  416. // points right after the header of the file block
  417. StreamReaderAny::pos start;
  418. std::string id;
  419. size_t size;
  420. // original memory address of the data
  421. Pointer address;
  422. // index into DNA
  423. unsigned int dna_index;
  424. // number of structure instances to follow
  425. size_t num;
  426. // file blocks are sorted by address to quickly locate specific memory addresses
  427. bool operator < (const FileBlockHead& o) const {
  428. return address.val < o.address.val;
  429. }
  430. // for std::upper_bound
  431. operator const Pointer& () const {
  432. return address;
  433. }
  434. };
  435. // for std::upper_bound
  436. inline bool operator< (const Pointer& a, const Pointer& b) {
  437. return a.val < b.val;
  438. }
  439. // -------------------------------------------------------------------------------
  440. /** Utility to read all master file blocks in turn. */
  441. // -------------------------------------------------------------------------------
  442. class SectionParser
  443. {
  444. public:
  445. // --------------------------------------------------------
  446. /** @param stream Inout stream, must point to the
  447. * first section in the file. Call Next() once
  448. * to have it read.
  449. * @param ptr64 Pointer size in file is 64 bits? */
  450. SectionParser(StreamReaderAny& stream,bool ptr64)
  451. : stream(stream)
  452. , ptr64(ptr64)
  453. {
  454. current.size = current.start = 0;
  455. }
  456. public:
  457. // --------------------------------------------------------
  458. const FileBlockHead& GetCurrent() const {
  459. return current;
  460. }
  461. public:
  462. // --------------------------------------------------------
  463. /** Advance to the next section.
  464. * @throw DeadlyImportError if the last chunk was passed. */
  465. void Next();
  466. public:
  467. FileBlockHead current;
  468. StreamReaderAny& stream;
  469. bool ptr64;
  470. };
  471. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  472. // -------------------------------------------------------------------------------
  473. /** Import statistics, i.e. number of file blocks read*/
  474. // -------------------------------------------------------------------------------
  475. class Statistics {
  476. public:
  477. Statistics ()
  478. : fields_read ()
  479. , pointers_resolved ()
  480. , cache_hits ()
  481. // , blocks_read ()
  482. , cached_objects ()
  483. {}
  484. public:
  485. /** total number of fields we read */
  486. unsigned int fields_read;
  487. /** total number of resolved pointers */
  488. unsigned int pointers_resolved;
  489. /** number of pointers resolved from the cache */
  490. unsigned int cache_hits;
  491. /** number of blocks (from FileDatabase::entries)
  492. we did actually read from. */
  493. // unsigned int blocks_read;
  494. /** objects in FileData::cache */
  495. unsigned int cached_objects;
  496. };
  497. #endif
  498. // -------------------------------------------------------------------------------
  499. /** The object cache - all objects addressed by pointers are added here. This
  500. * avoids circular references and avoids object duplication. */
  501. // -------------------------------------------------------------------------------
  502. template <template <typename> class TOUT>
  503. class ObjectCache
  504. {
  505. public:
  506. typedef std::map< Pointer, TOUT<ElemBase> > StructureCache;
  507. public:
  508. ObjectCache(const FileDatabase& db)
  509. : db(db)
  510. {
  511. // currently there are only ~400 structure records per blend file.
  512. // we read only a small part of them and don't cache objects
  513. // which we don't need, so this should suffice.
  514. caches.reserve(64);
  515. }
  516. public:
  517. // --------------------------------------------------------
  518. /** Check whether a specific item is in the cache.
  519. * @param s Data type of the item
  520. * @param out Output pointer. Unchanged if the
  521. * cache doens't know the item yet.
  522. * @param ptr Item address to look for. */
  523. template <typename T> void get (
  524. const Structure& s,
  525. TOUT<T>& out,
  526. const Pointer& ptr) const;
  527. // --------------------------------------------------------
  528. /** Add an item to the cache after the item has
  529. * been fully read. Do not insert anything that
  530. * may be faulty or might cause the loading
  531. * to abort.
  532. * @param s Data type of the item
  533. * @param out Item to insert into the cache
  534. * @param ptr address (cache key) of the item. */
  535. template <typename T> void set
  536. (const Structure& s,
  537. const TOUT<T>& out,
  538. const Pointer& ptr);
  539. private:
  540. mutable vector<StructureCache> caches;
  541. const FileDatabase& db;
  542. };
  543. // -------------------------------------------------------------------------------
  544. // -------------------------------------------------------------------------------
  545. template <> class ObjectCache<Blender::vector>
  546. {
  547. public:
  548. ObjectCache(const FileDatabase&) {}
  549. template <typename T> void get(const Structure&, vector<T>&, const Pointer&) {}
  550. template <typename T> void set(const Structure&, const vector<T>&, const Pointer&) {}
  551. };
  552. #ifdef _MSC_VER
  553. # pragma warning(disable:4355)
  554. #endif
  555. // -------------------------------------------------------------------------------
  556. /** Memory representation of a full BLEND file and all its dependencies. The
  557. * output aiScene is constructed from an instance of this data structure. */
  558. // -------------------------------------------------------------------------------
  559. class FileDatabase
  560. {
  561. template <template <typename> class TOUT> friend class ObjectCache;
  562. public:
  563. FileDatabase()
  564. : _cacheArrays(*this)
  565. , _cache(*this)
  566. , next_cache_idx()
  567. {}
  568. public:
  569. // publicly accessible fields
  570. bool i64bit;
  571. bool little;
  572. DNA dna;
  573. boost::shared_ptr< StreamReaderAny > reader;
  574. vector< FileBlockHead > entries;
  575. public:
  576. Statistics& stats() const {
  577. return _stats;
  578. }
  579. // For all our templates to work on both shared_ptr's and vector's
  580. // using the same code, a dummy cache for arrays is provided. Actually,
  581. // arrays of objects are never cached because we can't easily
  582. // ensure their proper destruction.
  583. template <typename T>
  584. ObjectCache<boost::shared_ptr>& cache(boost::shared_ptr<T>& /*in*/) const {
  585. return _cache;
  586. }
  587. template <typename T>
  588. ObjectCache<vector>& cache(vector<T>& /*in*/) const {
  589. return _cacheArrays;
  590. }
  591. private:
  592. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  593. mutable Statistics _stats;
  594. #endif
  595. mutable ObjectCache<vector> _cacheArrays;
  596. mutable ObjectCache<boost::shared_ptr> _cache;
  597. mutable size_t next_cache_idx;
  598. };
  599. #ifdef _MSC_VER
  600. # pragma warning(default:4355)
  601. #endif
  602. // -------------------------------------------------------------------------------
  603. /** Factory to extract a #DNA from the DNA1 file block in a BLEND file. */
  604. // -------------------------------------------------------------------------------
  605. class DNAParser
  606. {
  607. public:
  608. /** Bind the parser to a empty DNA and an input stream */
  609. DNAParser(FileDatabase& db)
  610. : db(db)
  611. {}
  612. public:
  613. // --------------------------------------------------------
  614. /** Locate the DNA in the file and parse it. The input
  615. * stream is expected to point to the beginning of the DN1
  616. * chunk at the time this method is called and is
  617. * undefined afterwards.
  618. * @throw DeadlyImportError if the DNA cannot be read.
  619. * @note The position of the stream pointer is undefined
  620. * afterwards.*/
  621. void Parse ();
  622. public:
  623. /** Obtain a reference to the extracted DNA information */
  624. const Blender::DNA& GetDNA() const {
  625. return db.dna;
  626. }
  627. private:
  628. FileDatabase& db;
  629. };
  630. } // end Blend
  631. } // end Assimp
  632. #include "BlenderDNA.inl"
  633. #endif