metadata.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 <stdint.h>
  44. #endif
  45. // -------------------------------------------------------------------------------
  46. /**
  47. * Enum used to distinguish data types
  48. */
  49. // -------------------------------------------------------------------------------
  50. typedef enum aiMetadataType
  51. {
  52. AI_BOOL = 0,
  53. AI_INT = 1,
  54. AI_UINT64 = 2,
  55. AI_FLOAT = 3,
  56. AI_AISTRING = 4,
  57. AI_AIVECTOR3D = 5,
  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. {
  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( int ) { return AI_INT; }
  83. inline aiMetadataType GetAiType( uint64_t ) { return AI_UINT64; }
  84. inline aiMetadataType GetAiType( float ) { return AI_FLOAT; }
  85. inline aiMetadataType GetAiType( aiString ) { return AI_AISTRING; }
  86. inline aiMetadataType GetAiType( aiVector3D ) { return AI_AIVECTOR3D; }
  87. #endif
  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. {
  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. /** Constructor */
  106. aiMetadata()
  107. // set all members to zero by default
  108. : mNumProperties(0)
  109. , mKeys(NULL)
  110. , mValues(NULL)
  111. {}
  112. /** Destructor */
  113. ~aiMetadata()
  114. {
  115. delete[] mKeys;
  116. mKeys = NULL;
  117. if (mValues)
  118. {
  119. // Delete each metadata entry
  120. for (unsigned i=0; i<mNumProperties; ++i)
  121. {
  122. void* data = mValues[i].mData;
  123. switch (mValues[i].mType)
  124. {
  125. case AI_BOOL:
  126. delete static_cast<bool*>(data);
  127. break;
  128. case AI_INT:
  129. delete static_cast<int*>(data);
  130. break;
  131. case AI_UINT64:
  132. delete static_cast<uint64_t*>(data);
  133. break;
  134. case AI_FLOAT:
  135. delete static_cast<float*>(data);
  136. break;
  137. case AI_AISTRING:
  138. delete static_cast<aiString*>(data);
  139. break;
  140. case AI_AIVECTOR3D:
  141. delete static_cast<aiVector3D*>(data);
  142. break;
  143. default:
  144. assert(false);
  145. break;
  146. }
  147. }
  148. // Delete the metadata array
  149. delete [] mValues;
  150. mValues = NULL;
  151. }
  152. }
  153. template<typename T>
  154. inline void Set( unsigned index, const std::string& key, const T& value )
  155. {
  156. // In range assertion
  157. assert(index < mNumProperties);
  158. // Set metadata key
  159. mKeys[index] = key;
  160. // Set metadata type
  161. mValues[index].mType = GetAiType(value);
  162. // Copy the given value to the dynamic storage
  163. mValues[index].mData = new T(value);
  164. }
  165. template<typename T>
  166. inline bool Get( unsigned index, T& value )
  167. {
  168. // In range assertion
  169. assert(index < mNumProperties);
  170. // Return false if the output data type does
  171. // not match the found value's data type
  172. if ( GetAiType( value ) != mValues[ index ].mType ) {
  173. return false;
  174. }
  175. // Otherwise, output the found value and
  176. // return true
  177. value = *static_cast<T*>(mValues[index].mData);
  178. return true;
  179. }
  180. template<typename T>
  181. inline bool Get( const aiString& key, T& value )
  182. {
  183. // Search for the given key
  184. for (unsigned i=0; i<mNumProperties; ++i)
  185. if (mKeys[i]==key)
  186. return Get(i, value);
  187. return false;
  188. }
  189. template<typename T>
  190. inline bool Get( const std::string& key, T& value ) {
  191. return Get(aiString(key), value);
  192. }
  193. #endif // __cplusplus
  194. };
  195. #endif // __AI_METADATA_H_INC__