BRE12
metadata.h
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2012, assimp team
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
13 
14 * Redistributions of source code must retain the above
15  copyright notice, this list of conditions and the
16  following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the
20  following disclaimer in the documentation and/or other
21  materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24  contributors may be used to endorse or promote products
25  derived from this software without specific prior
26  written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
41 
45 #ifndef __AI_METADATA_H_INC__
46 #define __AI_METADATA_H_INC__
47 
48 #include <assert.h>
49 
50 #if defined(_MSC_VER) && (_MSC_VER <= 1500)
51 #include "Compiler/pstdint.h"
52 #else
53 #include <stdint.h>
54 #endif
55 
56 
57 
58 // -------------------------------------------------------------------------------
62  // -------------------------------------------------------------------------------
63 typedef enum aiMetadataType
64 {
65  AI_BOOL = 0,
66  AI_INT = 1,
67  AI_UINT64 = 2,
68  AI_FLOAT = 3,
69  AI_AISTRING = 4,
70  AI_AIVECTOR3D = 5,
71 
72 #ifndef SWIG
73  FORCE_32BIT = INT_MAX
74 #endif
76 
77 
78 
79 // -------------------------------------------------------------------------------
85  // -------------------------------------------------------------------------------
87 {
88  aiMetadataType mType;
89  void* mData;
90 };
91 
92 
93 
94 #ifdef __cplusplus
95 
96 #include <string>
97 
98 
99 
100 // -------------------------------------------------------------------------------
104  // -------------------------------------------------------------------------------
105 inline aiMetadataType GetAiType( bool ) { return AI_BOOL; }
106 inline aiMetadataType GetAiType( int ) { return AI_INT; }
107 inline aiMetadataType GetAiType( uint64_t ) { return AI_UINT64; }
108 inline aiMetadataType GetAiType( float ) { return AI_FLOAT; }
109 inline aiMetadataType GetAiType( aiString ) { return AI_AISTRING; }
110 inline aiMetadataType GetAiType( aiVector3D ) { return AI_AIVECTOR3D; }
111 
112 
113 
114 #endif
115 
116 
117 
118 // -------------------------------------------------------------------------------
124  // -------------------------------------------------------------------------------
125 struct aiMetadata
126 {
128  unsigned int mNumProperties;
129 
131  C_STRUCT aiString* mKeys;
132 
136 
137 #ifdef __cplusplus
138 
140  aiMetadata()
141  // set all members to zero by default
142  : mNumProperties(0)
143  , mKeys(NULL)
144  , mValues(NULL)
145  {}
146 
147 
149  ~aiMetadata()
150  {
151  delete[] mKeys;
152  mKeys = NULL;
153  if (mValues)
154  {
155  // Delete each metadata entry
156  for (unsigned i=0; i<mNumProperties; ++i)
157  {
158  void* data = mValues[i].mData;
159  switch (mValues[i].mType)
160  {
161  case AI_BOOL:
162  delete static_cast<bool*>(data);
163  break;
164  case AI_INT:
165  delete static_cast<int*>(data);
166  break;
167  case AI_UINT64:
168  delete static_cast<uint64_t*>(data);
169  break;
170  case AI_FLOAT:
171  delete static_cast<float*>(data);
172  break;
173  case AI_AISTRING:
174  delete static_cast<aiString*>(data);
175  break;
176  case AI_AIVECTOR3D:
177  delete static_cast<aiVector3D*>(data);
178  break;
179  default:
180  assert(false);
181  break;
182  }
183  }
184 
185  // Delete the metadata array
186  delete [] mValues;
187  mValues = NULL;
188  }
189  }
190 
191 
192 
193  template<typename T>
194  inline void Set( unsigned index, const std::string& key, const T& value )
195  {
196  // In range assertion
197  assert(index < mNumProperties);
198 
199  // Set metadata key
200  mKeys[index] = key;
201 
202  // Set metadata type
203  mValues[index].mType = GetAiType(value);
204  // Copy the given value to the dynamic storage
205  mValues[index].mData = new T(value);
206  }
207 
208  template<typename T>
209  inline bool Get( unsigned index, T& value )
210  {
211  // In range assertion
212  assert(index < mNumProperties);
213 
214  // Return false if the output data type does
215  // not match the found value's data type
216  if ( GetAiType( value ) != mValues[ index ].mType ) {
217  return false;
218  }
219 
220  // Otherwise, output the found value and
221  // return true
222  value = *static_cast<T*>(mValues[index].mData);
223  return true;
224  }
225 
226  template<typename T>
227  inline bool Get( const aiString& key, T& value )
228  {
229  // Search for the given key
230  for (unsigned i=0; i<mNumProperties; ++i)
231  if (mKeys[i]==key)
232  return Get(i, value);
233  return false;
234  }
235 
236  template<typename T>
237  inline bool Get( const std::string& key, T& value ) {
238  return Get(aiString(key), value);
239  }
240 
241 #endif // __cplusplus
242 
243 };
244 
245 #endif // __AI_METADATA_H_INC__
246 
247 
Metadata entry.
Definition: metadata.h:86
aiMetadataType
Enum used to distinguish data types.
Definition: metadata.h:63
Represents an UTF-8 string, zero byte terminated.
Definition: types.h:251
C_STRUCT aiMetadataEntry * mValues
Arrays of values, may not be NULL.
Definition: metadata.h:135
Definition: vector3.h:134
C_STRUCT aiString * mKeys
Arrays of keys, may not be NULL.
Definition: metadata.h:131
Container for holding metadata.
Definition: metadata.h:125
unsigned int mNumProperties
Length of the mKeys and mValues arrays, respectively.
Definition: metadata.h:128