metadata.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, 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_AIMETADATA = 7,
  62. AI_META_MAX = 8,
  63. #ifndef SWIG
  64. FORCE_32BIT = INT_MAX
  65. #endif
  66. } aiMetadataType;
  67. // -------------------------------------------------------------------------------
  68. /**
  69. * Metadata entry
  70. *
  71. * The type field uniquely identifies the underlying type of the data field
  72. */
  73. // -------------------------------------------------------------------------------
  74. struct aiMetadataEntry {
  75. aiMetadataType mType;
  76. void *mData;
  77. #ifdef __cplusplus
  78. aiMetadataEntry() :
  79. mType(AI_META_MAX),
  80. mData( nullptr ) {
  81. // empty
  82. }
  83. #endif
  84. };
  85. #ifdef __cplusplus
  86. #include <string>
  87. struct aiMetadata;
  88. // -------------------------------------------------------------------------------
  89. /**
  90. * Helper functions to get the aiType enum entry for a type
  91. */
  92. // -------------------------------------------------------------------------------
  93. inline aiMetadataType GetAiType(bool) {
  94. return AI_BOOL;
  95. }
  96. inline aiMetadataType GetAiType(int32_t) {
  97. return AI_INT32;
  98. }
  99. inline aiMetadataType GetAiType(uint64_t) {
  100. return AI_UINT64;
  101. }
  102. inline aiMetadataType GetAiType(float) {
  103. return AI_FLOAT;
  104. }
  105. inline aiMetadataType GetAiType(double) {
  106. return AI_DOUBLE;
  107. }
  108. inline aiMetadataType GetAiType(const aiString &) {
  109. return AI_AISTRING;
  110. }
  111. inline aiMetadataType GetAiType(const aiVector3D &) {
  112. return AI_AIVECTOR3D;
  113. }
  114. inline aiMetadataType GetAiType(const aiMetadata &) {
  115. return AI_AIMETADATA;
  116. }
  117. #endif // __cplusplus
  118. // -------------------------------------------------------------------------------
  119. /**
  120. * Container for holding metadata.
  121. *
  122. * Metadata is a key-value store using string keys and values.
  123. */
  124. // -------------------------------------------------------------------------------
  125. struct aiMetadata {
  126. /** Length of the mKeys and mValues arrays, respectively */
  127. unsigned int mNumProperties;
  128. /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */
  129. C_STRUCT aiString *mKeys;
  130. /** Arrays of values, may not be NULL. Entries in this array may be NULL if the
  131. * corresponding property key has no assigned value. */
  132. C_STRUCT aiMetadataEntry *mValues;
  133. #ifdef __cplusplus
  134. /**
  135. * @brief The default constructor, set all members to zero by default.
  136. */
  137. aiMetadata() AI_NO_EXCEPT
  138. : mNumProperties(0),
  139. mKeys(nullptr),
  140. mValues(nullptr) {
  141. // empty
  142. }
  143. aiMetadata(const aiMetadata &rhs) :
  144. mNumProperties(rhs.mNumProperties), mKeys(nullptr), mValues(nullptr) {
  145. mKeys = new aiString[mNumProperties];
  146. for (size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i) {
  147. mKeys[i] = rhs.mKeys[i];
  148. }
  149. mValues = new aiMetadataEntry[mNumProperties];
  150. for (size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i) {
  151. mValues[i].mType = rhs.mValues[i].mType;
  152. switch (rhs.mValues[i].mType) {
  153. case AI_BOOL:
  154. mValues[i].mData = new bool;
  155. ::memcpy(mValues[i].mData, rhs.mValues[i].mData, sizeof(bool));
  156. break;
  157. case AI_INT32: {
  158. int32_t v;
  159. ::memcpy(&v, rhs.mValues[i].mData, sizeof(int32_t));
  160. mValues[i].mData = new int32_t(v);
  161. } break;
  162. case AI_UINT64: {
  163. uint64_t v;
  164. ::memcpy(&v, rhs.mValues[i].mData, sizeof(uint64_t));
  165. mValues[i].mData = new uint64_t(v);
  166. } break;
  167. case AI_FLOAT: {
  168. float v;
  169. ::memcpy(&v, rhs.mValues[i].mData, sizeof(float));
  170. mValues[i].mData = new float(v);
  171. } break;
  172. case AI_DOUBLE: {
  173. double v;
  174. ::memcpy(&v, rhs.mValues[i].mData, sizeof(double));
  175. mValues[i].mData = new double(v);
  176. } break;
  177. case AI_AISTRING: {
  178. aiString v;
  179. rhs.Get<aiString>(static_cast<unsigned int>(i), v);
  180. mValues[i].mData = new aiString(v);
  181. } break;
  182. case AI_AIVECTOR3D: {
  183. aiVector3D v;
  184. rhs.Get<aiVector3D>(static_cast<unsigned int>(i), v);
  185. mValues[i].mData = new aiVector3D(v);
  186. } break;
  187. case AI_AIMETADATA: {
  188. aiMetadata v;
  189. rhs.Get<aiMetadata>(static_cast<unsigned int>(i), v);
  190. mValues[i].mData = new aiMetadata(v);
  191. } break;
  192. #ifndef SWIG
  193. case FORCE_32BIT:
  194. #endif
  195. default:
  196. break;
  197. }
  198. }
  199. }
  200. aiMetadata &operator=(aiMetadata rhs) {
  201. using std::swap;
  202. swap(mNumProperties, rhs.mNumProperties);
  203. swap(mKeys, rhs.mKeys);
  204. swap(mValues, rhs.mValues);
  205. return *this;
  206. }
  207. /**
  208. * @brief The destructor.
  209. */
  210. ~aiMetadata() {
  211. delete[] mKeys;
  212. mKeys = nullptr;
  213. if (mValues) {
  214. // Delete each metadata entry
  215. for (unsigned i = 0; i < mNumProperties; ++i) {
  216. void *data = mValues[i].mData;
  217. switch (mValues[i].mType) {
  218. case AI_BOOL:
  219. delete static_cast<bool *>(data);
  220. break;
  221. case AI_INT32:
  222. delete static_cast<int32_t *>(data);
  223. break;
  224. case AI_UINT64:
  225. delete static_cast<uint64_t *>(data);
  226. break;
  227. case AI_FLOAT:
  228. delete static_cast<float *>(data);
  229. break;
  230. case AI_DOUBLE:
  231. delete static_cast<double *>(data);
  232. break;
  233. case AI_AISTRING:
  234. delete static_cast<aiString *>(data);
  235. break;
  236. case AI_AIVECTOR3D:
  237. delete static_cast<aiVector3D *>(data);
  238. break;
  239. case AI_AIMETADATA:
  240. delete static_cast<aiMetadata *>(data);
  241. break;
  242. #ifndef SWIG
  243. case FORCE_32BIT:
  244. #endif
  245. default:
  246. break;
  247. }
  248. }
  249. // Delete the metadata array
  250. delete[] mValues;
  251. mValues = nullptr;
  252. }
  253. }
  254. /**
  255. * @brief Allocates property fields + keys.
  256. * @param numProperties Number of requested properties.
  257. */
  258. static inline aiMetadata *Alloc(unsigned int numProperties) {
  259. if (0 == numProperties) {
  260. return nullptr;
  261. }
  262. aiMetadata *data = new aiMetadata;
  263. data->mNumProperties = numProperties;
  264. data->mKeys = new aiString[data->mNumProperties]();
  265. data->mValues = new aiMetadataEntry[data->mNumProperties]();
  266. return data;
  267. }
  268. /**
  269. * @brief Deallocates property fields + keys.
  270. */
  271. static inline void Dealloc(aiMetadata *metadata) {
  272. delete metadata;
  273. }
  274. template <typename T>
  275. inline void Add(const std::string &key, const T &value) {
  276. aiString *new_keys = new aiString[mNumProperties + 1];
  277. aiMetadataEntry *new_values = new aiMetadataEntry[mNumProperties + 1];
  278. for (unsigned int i = 0; i < mNumProperties; ++i) {
  279. new_keys[i] = mKeys[i];
  280. new_values[i] = mValues[i];
  281. }
  282. delete[] mKeys;
  283. delete[] mValues;
  284. mKeys = new_keys;
  285. mValues = new_values;
  286. mNumProperties++;
  287. Set(mNumProperties - 1, key, value);
  288. }
  289. template <typename T>
  290. inline bool Set(unsigned index, const std::string &key, const T &value) {
  291. // In range assertion
  292. if (index >= mNumProperties) {
  293. return false;
  294. }
  295. // Ensure that we have a valid key.
  296. if (key.empty()) {
  297. return false;
  298. }
  299. // Set metadata key
  300. mKeys[index] = key;
  301. // Set metadata type
  302. mValues[index].mType = GetAiType(value);
  303. // Copy the given value to the dynamic storage
  304. if (nullptr != mValues[index].mData && AI_AIMETADATA != mValues[index].mType) {
  305. ::memcpy(mValues[index].mData, &value, sizeof(T));
  306. } else if (nullptr != mValues[index].mData && AI_AIMETADATA == mValues[index].mType) {
  307. *static_cast<T *>(mValues[index].mData) = value;
  308. } else {
  309. if (nullptr != mValues[index].mData) {
  310. delete static_cast<T *>(mValues[index].mData);
  311. mValues[index].mData = nullptr;
  312. }
  313. mValues[index].mData = new T(value);
  314. }
  315. return true;
  316. }
  317. template <typename T>
  318. inline bool Set(const std::string &key, const T &value) {
  319. if (key.empty()) {
  320. return false;
  321. }
  322. bool result = false;
  323. for (unsigned int i = 0; i < mNumProperties; ++i) {
  324. if (key == mKeys[i].C_Str()) {
  325. Set(i, key, value);
  326. result = true;
  327. break;
  328. }
  329. }
  330. return result;
  331. }
  332. template <typename T>
  333. inline bool Get(unsigned index, T &value) const {
  334. // In range assertion
  335. if (index >= mNumProperties) {
  336. return false;
  337. }
  338. // Return false if the output data type does
  339. // not match the found value's data type
  340. if (GetAiType(value) != mValues[index].mType) {
  341. return false;
  342. }
  343. // Otherwise, output the found value and
  344. // return true
  345. value = *static_cast<T *>(mValues[index].mData);
  346. return true;
  347. }
  348. template <typename T>
  349. inline bool Get(const aiString &key, T &value) const {
  350. // Search for the given key
  351. for (unsigned int i = 0; i < mNumProperties; ++i) {
  352. if (mKeys[i] == key) {
  353. return Get(i, value);
  354. }
  355. }
  356. return false;
  357. }
  358. template <typename T>
  359. inline bool Get(const std::string &key, T &value) const {
  360. return Get(aiString(key), value);
  361. }
  362. /// Return metadata entry for analyzing it by user.
  363. /// \param [in] pIndex - index of the entry.
  364. /// \param [out] pKey - pointer to the key value.
  365. /// \param [out] pEntry - pointer to the entry: type and value.
  366. /// \return false - if pIndex is out of range, else - true.
  367. inline bool Get(size_t index, const aiString *&key, const aiMetadataEntry *&entry) const {
  368. if (index >= mNumProperties) {
  369. return false;
  370. }
  371. key = &mKeys[index];
  372. entry = &mValues[index];
  373. return true;
  374. }
  375. /// Check whether there is a metadata entry for the given key.
  376. /// \param [in] Key - the key value value to check for.
  377. inline bool HasKey(const char *key) const {
  378. if (nullptr == key) {
  379. return false;
  380. }
  381. // Search for the given key
  382. for (unsigned int i = 0; i < mNumProperties; ++i) {
  383. if (0 == strncmp(mKeys[i].C_Str(), key, mKeys[i].length)) {
  384. return true;
  385. }
  386. }
  387. return false;
  388. }
  389. friend bool CompareKeys(const aiMetadata &lhs, const aiMetadata &rhs) {
  390. if (lhs.mNumProperties != rhs.mNumProperties) {
  391. return false;
  392. }
  393. for (unsigned int i = 0; i < lhs.mNumProperties; ++i) {
  394. if (lhs.mKeys[i] != rhs.mKeys[i]) {
  395. return false;
  396. }
  397. }
  398. return true;
  399. }
  400. friend bool CompareValues(const aiMetadata &lhs, const aiMetadata &rhs) {
  401. if (lhs.mNumProperties != rhs.mNumProperties) {
  402. return false;
  403. }
  404. for (unsigned int i = 0; i < lhs.mNumProperties; ++i) {
  405. if (lhs.mValues[i].mType != rhs.mValues[i].mType) {
  406. return false;
  407. }
  408. switch (lhs.mValues[i].mType) {
  409. case AI_BOOL: {
  410. if (*static_cast<bool *>(lhs.mValues[i].mData) != *static_cast<bool *>(rhs.mValues[i].mData)) {
  411. return false;
  412. }
  413. } break;
  414. case AI_INT32: {
  415. if (*static_cast<int32_t *>(lhs.mValues[i].mData) != *static_cast<int32_t *>(rhs.mValues[i].mData)) {
  416. return false;
  417. }
  418. } break;
  419. case AI_UINT64: {
  420. if (*static_cast<uint64_t *>(lhs.mValues[i].mData) != *static_cast<uint64_t *>(rhs.mValues[i].mData)) {
  421. return false;
  422. }
  423. } break;
  424. case AI_FLOAT: {
  425. if (*static_cast<float *>(lhs.mValues[i].mData) != *static_cast<float *>(rhs.mValues[i].mData)) {
  426. return false;
  427. }
  428. } break;
  429. case AI_DOUBLE: {
  430. if (*static_cast<double *>(lhs.mValues[i].mData) != *static_cast<double *>(rhs.mValues[i].mData)) {
  431. return false;
  432. }
  433. } break;
  434. case AI_AISTRING: {
  435. if (*static_cast<aiString *>(lhs.mValues[i].mData) != *static_cast<aiString *>(rhs.mValues[i].mData)) {
  436. return false;
  437. }
  438. } break;
  439. case AI_AIVECTOR3D: {
  440. if (*static_cast<aiVector3D *>(lhs.mValues[i].mData) != *static_cast<aiVector3D *>(rhs.mValues[i].mData)) {
  441. return false;
  442. }
  443. } break;
  444. case AI_AIMETADATA: {
  445. if (*static_cast<aiMetadata *>(lhs.mValues[i].mData) != *static_cast<aiMetadata *>(rhs.mValues[i].mData)) {
  446. return false;
  447. }
  448. } break;
  449. #ifndef SWIG
  450. case FORCE_32BIT:
  451. #endif
  452. default:
  453. break;
  454. }
  455. }
  456. return true;
  457. }
  458. friend bool operator==(const aiMetadata &lhs, const aiMetadata &rhs) {
  459. return CompareKeys(lhs, rhs) && CompareValues(lhs, rhs);
  460. }
  461. friend bool operator!=(const aiMetadata &lhs, const aiMetadata &rhs) {
  462. return !(lhs == rhs);
  463. }
  464. #endif // __cplusplus
  465. };
  466. #endif // AI_METADATA_H_INC