metadata.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2019, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file metadata.h
  35. * @brief Defines the data structures for holding node meta information.
  36. */
  37. #pragma once
  38. #ifndef AI_METADATA_H_INC
  39. #define AI_METADATA_H_INC
  40. #if defined(_MSC_VER) && (_MSC_VER <= 1500)
  41. # include "Compiler/pstdint.h"
  42. #else
  43. # include <stdint.h>
  44. #endif
  45. // -------------------------------------------------------------------------------
  46. /**
  47. * Enum used to distinguish data types
  48. */
  49. // -------------------------------------------------------------------------------
  50. typedef enum aiMetadataType {
  51. AI_BOOL = 0,
  52. AI_INT32 = 1,
  53. AI_UINT64 = 2,
  54. AI_FLOAT = 3,
  55. AI_DOUBLE = 4,
  56. AI_AISTRING = 5,
  57. AI_AIVECTOR3D = 6,
  58. AI_META_MAX = 7,
  59. #ifndef SWIG
  60. FORCE_32BIT = INT_MAX
  61. #endif
  62. } aiMetadataType;
  63. // -------------------------------------------------------------------------------
  64. /**
  65. * Metadata entry
  66. *
  67. * The type field uniquely identifies the underlying type of the data field
  68. */
  69. // -------------------------------------------------------------------------------
  70. struct aiMetadataEntry {
  71. aiMetadataType mType;
  72. void* mData;
  73. };
  74. #ifdef __cplusplus
  75. #include <string>
  76. // -------------------------------------------------------------------------------
  77. /**
  78. * Helper functions to get the aiType enum entry for a type
  79. */
  80. // -------------------------------------------------------------------------------
  81. inline aiMetadataType GetAiType( bool ) { return AI_BOOL; }
  82. inline aiMetadataType GetAiType( int32_t ) { return AI_INT32; }
  83. inline aiMetadataType GetAiType( uint64_t ) { return AI_UINT64; }
  84. inline aiMetadataType GetAiType( float ) { return AI_FLOAT; }
  85. inline aiMetadataType GetAiType( double ) { return AI_DOUBLE; }
  86. inline aiMetadataType GetAiType( const aiString & ) { return AI_AISTRING; }
  87. inline aiMetadataType GetAiType( const aiVector3D & ) { return AI_AIVECTOR3D; }
  88. #endif // __cplusplus
  89. // -------------------------------------------------------------------------------
  90. /**
  91. * Container for holding metadata.
  92. *
  93. * Metadata is a key-value store using string keys and values.
  94. */
  95. // -------------------------------------------------------------------------------
  96. struct aiMetadata {
  97. /** Length of the mKeys and mValues arrays, respectively */
  98. unsigned int mNumProperties;
  99. /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
  100. C_STRUCT aiString* mKeys;
  101. /** Arrays of values, may not be NULL. Entries in this array may be NULL if the
  102. * corresponding property key has no assigned value. */
  103. C_STRUCT aiMetadataEntry* mValues;
  104. #ifdef __cplusplus
  105. /**
  106. * @brief The default constructor, set all members to zero by default.
  107. */
  108. aiMetadata() AI_NO_EXCEPT
  109. : mNumProperties(0)
  110. , mKeys(nullptr)
  111. , mValues(nullptr) {
  112. // empty
  113. }
  114. aiMetadata( const aiMetadata &rhs )
  115. : mNumProperties( rhs.mNumProperties )
  116. , mKeys( nullptr )
  117. , mValues( nullptr ) {
  118. mKeys = new aiString[ mNumProperties ];
  119. for ( size_t i = 0; i < static_cast<size_t>( mNumProperties ); ++i ) {
  120. mKeys[ i ] = rhs.mKeys[ i ];
  121. }
  122. mValues = new aiMetadataEntry[ mNumProperties ];
  123. for ( size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i ) {
  124. mValues[ i ].mType = rhs.mValues[ i ].mType;
  125. switch ( rhs.mValues[ i ].mType ) {
  126. case AI_BOOL:
  127. mValues[ i ].mData = new bool;
  128. ::memcpy( mValues[ i ].mData, rhs.mValues[ i ].mData, sizeof(bool) );
  129. break;
  130. case AI_INT32: {
  131. int32_t v;
  132. ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( int32_t ) );
  133. mValues[ i ].mData = new int32_t( v );
  134. }
  135. break;
  136. case AI_UINT64: {
  137. uint64_t v;
  138. ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( uint64_t ) );
  139. mValues[ i ].mData = new uint64_t( v );
  140. }
  141. break;
  142. case AI_FLOAT: {
  143. float v;
  144. ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( float ) );
  145. mValues[ i ].mData = new float( v );
  146. }
  147. break;
  148. case AI_DOUBLE: {
  149. double v;
  150. ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( double ) );
  151. mValues[ i ].mData = new double( v );
  152. }
  153. break;
  154. case AI_AISTRING: {
  155. aiString v;
  156. rhs.Get<aiString>( mKeys[ i ], v );
  157. mValues[ i ].mData = new aiString( v );
  158. }
  159. break;
  160. case AI_AIVECTOR3D: {
  161. aiVector3D v;
  162. rhs.Get<aiVector3D>( mKeys[ i ], v );
  163. mValues[ i ].mData = new aiVector3D( v );
  164. }
  165. break;
  166. #ifndef SWIG
  167. case FORCE_32BIT:
  168. #endif
  169. default:
  170. break;
  171. }
  172. }
  173. }
  174. /**
  175. * @brief The destructor.
  176. */
  177. ~aiMetadata() {
  178. delete [] mKeys;
  179. mKeys = nullptr;
  180. if (mValues) {
  181. // Delete each metadata entry
  182. for (unsigned i=0; i<mNumProperties; ++i) {
  183. void* data = mValues[i].mData;
  184. switch (mValues[i].mType) {
  185. case AI_BOOL:
  186. delete static_cast< bool* >( data );
  187. break;
  188. case AI_INT32:
  189. delete static_cast< int32_t* >( data );
  190. break;
  191. case AI_UINT64:
  192. delete static_cast< uint64_t* >( data );
  193. break;
  194. case AI_FLOAT:
  195. delete static_cast< float* >( data );
  196. break;
  197. case AI_DOUBLE:
  198. delete static_cast< double* >( data );
  199. break;
  200. case AI_AISTRING:
  201. delete static_cast< aiString* >( data );
  202. break;
  203. case AI_AIVECTOR3D:
  204. delete static_cast< aiVector3D* >( data );
  205. break;
  206. #ifndef SWIG
  207. case FORCE_32BIT:
  208. #endif
  209. default:
  210. break;
  211. }
  212. }
  213. // Delete the metadata array
  214. delete [] mValues;
  215. mValues = nullptr;
  216. }
  217. }
  218. /**
  219. * @brief Allocates property fields + keys.
  220. * @param numProperties Number of requested properties.
  221. */
  222. static inline
  223. aiMetadata *Alloc( unsigned int numProperties ) {
  224. if ( 0 == numProperties ) {
  225. return nullptr;
  226. }
  227. aiMetadata *data = new aiMetadata;
  228. data->mNumProperties = numProperties;
  229. data->mKeys = new aiString[ data->mNumProperties ]();
  230. data->mValues = new aiMetadataEntry[ data->mNumProperties ]();
  231. return data;
  232. }
  233. /**
  234. * @brief Deallocates property fields + keys.
  235. */
  236. static inline
  237. void Dealloc( aiMetadata *metadata ) {
  238. delete metadata;
  239. }
  240. template<typename T>
  241. inline
  242. void Add(const std::string& key, const T& value) {
  243. aiString* new_keys = new aiString[mNumProperties + 1];
  244. aiMetadataEntry* new_values = new aiMetadataEntry[mNumProperties + 1];
  245. for(unsigned int i = 0; i < mNumProperties; ++i)
  246. {
  247. new_keys[i] = mKeys[i];
  248. new_values[i] = mValues[i];
  249. }
  250. delete mKeys;
  251. delete mValues;
  252. mKeys = new_keys;
  253. mValues = new_values;
  254. mNumProperties++;
  255. Set(mNumProperties - 1, key, value);
  256. }
  257. template<typename T>
  258. inline
  259. bool Set( unsigned index, const std::string& key, const T& value ) {
  260. // In range assertion
  261. if ( index >= mNumProperties ) {
  262. return false;
  263. }
  264. // Ensure that we have a valid key.
  265. if ( key.empty() ) {
  266. return false;
  267. }
  268. // Set metadata key
  269. mKeys[index] = key;
  270. // Set metadata type
  271. mValues[index].mType = GetAiType(value);
  272. // Copy the given value to the dynamic storage
  273. mValues[index].mData = new T(value);
  274. return true;
  275. }
  276. template<typename T>
  277. inline
  278. bool Get( unsigned index, T& value ) const {
  279. // In range assertion
  280. if ( index >= mNumProperties ) {
  281. return false;
  282. }
  283. // Return false if the output data type does
  284. // not match the found value's data type
  285. if ( GetAiType( value ) != mValues[ index ].mType ) {
  286. return false;
  287. }
  288. // Otherwise, output the found value and
  289. // return true
  290. value = *static_cast<T*>(mValues[index].mData);
  291. return true;
  292. }
  293. template<typename T>
  294. inline
  295. bool Get( const aiString& key, T& value ) const {
  296. // Search for the given key
  297. for ( unsigned int i = 0; i < mNumProperties; ++i ) {
  298. if ( mKeys[ i ] == key ) {
  299. return Get( i, value );
  300. }
  301. }
  302. return false;
  303. }
  304. template<typename T>
  305. inline
  306. bool Get( const std::string& key, T& value ) const {
  307. return Get(aiString(key), value);
  308. }
  309. /// Return metadata entry for analyzing it by user.
  310. /// \param [in] pIndex - index of the entry.
  311. /// \param [out] pKey - pointer to the key value.
  312. /// \param [out] pEntry - pointer to the entry: type and value.
  313. /// \return false - if pIndex is out of range, else - true.
  314. inline
  315. bool Get(size_t index, const aiString*& key, const aiMetadataEntry*& entry) const {
  316. if ( index >= mNumProperties ) {
  317. return false;
  318. }
  319. key = &mKeys[index];
  320. entry = &mValues[index];
  321. return true;
  322. }
  323. #endif // __cplusplus
  324. };
  325. #endif // AI_METADATA_H_INC