miniassimp
diff include/miniassimp/metadata.h @ 0:879c81d94345
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 28 Jan 2019 18:19:26 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/include/miniassimp/metadata.h Mon Jan 28 18:19:26 2019 +0200 1.3 @@ -0,0 +1,380 @@ 1.4 +/* 1.5 +--------------------------------------------------------------------------- 1.6 +Open Asset Import Library (assimp) 1.7 +--------------------------------------------------------------------------- 1.8 + 1.9 +Copyright (c) 2006-2018, assimp team 1.10 + 1.11 + 1.12 + 1.13 +All rights reserved. 1.14 + 1.15 +Redistribution and use of this software in source and binary forms, 1.16 +with or without modification, are permitted provided that the following 1.17 +conditions are met: 1.18 + 1.19 +* Redistributions of source code must retain the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer. 1.22 + 1.23 +* Redistributions in binary form must reproduce the above 1.24 + copyright notice, this list of conditions and the 1.25 + following disclaimer in the documentation and/or other 1.26 + materials provided with the distribution. 1.27 + 1.28 +* Neither the name of the assimp team, nor the names of its 1.29 + contributors may be used to endorse or promote products 1.30 + derived from this software without specific prior 1.31 + written permission of the assimp team. 1.32 + 1.33 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.34 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.35 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.36 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.37 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.38 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.39 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.40 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.41 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.42 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.43 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.44 +--------------------------------------------------------------------------- 1.45 +*/ 1.46 + 1.47 +/** @file metadata.h 1.48 + * @brief Defines the data structures for holding node meta information. 1.49 + */ 1.50 +#pragma once 1.51 +#ifndef AI_METADATA_H_INC 1.52 +#define AI_METADATA_H_INC 1.53 + 1.54 +#if defined(_MSC_VER) && (_MSC_VER <= 1500) 1.55 +# include "Compiler/pstdint.h" 1.56 +#else 1.57 +# include <inttypes.h> 1.58 +#endif 1.59 + 1.60 +// ------------------------------------------------------------------------------- 1.61 +/** 1.62 + * Enum used to distinguish data types 1.63 + */ 1.64 + // ------------------------------------------------------------------------------- 1.65 +typedef enum aiMetadataType { 1.66 + AI_BOOL = 0, 1.67 + AI_INT32 = 1, 1.68 + AI_UINT64 = 2, 1.69 + AI_FLOAT = 3, 1.70 + AI_DOUBLE = 4, 1.71 + AI_AISTRING = 5, 1.72 + AI_AIVECTOR3D = 6, 1.73 + AI_META_MAX = 7, 1.74 + 1.75 +#ifndef SWIG 1.76 + FORCE_32BIT = INT_MAX 1.77 +#endif 1.78 +} aiMetadataType; 1.79 + 1.80 +// ------------------------------------------------------------------------------- 1.81 +/** 1.82 + * Metadata entry 1.83 + * 1.84 + * The type field uniquely identifies the underlying type of the data field 1.85 + */ 1.86 + // ------------------------------------------------------------------------------- 1.87 +struct aiMetadataEntry { 1.88 + aiMetadataType mType; 1.89 + void* mData; 1.90 +}; 1.91 + 1.92 +#ifdef __cplusplus 1.93 + 1.94 +#include <string> 1.95 + 1.96 +// ------------------------------------------------------------------------------- 1.97 +/** 1.98 + * Helper functions to get the aiType enum entry for a type 1.99 + */ 1.100 + // ------------------------------------------------------------------------------- 1.101 + 1.102 +inline aiMetadataType GetAiType( bool ) { return AI_BOOL; } 1.103 +inline aiMetadataType GetAiType( int32_t ) { return AI_INT32; } 1.104 +inline aiMetadataType GetAiType( uint64_t ) { return AI_UINT64; } 1.105 +inline aiMetadataType GetAiType( float ) { return AI_FLOAT; } 1.106 +inline aiMetadataType GetAiType( double ) { return AI_DOUBLE; } 1.107 +inline aiMetadataType GetAiType( const aiString & ) { return AI_AISTRING; } 1.108 +inline aiMetadataType GetAiType( const aiVector3D & ) { return AI_AIVECTOR3D; } 1.109 + 1.110 +#endif // __cplusplus 1.111 + 1.112 +// ------------------------------------------------------------------------------- 1.113 +/** 1.114 + * Container for holding metadata. 1.115 + * 1.116 + * Metadata is a key-value store using string keys and values. 1.117 + */ 1.118 + // ------------------------------------------------------------------------------- 1.119 +struct aiMetadata { 1.120 + /** Length of the mKeys and mValues arrays, respectively */ 1.121 + unsigned int mNumProperties; 1.122 + 1.123 + /** Arrays of keys, may not be NULL. Entries in this array may not be NULL as well. */ 1.124 + C_STRUCT aiString* mKeys; 1.125 + 1.126 + /** Arrays of values, may not be NULL. Entries in this array may be NULL if the 1.127 + * corresponding property key has no assigned value. */ 1.128 + C_STRUCT aiMetadataEntry* mValues; 1.129 + 1.130 +#ifdef __cplusplus 1.131 + 1.132 + /** 1.133 + * @brief The default constructor, set all members to zero by default. 1.134 + */ 1.135 + aiMetadata() AI_NO_EXCEPT 1.136 + : mNumProperties(0) 1.137 + , mKeys(0) 1.138 + , mValues(0) { 1.139 + // empty 1.140 + } 1.141 + 1.142 + aiMetadata( const aiMetadata &rhs ) 1.143 + : mNumProperties( rhs.mNumProperties ) 1.144 + , mKeys( 0 ) 1.145 + , mValues( 0 ) { 1.146 + mKeys = new aiString[ mNumProperties ]; 1.147 + for ( size_t i = 0; i < static_cast<size_t>( mNumProperties ); ++i ) { 1.148 + mKeys[ i ] = rhs.mKeys[ i ]; 1.149 + } 1.150 + mValues = new aiMetadataEntry[ mNumProperties ]; 1.151 + for ( size_t i = 0; i < static_cast<size_t>(mNumProperties); ++i ) { 1.152 + mValues[ i ].mType = rhs.mValues[ i ].mType; 1.153 + switch ( rhs.mValues[ i ].mType ) { 1.154 + case AI_BOOL: 1.155 + mValues[ i ].mData = new bool; 1.156 + ::memcpy( mValues[ i ].mData, rhs.mValues[ i ].mData, sizeof(bool) ); 1.157 + break; 1.158 + case AI_INT32: { 1.159 + int32_t v; 1.160 + ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( int32_t ) ); 1.161 + mValues[ i ].mData = new int32_t( v ); 1.162 + } 1.163 + break; 1.164 + case AI_UINT64: { 1.165 + uint64_t v; 1.166 + ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( uint64_t ) ); 1.167 + mValues[ i ].mData = new uint64_t( v ); 1.168 + } 1.169 + break; 1.170 + case AI_FLOAT: { 1.171 + float v; 1.172 + ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( float ) ); 1.173 + mValues[ i ].mData = new float( v ); 1.174 + } 1.175 + break; 1.176 + case AI_DOUBLE: { 1.177 + double v; 1.178 + ::memcpy( &v, rhs.mValues[ i ].mData, sizeof( double ) ); 1.179 + mValues[ i ].mData = new double( v ); 1.180 + } 1.181 + break; 1.182 + case AI_AISTRING: { 1.183 + aiString v; 1.184 + rhs.Get<aiString>( mKeys[ i ], v ); 1.185 + mValues[ i ].mData = new aiString( v ); 1.186 + } 1.187 + break; 1.188 + case AI_AIVECTOR3D: { 1.189 + aiVector3D v; 1.190 + rhs.Get<aiVector3D>( mKeys[ i ], v ); 1.191 + mValues[ i ].mData = new aiVector3D( v ); 1.192 + } 1.193 + break; 1.194 +#ifndef SWIG 1.195 + case FORCE_32BIT: 1.196 +#endif 1.197 + default: 1.198 + break; 1.199 + } 1.200 + 1.201 + } 1.202 + } 1.203 + 1.204 + /** 1.205 + * @brief The destructor. 1.206 + */ 1.207 + ~aiMetadata() { 1.208 + delete [] mKeys; 1.209 + mKeys = 0; 1.210 + if (mValues) { 1.211 + // Delete each metadata entry 1.212 + for (unsigned i=0; i<mNumProperties; ++i) { 1.213 + void* data = mValues[i].mData; 1.214 + switch (mValues[i].mType) { 1.215 + case AI_BOOL: 1.216 + delete static_cast< bool* >( data ); 1.217 + break; 1.218 + case AI_INT32: 1.219 + delete static_cast< int32_t* >( data ); 1.220 + break; 1.221 + case AI_UINT64: 1.222 + delete static_cast< uint64_t* >( data ); 1.223 + break; 1.224 + case AI_FLOAT: 1.225 + delete static_cast< float* >( data ); 1.226 + break; 1.227 + case AI_DOUBLE: 1.228 + delete static_cast< double* >( data ); 1.229 + break; 1.230 + case AI_AISTRING: 1.231 + delete static_cast< aiString* >( data ); 1.232 + break; 1.233 + case AI_AIVECTOR3D: 1.234 + delete static_cast< aiVector3D* >( data ); 1.235 + break; 1.236 +#ifndef SWIG 1.237 + case FORCE_32BIT: 1.238 +#endif 1.239 + default: 1.240 + break; 1.241 + } 1.242 + } 1.243 + 1.244 + // Delete the metadata array 1.245 + delete [] mValues; 1.246 + mValues = 0; 1.247 + } 1.248 + } 1.249 + 1.250 + /** 1.251 + * @brief Allocates property fields + keys. 1.252 + * @param numProperties Number of requested properties. 1.253 + */ 1.254 + static inline 1.255 + aiMetadata *Alloc( unsigned int numProperties ) { 1.256 + if ( 0 == numProperties ) { 1.257 + return 0; 1.258 + } 1.259 + 1.260 + aiMetadata *data = new aiMetadata; 1.261 + data->mNumProperties = numProperties; 1.262 + data->mKeys = new aiString[ data->mNumProperties ](); 1.263 + data->mValues = new aiMetadataEntry[ data->mNumProperties ](); 1.264 + 1.265 + return data; 1.266 + } 1.267 + 1.268 + /** 1.269 + * @brief Deallocates property fields + keys. 1.270 + */ 1.271 + static inline 1.272 + void Dealloc( aiMetadata *metadata ) { 1.273 + delete metadata; 1.274 + } 1.275 + 1.276 + template<typename T> 1.277 + inline 1.278 + void Add(const std::string& key, const T& value) { 1.279 + aiString* new_keys = new aiString[mNumProperties + 1]; 1.280 + aiMetadataEntry* new_values = new aiMetadataEntry[mNumProperties + 1]; 1.281 + 1.282 + for(unsigned int i = 0; i < mNumProperties; ++i) 1.283 + { 1.284 + new_keys[i] = mKeys[i]; 1.285 + new_values[i] = mValues[i]; 1.286 + } 1.287 + 1.288 + delete mKeys; 1.289 + delete mValues; 1.290 + 1.291 + mKeys = new_keys; 1.292 + mValues = new_values; 1.293 + 1.294 + mNumProperties++; 1.295 + 1.296 + Set(mNumProperties - 1, key, value); 1.297 + } 1.298 + 1.299 + template<typename T> 1.300 + inline 1.301 + bool Set( unsigned index, const std::string& key, const T& value ) { 1.302 + // In range assertion 1.303 + if ( index >= mNumProperties ) { 1.304 + return false; 1.305 + } 1.306 + 1.307 + // Ensure that we have a valid key. 1.308 + if ( key.empty() ) { 1.309 + return false; 1.310 + } 1.311 + 1.312 + // Set metadata key 1.313 + mKeys[index] = key; 1.314 + 1.315 + // Set metadata type 1.316 + mValues[index].mType = GetAiType(value); 1.317 + // Copy the given value to the dynamic storage 1.318 + mValues[index].mData = new T(value); 1.319 + 1.320 + return true; 1.321 + } 1.322 + 1.323 + template<typename T> 1.324 + inline 1.325 + bool Get( unsigned index, T& value ) const { 1.326 + // In range assertion 1.327 + if ( index >= mNumProperties ) { 1.328 + return false; 1.329 + } 1.330 + 1.331 + // Return false if the output data type does 1.332 + // not match the found value's data type 1.333 + if ( GetAiType( value ) != mValues[ index ].mType ) { 1.334 + return false; 1.335 + } 1.336 + 1.337 + // Otherwise, output the found value and 1.338 + // return true 1.339 + value = *static_cast<T*>(mValues[index].mData); 1.340 + 1.341 + return true; 1.342 + } 1.343 + 1.344 + template<typename T> 1.345 + inline 1.346 + bool Get( const aiString& key, T& value ) const { 1.347 + // Search for the given key 1.348 + for ( unsigned int i = 0; i < mNumProperties; ++i ) { 1.349 + if ( mKeys[ i ] == key ) { 1.350 + return Get( i, value ); 1.351 + } 1.352 + } 1.353 + return false; 1.354 + } 1.355 + 1.356 + template<typename T> 1.357 + inline 1.358 + bool Get( const std::string& key, T& value ) const { 1.359 + return Get(aiString(key), value); 1.360 + } 1.361 + 1.362 + /// Return metadata entry for analyzing it by user. 1.363 + /// \param [in] pIndex - index of the entry. 1.364 + /// \param [out] pKey - pointer to the key value. 1.365 + /// \param [out] pEntry - pointer to the entry: type and value. 1.366 + /// \return false - if pIndex is out of range, else - true. 1.367 + inline 1.368 + bool Get(size_t index, const aiString*& key, const aiMetadataEntry*& entry) const { 1.369 + if ( index >= mNumProperties ) { 1.370 + return false; 1.371 + } 1.372 + 1.373 + key = &mKeys[index]; 1.374 + entry = &mValues[index]; 1.375 + 1.376 + return true; 1.377 + } 1.378 + 1.379 +#endif // __cplusplus 1.380 + 1.381 +}; 1.382 + 1.383 +#endif // AI_METADATA_H_INC