metadata.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. #ifdef __GNUC__
  41. #pragma GCC system_header
  42. #endif
  43. #if defined(_MSC_VER) && (_MSC_VER <= 1500)
  44. #include "Compiler/pstdint.h"
  45. #else
  46. #include <stdint.h>
  47. #endif
  48. // -------------------------------------------------------------------------------
  49. /**
  50. * Enum used to distinguish data types
  51. */
  52. // -------------------------------------------------------------------------------
  53. typedef enum aiMetadataType {
  54. AI_BOOL = 0,
  55. AI_INT32 = 1,
  56. AI_UINT64 = 2,
  57. AI_FLOAT = 3,
  58. AI_DOUBLE = 4,
  59. AI_AISTRING = 5,
  60. AI_AIVECTOR3D = 6,
  61. AI_META_MAX = 7,
  62. #ifndef SWIG
  63. FORCE_32BIT = INT_MAX
  64. #endif
  65. } aiMetadataType;
  66. // -------------------------------------------------------------------------------
  67. /**
  68. * Metadata entry
  69. *
  70. * The type field uniquely identifies the underlying type of the data field
  71. */
  72. // -------------------------------------------------------------------------------
  73. struct aiMetadataEntry {
  74. aiMetadataType mType;
  75. void *mData;
  76. };
  77. #ifdef __cplusplus
  78. #include <string>
  79. // -------------------------------------------------------------------------------
  80. /**
  81. * Helper functions to get the aiType enum entry for a type
  82. */
  83. // -------------------------------------------------------------------------------
  84. inline aiMetadataType GetAiType(bool) {
  85. return AI_BOOL;
  86. }
  87. inline aiMetadataType GetAiType(int32_t) {
  88. return AI_INT32;
  89. }
  90. inline aiMetadataType GetAiType(uint64_t) {
  91. return AI_UINT64;
  92. }
  93. inline aiMetadataType GetAiType(float) {
  94. return AI_FLOAT;
  95. }
  96. inline aiMetadataType GetAiType(double) {
  97. return AI_DOUBLE;
  98. }
  99. inline aiMetadataType GetAiType(const aiString &) {
  100. return AI_AISTRING;
  101. }
  102. inline aiMetadataType GetAiType(const aiVector3D &) {
  103. return AI_AIVECTOR3D;
  104. }
  105. #endif // __cplusplus
  106. // -------------------------------------------------------------------------------
  107. /**
  108. * Container for holding metadata.
  109. *
  110. * Metadata is a key-value store using string keys and values.
  111. */
  112. // -------------------------------------------------------------------------------
  113. struct aiMetadata {
  114. /** Length of the mKeys and mValues arrays, respectively */
  115. unsigned int mNumProperties;
  116. /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
  117. C_STRUCT aiString *mKeys;
  118. /** Arrays of values, may not be NULL. Entries in this array may be NULL if the
  119. * corresponding property key has no assigned value. */
  120. C_STRUCT aiMetadataEntry *mValues;
  121. #ifdef __cplusplus
  122. /**
  123. * @brief The default constructor, set all members to zero by default.
  124. */
  125. aiMetadata() AI_NO_EXCEPT
  126. : mNumProperties(0),
  127. mKeys(nullptr),
  128. mValues(nullptr) {
  129. // empty
  130. }
  131. aiMetadata(const aiMetadata &rhs) :
  132. mNumProperties(rhs.mNumProperties), mKeys(nullptr), mValues(nullptr) {
  133. mKeys = new aiString[mNumProperties];
  134. for (size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i) {
  135. mKeys[i] = rhs.mKeys[i];
  136. }
  137. mValues = new aiMetadataEntry[mNumProperties];
  138. for (size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i) {
  139. mValues[i].mType = rhs.mValues[i].mType;
  140. switch (rhs.mValues[i].mType) {
  141. case AI_BOOL:
  142. mValues[i].mData = new bool;
  143. ::memcpy(mValues[i].mData, rhs.mValues[i].mData, sizeof(bool));
  144. break;
  145. case AI_INT32: {
  146. int32_t v;
  147. ::memcpy(&v, rhs.mValues[i].mData, sizeof(int32_t));
  148. mValues[i].mData = new int32_t(v);
  149. } break;
  150. case AI_UINT64: {
  151. uint64_t v;
  152. ::memcpy(&v, rhs.mValues[i].mData, sizeof(uint64_t));
  153. mValues[i].mData = new uint64_t(v);
  154. } break;
  155. case AI_FLOAT: {
  156. float v;
  157. ::memcpy(&v, rhs.mValues[i].mData, sizeof(float));
  158. mValues[i].mData = new float(v);
  159. } break;
  160. case AI_DOUBLE: {
  161. double v;
  162. ::memcpy(&v, rhs.mValues[i].mData, sizeof(double));
  163. mValues[i].mData = new double(v);
  164. } break;
  165. case AI_AISTRING: {
  166. aiString v;
  167. rhs.Get<aiString>(mKeys[i], v);
  168. mValues[i].mData = new aiString(v);
  169. } break;
  170. case AI_AIVECTOR3D: {
  171. aiVector3D v;
  172. rhs.Get<aiVector3D>(mKeys[i], v);
  173. mValues[i].mData = new aiVector3D(v);
  174. } break;
  175. #ifndef SWIG
  176. case FORCE_32BIT:
  177. #endif
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. /**
  184. * @brief The destructor.
  185. */
  186. ~aiMetadata() {
  187. delete[] mKeys;
  188. mKeys = nullptr;
  189. if (mValues) {
  190. // Delete each metadata entry
  191. for (unsigned i = 0; i < mNumProperties; ++i) {
  192. void *data = mValues[i].mData;
  193. switch (mValues[i].mType) {
  194. case AI_BOOL:
  195. delete static_cast<bool *>(data);
  196. break;
  197. case AI_INT32:
  198. delete static_cast<int32_t *>(data);
  199. break;
  200. case AI_UINT64:
  201. delete static_cast<uint64_t *>(data);
  202. break;
  203. case AI_FLOAT:
  204. delete static_cast<float *>(data);
  205. break;
  206. case AI_DOUBLE:
  207. delete static_cast<double *>(data);
  208. break;
  209. case AI_AISTRING:
  210. delete static_cast<aiString *>(data);
  211. break;
  212. case AI_AIVECTOR3D:
  213. delete static_cast<aiVector3D *>(data);
  214. break;
  215. #ifndef SWIG
  216. case FORCE_32BIT:
  217. #endif
  218. default:
  219. break;
  220. }
  221. }
  222. // Delete the metadata array
  223. delete[] mValues;
  224. mValues = nullptr;
  225. }
  226. }
  227. /**
  228. * @brief Allocates property fields + keys.
  229. * @param numProperties Number of requested properties.
  230. */
  231. static inline aiMetadata *Alloc(unsigned int numProperties) {
  232. if (0 == numProperties) {
  233. return nullptr;
  234. }
  235. aiMetadata *data = new aiMetadata;
  236. data->mNumProperties = numProperties;
  237. data->mKeys = new aiString[data->mNumProperties]();
  238. data->mValues = new aiMetadataEntry[data->mNumProperties]();
  239. return data;
  240. }
  241. /**
  242. * @brief Deallocates property fields + keys.
  243. */
  244. static inline void Dealloc(aiMetadata *metadata) {
  245. delete metadata;
  246. }
  247. template <typename T>
  248. inline void Add(const std::string &key, const T &value) {
  249. aiString *new_keys = new aiString[mNumProperties + 1];
  250. aiMetadataEntry *new_values = new aiMetadataEntry[mNumProperties + 1];
  251. for (unsigned int i = 0; i < mNumProperties; ++i) {
  252. new_keys[i] = mKeys[i];
  253. new_values[i] = mValues[i];
  254. }
  255. delete[] mKeys;
  256. delete[] mValues;
  257. mKeys = new_keys;
  258. mValues = new_values;
  259. mNumProperties++;
  260. Set(mNumProperties - 1, key, value);
  261. }
  262. template <typename T>
  263. inline bool Set(unsigned index, const std::string &key, const T &value) {
  264. // In range assertion
  265. if (index >= mNumProperties) {
  266. return false;
  267. }
  268. // Ensure that we have a valid key.
  269. if (key.empty()) {
  270. return false;
  271. }
  272. // Set metadata key
  273. mKeys[index] = key;
  274. // Set metadata type
  275. mValues[index].mType = GetAiType(value);
  276. // Copy the given value to the dynamic storage
  277. mValues[index].mData = new T(value);
  278. return true;
  279. }
  280. template <typename T>
  281. inline bool Set(const std::string &key, const T &value) {
  282. if (key.empty()) {
  283. return false;
  284. }
  285. bool result = false;
  286. for (unsigned int i = 0; i < mNumProperties; ++i) {
  287. if (key == mKeys[i].C_Str()) {
  288. Set(i, key, value);
  289. result = true;
  290. break;
  291. }
  292. }
  293. return result;
  294. }
  295. template <typename T>
  296. inline bool Get(unsigned index, T &value) const {
  297. // In range assertion
  298. if (index >= mNumProperties) {
  299. return false;
  300. }
  301. // Return false if the output data type does
  302. // not match the found value's data type
  303. if (GetAiType(value) != mValues[index].mType) {
  304. return false;
  305. }
  306. // Otherwise, output the found value and
  307. // return true
  308. value = *static_cast<T *>(mValues[index].mData);
  309. return true;
  310. }
  311. template <typename T>
  312. inline bool Get(const aiString &key, T &value) const {
  313. // Search for the given key
  314. for (unsigned int i = 0; i < mNumProperties; ++i) {
  315. if (mKeys[i] == key) {
  316. return Get(i, value);
  317. }
  318. }
  319. return false;
  320. }
  321. template <typename T>
  322. inline bool Get(const std::string &key, T &value) const {
  323. return Get(aiString(key), value);
  324. }
  325. /// Return metadata entry for analyzing it by user.
  326. /// \param [in] pIndex - index of the entry.
  327. /// \param [out] pKey - pointer to the key value.
  328. /// \param [out] pEntry - pointer to the entry: type and value.
  329. /// \return false - if pIndex is out of range, else - true.
  330. inline bool Get(size_t index, const aiString *&key, const aiMetadataEntry *&entry) const {
  331. if (index >= mNumProperties) {
  332. return false;
  333. }
  334. key = &mKeys[index];
  335. entry = &mValues[index];
  336. return true;
  337. }
  338. /// Check whether there is a metadata entry for the given key.
  339. /// \param [in] Key - the key value value to check for.
  340. inline bool HasKey(const char *key) {
  341. if (nullptr == key) {
  342. return false;
  343. }
  344. // Search for the given key
  345. for (unsigned int i = 0; i < mNumProperties; ++i) {
  346. if (0 == strncmp(mKeys[i].C_Str(), key, mKeys[i].length)) {
  347. return true;
  348. }
  349. }
  350. return false;
  351. }
  352. #endif // __cplusplus
  353. };
  354. #endif // AI_METADATA_H_INC