metadata.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2015, 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. #ifndef __AI_METADATA_H_INC__
  38. #define __AI_METADATA_H_INC__
  39. #include <assert.h>
  40. #if defined(_MSC_VER) && (_MSC_VER <= 1500)
  41. #include "Compiler/pstdint.h"
  42. #else
  43. #include <limits.h>
  44. #include <stdint.h>
  45. #endif
  46. // -------------------------------------------------------------------------------
  47. /**
  48. * Enum used to distinguish data types
  49. */
  50. // -------------------------------------------------------------------------------
  51. typedef enum aiMetadataType
  52. {
  53. AI_BOOL = 0,
  54. AI_INT = 1,
  55. AI_UINT64 = 2,
  56. AI_FLOAT = 3,
  57. AI_AISTRING = 4,
  58. AI_AIVECTOR3D = 5,
  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. {
  72. aiMetadataType mType;
  73. void* mData;
  74. };
  75. #ifdef __cplusplus
  76. #include <string>
  77. // -------------------------------------------------------------------------------
  78. /**
  79. * Helper functions to get the aiType enum entry for a type
  80. */
  81. // -------------------------------------------------------------------------------
  82. inline aiMetadataType GetAiType( bool ) { return AI_BOOL; }
  83. inline aiMetadataType GetAiType( int ) { return AI_INT; }
  84. inline aiMetadataType GetAiType( uint64_t ) { return AI_UINT64; }
  85. inline aiMetadataType GetAiType( float ) { return AI_FLOAT; }
  86. inline aiMetadataType GetAiType( aiString ) { return AI_AISTRING; }
  87. inline aiMetadataType GetAiType( aiVector3D ) { return AI_AIVECTOR3D; }
  88. #endif
  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. {
  98. /** Length of the mKeys and mValues arrays, respectively */
  99. unsigned int mNumProperties;
  100. /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
  101. C_STRUCT aiString* mKeys;
  102. /** Arrays of values, may not be NULL. Entries in this array may be NULL if the
  103. * corresponding property key has no assigned value. */
  104. C_STRUCT aiMetadataEntry* mValues;
  105. #ifdef __cplusplus
  106. /** Constructor */
  107. aiMetadata()
  108. // set all members to zero by default
  109. : mNumProperties(0)
  110. , mKeys(NULL)
  111. , mValues(NULL)
  112. {}
  113. /** Destructor */
  114. ~aiMetadata()
  115. {
  116. delete[] mKeys;
  117. mKeys = NULL;
  118. if (mValues)
  119. {
  120. // Delete each metadata entry
  121. for (unsigned i=0; i<mNumProperties; ++i)
  122. {
  123. void* data = mValues[i].mData;
  124. switch (mValues[i].mType)
  125. {
  126. case AI_BOOL:
  127. delete static_cast<bool*>(data);
  128. break;
  129. case AI_INT:
  130. delete static_cast<int*>(data);
  131. break;
  132. case AI_UINT64:
  133. delete static_cast<uint64_t*>(data);
  134. break;
  135. case AI_FLOAT:
  136. delete static_cast<float*>(data);
  137. break;
  138. case AI_AISTRING:
  139. delete static_cast<aiString*>(data);
  140. break;
  141. case AI_AIVECTOR3D:
  142. delete static_cast<aiVector3D*>(data);
  143. break;
  144. default:
  145. assert(false);
  146. break;
  147. }
  148. }
  149. // Delete the metadata array
  150. delete [] mValues;
  151. mValues = NULL;
  152. }
  153. }
  154. template<typename T>
  155. inline void Set( unsigned index, const std::string& key, const T& value )
  156. {
  157. // In range assertion
  158. assert(index < mNumProperties);
  159. // Set metadata key
  160. mKeys[index] = key;
  161. // Set metadata type
  162. mValues[index].mType = GetAiType(value);
  163. // Copy the given value to the dynamic storage
  164. mValues[index].mData = new T(value);
  165. }
  166. template<typename T>
  167. inline bool Get( unsigned index, T& value )
  168. {
  169. // In range assertion
  170. assert(index < mNumProperties);
  171. // Return false if the output data type does
  172. // not match the found value's data type
  173. if ( GetAiType( value ) != mValues[ index ].mType ) {
  174. return false;
  175. }
  176. // Otherwise, output the found value and
  177. // return true
  178. value = *static_cast<T*>(mValues[index].mData);
  179. return true;
  180. }
  181. template<typename T>
  182. inline bool Get( const aiString& key, T& value )
  183. {
  184. // Search for the given key
  185. for (unsigned i=0; i<mNumProperties; ++i)
  186. if (mKeys[i]==key)
  187. return Get(i, value);
  188. return false;
  189. }
  190. template<typename T>
  191. inline bool Get( const std::string& key, T& value ) {
  192. return Get(aiString(key), value);
  193. }
  194. #endif // __cplusplus
  195. };
  196. #endif // __AI_METADATA_H_INC__