metadata.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2017, 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. #ifndef SWIG
  59. FORCE_32BIT = INT_MAX
  60. #endif
  61. } aiMetadataType;
  62. // -------------------------------------------------------------------------------
  63. /**
  64. * Metadata entry
  65. *
  66. * The type field uniquely identifies the underlying type of the data field
  67. */
  68. // -------------------------------------------------------------------------------
  69. struct aiMetadataEntry {
  70. aiMetadataType mType;
  71. void* mData;
  72. };
  73. #ifdef __cplusplus
  74. #include <string>
  75. // -------------------------------------------------------------------------------
  76. /**
  77. * Helper functions to get the aiType enum entry for a type
  78. */
  79. // -------------------------------------------------------------------------------
  80. inline aiMetadataType GetAiType( bool ) { return AI_BOOL; }
  81. inline aiMetadataType GetAiType( int32_t ) { return AI_INT32; }
  82. inline aiMetadataType GetAiType( uint64_t ) { return AI_UINT64; }
  83. inline aiMetadataType GetAiType( float ) { return AI_FLOAT; }
  84. inline aiMetadataType GetAiType( double ) { return AI_DOUBLE; }
  85. inline aiMetadataType GetAiType( const aiString & ) { return AI_AISTRING; }
  86. inline aiMetadataType GetAiType( const aiVector3D & ) { return AI_AIVECTOR3D; }
  87. #endif // __cplusplus
  88. // -------------------------------------------------------------------------------
  89. /**
  90. * Container for holding metadata.
  91. *
  92. * Metadata is a key-value store using string keys and values.
  93. */
  94. // -------------------------------------------------------------------------------
  95. struct aiMetadata {
  96. /** Length of the mKeys and mValues arrays, respectively */
  97. unsigned int mNumProperties;
  98. /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
  99. C_STRUCT aiString* mKeys;
  100. /** Arrays of values, may not be NULL. Entries in this array may be NULL if the
  101. * corresponding property key has no assigned value. */
  102. C_STRUCT aiMetadataEntry* mValues;
  103. #ifdef __cplusplus
  104. /**
  105. * @brief The default constructor, set all members to zero by default.
  106. */
  107. aiMetadata()
  108. : mNumProperties(0)
  109. , mKeys(NULL)
  110. , mValues(NULL) {
  111. // empty
  112. }
  113. /**
  114. * @brief The destructor.
  115. */
  116. ~aiMetadata() {
  117. delete [] mKeys;
  118. mKeys = NULL;
  119. if (mValues) {
  120. // Delete each metadata entry
  121. for (unsigned i=0; i<mNumProperties; ++i) {
  122. void* data = mValues[i].mData;
  123. switch (mValues[i].mType) {
  124. case AI_BOOL:
  125. delete static_cast<bool*>(data);
  126. break;
  127. case AI_INT32:
  128. delete static_cast<int32_t*>(data);
  129. break;
  130. case AI_UINT64:
  131. delete static_cast<uint64_t*>(data);
  132. break;
  133. case AI_FLOAT:
  134. delete static_cast<float*>(data);
  135. break;
  136. case AI_DOUBLE:
  137. delete static_cast<double*>(data);
  138. break;
  139. case AI_AISTRING:
  140. delete static_cast<aiString*>(data);
  141. break;
  142. case AI_AIVECTOR3D:
  143. delete static_cast<aiVector3D*>(data);
  144. break;
  145. #ifndef SWIG
  146. case FORCE_32BIT:
  147. #endif
  148. default:
  149. break;
  150. }
  151. }
  152. // Delete the metadata array
  153. delete [] mValues;
  154. mValues = NULL;
  155. }
  156. }
  157. /**
  158. * @brief Allocates property fields + keys.
  159. * @param numProperties Number of requested properties.
  160. */
  161. static inline
  162. aiMetadata *Alloc( unsigned int numProperties ) {
  163. if ( 0 == numProperties ) {
  164. return nullptr;
  165. }
  166. aiMetadata *data = new aiMetadata;
  167. data->mNumProperties = numProperties;
  168. data->mKeys = new aiString[ data->mNumProperties ]();
  169. data->mValues = new aiMetadataEntry[ data->mNumProperties ]();
  170. return data;
  171. }
  172. /**
  173. * @brief Deallocates property fields + keys.
  174. */
  175. static inline
  176. void Dealloc( aiMetadata *metadata ) {
  177. delete metadata;
  178. }
  179. template<typename T>
  180. inline void Add(const std::string& key, const T& value)
  181. {
  182. aiString* new_keys = new aiString[mNumProperties + 1];
  183. aiMetadataEntry* new_values = new aiMetadataEntry[mNumProperties + 1];
  184. for(unsigned int i = 0; i < mNumProperties; ++i)
  185. {
  186. new_keys[i] = mKeys[i];
  187. new_values[i] = mValues[i];
  188. }
  189. delete mKeys;
  190. delete mValues;
  191. mKeys = new_keys;
  192. mValues = new_values;
  193. mNumProperties++;
  194. Set(mNumProperties - 1, key, value);
  195. }
  196. template<typename T>
  197. inline
  198. bool Set( unsigned index, const std::string& key, const T& value ) {
  199. // In range assertion
  200. if ( index >= mNumProperties ) {
  201. return false;
  202. }
  203. // Ensure that we have a valid key.
  204. if ( key.empty() ) {
  205. return false;
  206. }
  207. // Set metadata key
  208. mKeys[index] = key;
  209. // Set metadata type
  210. mValues[index].mType = GetAiType(value);
  211. // Copy the given value to the dynamic storage
  212. mValues[index].mData = new T(value);
  213. return true;
  214. }
  215. template<typename T>
  216. inline
  217. bool Get( unsigned index, T& value ) {
  218. // In range assertion
  219. if ( index >= mNumProperties ) {
  220. return false;
  221. }
  222. // Return false if the output data type does
  223. // not match the found value's data type
  224. if ( GetAiType( value ) != mValues[ index ].mType ) {
  225. return false;
  226. }
  227. // Otherwise, output the found value and
  228. // return true
  229. value = *static_cast<T*>(mValues[index].mData);
  230. return true;
  231. }
  232. template<typename T>
  233. inline
  234. bool Get( const aiString& key, T& value ) {
  235. // Search for the given key
  236. for ( unsigned int i = 0; i < mNumProperties; ++i ) {
  237. if ( mKeys[ i ] == key ) {
  238. return Get( i, value );
  239. }
  240. }
  241. return false;
  242. }
  243. template<typename T>
  244. inline bool Get( const std::string& key, T& value ) {
  245. return Get(aiString(key), value);
  246. }
  247. /// Return metadata entry for analyzing it by user.
  248. /// \param [in] pIndex - index of the entry.
  249. /// \param [out] pKey - pointer to the key value.
  250. /// \param [out] pEntry - pointer to the entry: type and value.
  251. /// \return false - if pIndex is out of range, else - true.
  252. inline bool Get(size_t index, const aiString*& key, const aiMetadataEntry*& entry) {
  253. if ( index >= mNumProperties ) {
  254. return false;
  255. }
  256. key = &mKeys[index];
  257. entry = &mValues[index];
  258. return true;
  259. }
  260. #endif // __cplusplus
  261. };
  262. #endif // AI_METADATA_H_INC