Open Inventor Release 2024.1.1
 
Loading...
Searching...
No Matches
SbConstCharMap.h
1#if !defined(HIDDEN_FROM_DOC)
2#if !defined _SB_CONST_CHAR_MAP_H_
3#define _SB_CONST_CHAR_MAP_H_
4/*=======================================================================
5 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
6 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
7 *** ***
8 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
9 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
10 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
11 *** ***
12 *** RESTRICTED RIGHTS LEGEND ***
13 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
14 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
15 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
16 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
17 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
18 *** ***
19 *** COPYRIGHT (C) 1996-2020 BY FEI S.A.S, ***
20 *** BORDEAUX, FRANCE ***
21 *** ALL RIGHTS RESERVED ***
22**=======================================================================*/
23/*=======================================================================
24** Author : David Beilloin (Dec 2012)
25**=======================================================================*/
26#include <Inventor/STL/map>
27#include <Inventor/STL/functional>
28
34template <typename T>
35
36{
37public:
39 SbConstCharMap()
40 {}
41
43 ~SbConstCharMap()
44 { clear(); }
45
47 struct StrCompare {
48 bool operator() (const char* str1, const char* str2) const
49 {
50 if ( str1 == str2 )
51 return true;
52 return strcmp(str1, str2) < 0;
53 }
54 };
55
57 typedef typename std::map<const char*, T , StrCompare> UniformMap;
58 typedef typename UniformMap::value_type value_type;
59 typedef typename UniformMap::iterator iterator;
60 typedef typename UniformMap::const_iterator const_iterator;
61
63 const_iterator begin() const
64 { return m_uniforms.begin(); }
65
67 iterator begin()
68 { return m_uniforms.begin(); }
69
71 const_iterator end() const
72 { return m_uniforms.end(); }
73
75 iterator end()
76 { return m_uniforms.end(); }
77
79 bool empty() const
80 { return m_uniforms.empty(); }
81
83 size_t size() const
84 { return m_uniforms.size(); }
85
87 iterator find(const char* key)
88 { return m_uniforms.find(key); }
89
91 const_iterator find(const char* key) const
92 { return m_uniforms.find(key); }
93
95 iterator erase (iterator it)
96 {
97 free((void*)it->first);
98 return m_uniforms.erase(it);
99 }
100
102 void clear()
103 {
104 iterator it = m_uniforms.begin();
105 while(it!=m_uniforms.end())
106 {
107 free((void*)it->first);
108 ++it;
109 }
110 m_uniforms.clear();
111 }
112
114 T& operator[] ( const char* name )
115 {
116 iterator it = find(name);
117 if ( it==end())
118 {
119#ifdef _WIN32
120 return m_uniforms[_strdup(name)];
121#else
122 return m_uniforms[strdup(name)];
123#endif
124 }
125 else
126 return ((*it).second);
127 }
128
130 SbConstCharMap<T>& operator=(const SbConstCharMap<T>& copyFrom)
131 {
132 // first clear existing entry
133 clear();
134 // Then insert each element
135 iterator insertedIt = begin();
136 const_iterator it = copyFrom.begin();
137 while(it!=copyFrom.end())
138 {
139#ifdef _WIN32
140 insertedIt = m_uniforms.insert(insertedIt,value_type(_strdup((*it).first),(*it).second));
141#else
142 insertedIt = m_uniforms.insert(insertedIt,value_type(strdup((*it).first),(*it).second));
143#endif
144 ++it;
145 }
146 return *this;
147 }
148
150 friend int operator !=(const SbConstCharMap<T>& map1, const SbConstCharMap<T>& map2)
151 { return !(map1==map2); }
152
154 friend int operator ==(const SbConstCharMap<T>& map1, const SbConstCharMap<T>& map2)
155 {
156 if ( map1.size() != map2.size() )
157 return false;
158
159 const const_iterator itEnd1 = map1.end();
160 const_iterator it1 = map1.begin();
161 const_iterator it2 = map2.begin();
162 for (; it1 != itEnd1; ++it1, ++it2)
163 {
164 if ( it1->second != it2->second )
165 return false;
166
167 // ideally, we should do if ( StrCompare(map1, map2) || StrCompare(map2, map1) ) return false;
168 // but this would need 2 string comparison. Hard code strcmp for perf reason.
169 else if ( strcmp(it1->first, it2->first) != 0 )
170 return false;
171 }
172 return true;
173 }
174
175private:
176 // internal std::map<> storage
177 UniformMap m_uniforms;
178};
179
180#endif //_SB_CONST_CHAR_MAP_H_
181#endif // HIDDEN_FROM_DOC