1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following 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
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF 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// ------------------------------------------------------------------------------------------------
52template <class T>
53inline 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// ------------------------------------------------------------------------------------------------
69template <class T>
70inline 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
86template <class T>
87inline 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// ------------------------------------------------------------------------------------------------
113template <class T>
114inline 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