miniassimp

view include/miniassimp/GenericProperty.h @ 0:879c81d94345

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Jan 2019 18:19:26 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2018, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
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.
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.
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.
40 ----------------------------------------------------------------------
41 */
43 #ifndef AI_GENERIC_PROPERTY_H_INCLUDED
44 #define AI_GENERIC_PROPERTY_H_INCLUDED
46 #include <miniassimp/Importer.hpp>
47 #include <miniassimp/ai_assert.h>
48 #include "Hash.h"
50 #include <map>
52 // ------------------------------------------------------------------------------------------------
53 template <class T>
54 inline
55 bool SetGenericProperty(std::map< unsigned int, T >& list,
56 const char* szName, const T& value) {
57 ai_assert(0 != szName);
58 const uint32_t hash = SuperFastHash(szName);
60 typename std::map<unsigned int, T>::iterator it = list.find(hash);
61 if (it == list.end()) {
62 list.insert(std::pair<unsigned int, T>( hash, value ));
63 return false;
64 }
65 (*it).second = value;
67 return true;
68 }
70 // ------------------------------------------------------------------------------------------------
71 template <class T>
72 inline
73 const T& GetGenericProperty(const std::map< unsigned int, T >& list,
74 const char* szName, const T& errorReturn) {
75 ai_assert(0 != szName);
76 const uint32_t hash = SuperFastHash(szName);
78 typename std::map<unsigned int, T>::const_iterator it = list.find(hash);
79 if (it == list.end()) {
80 return errorReturn;
81 }
83 return (*it).second;
84 }
86 // ------------------------------------------------------------------------------------------------
87 // Special version for pointer types - they will be deleted when replaced with another value
88 // passing NULL removes the whole property
89 template <class T>
90 inline
91 void SetGenericPropertyPtr(std::map< unsigned int, T* >& list,
92 const char* szName, T* value, bool* bWasExisting = 0 ) {
93 ai_assert(0 != szName);
94 const uint32_t hash = SuperFastHash(szName);
96 typename std::map<unsigned int, T*>::iterator it = list.find(hash);
97 if (it == list.end()) {
98 if (bWasExisting) {
99 *bWasExisting = false;
100 }
102 list.insert(std::pair<unsigned int,T*>( hash, value ));
103 return;
104 }
105 if ((*it).second != value) {
106 delete (*it).second;
107 (*it).second = value;
108 }
109 if (!value) {
110 list.erase(it);
111 }
112 if (bWasExisting) {
113 *bWasExisting = true;
114 }
115 }
117 // ------------------------------------------------------------------------------------------------
118 template <class T>
119 inline
120 bool HasGenericProperty(const std::map< unsigned int, T >& list,
121 const char* szName) {
122 ai_assert(0 != szName);
123 const uint32_t hash = SuperFastHash(szName);
125 typename std::map<unsigned int, T>::const_iterator it = list.find(hash);
126 if (it == list.end()) {
127 return false;
128 }
130 return true;
131 }
133 #endif // !! AI_GENERIC_PROPERTY_H_INCLUDED