1 | /* |
2 | Open Asset Import Library (assimp) |
3 | ---------------------------------------------------------------------- |
4 | |
5 | Copyright (c) 2006-2017, assimp team |
6 | |
7 | All rights reserved. |
8 | |
9 | Redistribution and use of this software in source and binary forms, |
10 | with or without modification, are permitted provided that the |
11 | following conditions are met: |
12 | |
13 | * Redistributions of source code must retain the above |
14 | copyright notice, this list of conditions and the |
15 | following disclaimer. |
16 | |
17 | * Redistributions in binary form must reproduce the above |
18 | copyright notice, this list of conditions and the |
19 | following disclaimer in the documentation and/or other |
20 | materials provided with the distribution. |
21 | |
22 | * Neither the name of the assimp team, nor the names of its |
23 | contributors may be used to endorse or promote products |
24 | derived from this software without specific prior |
25 | written permission of the assimp team. |
26 | |
27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | |
39 | ---------------------------------------------------------------------- |
40 | */ |
41 | |
42 | #ifndef AI_GENERIC_PROPERTY_H_INCLUDED |
43 | #define AI_GENERIC_PROPERTY_H_INCLUDED |
44 | |
45 | #include <assimp/Importer.hpp> |
46 | #include <assimp/ai_assert.h> |
47 | #include "Hash.h" |
48 | #include <map> |
49 | |
50 | |
51 | // ------------------------------------------------------------------------------------------------ |
52 | template <class T> |
53 | inline bool SetGenericProperty(std::map< unsigned int, T >& list, |
54 | const char* szName, const T& value) |
55 | { |
56 | ai_assert(NULL != szName); |
57 | const uint32_t hash = SuperFastHash(szName); |
58 | |
59 | typename std::map<unsigned int, T>::iterator it = list.find(hash); |
60 | if (it == list.end()) { |
61 | list.insert(std::pair<unsigned int, T>( hash, value )); |
62 | return false; |
63 | } |
64 | (*it).second = value; |
65 | return true; |
66 | } |
67 | |
68 | // ------------------------------------------------------------------------------------------------ |
69 | template <class T> |
70 | inline const T& GetGenericProperty(const std::map< unsigned int, T >& list, |
71 | const char* szName, const T& errorReturn) |
72 | { |
73 | ai_assert(NULL != szName); |
74 | const uint32_t hash = SuperFastHash(szName); |
75 | |
76 | typename std::map<unsigned int, T>::const_iterator it = list.find(hash); |
77 | if (it == list.end()) |
78 | return errorReturn; |
79 | |
80 | return (*it).second; |
81 | } |
82 | |
83 | // ------------------------------------------------------------------------------------------------ |
84 | // Special version for pointer types - they will be deleted when replaced with another value |
85 | // passing NULL removes the whole property |
86 | template <class T> |
87 | inline void SetGenericPropertyPtr(std::map< unsigned int, T* >& list, |
88 | const char* szName, T* value, bool* bWasExisting = NULL) |
89 | { |
90 | ai_assert(NULL != szName); |
91 | const uint32_t hash = SuperFastHash(szName); |
92 | |
93 | typename std::map<unsigned int, T*>::iterator it = list.find(hash); |
94 | if (it == list.end()) { |
95 | if (bWasExisting) |
96 | *bWasExisting = false; |
97 | |
98 | list.insert(std::pair<unsigned int,T*>( hash, value )); |
99 | return; |
100 | } |
101 | if ((*it).second != value) { |
102 | delete (*it).second; |
103 | (*it).second = value; |
104 | } |
105 | if (!value) { |
106 | list.erase(it); |
107 | } |
108 | if (bWasExisting) |
109 | *bWasExisting = true; |
110 | } |
111 | |
112 | // ------------------------------------------------------------------------------------------------ |
113 | template <class T> |
114 | inline bool HasGenericProperty(const std::map< unsigned int, T >& list, |
115 | const char* szName) |
116 | { |
117 | ai_assert(NULL != szName); |
118 | const uint32_t hash = SuperFastHash(szName); |
119 | |
120 | typename std::map<unsigned int, T>::const_iterator it = list.find(hash); |
121 | if (it == list.end()) return false; |
122 | |
123 | return true; |
124 | } |
125 | |
126 | #endif // !! AI_GENERIC_PROPERTY_H_INCLUDED |
127 | |