Loading...
Searching...
No Matches
typeInfoMap.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_TYPE_INFO_MAP_H
25#define PXR_BASE_TF_TYPE_INFO_MAP_H
26
30
31#include "pxr/pxr.h"
32
33#include "pxr/base/tf/hash.h"
35
36#include "pxr/base/tf/hashmap.h"
37
38#include <typeinfo>
39#include <string>
40#include <list>
41
42PXR_NAMESPACE_OPEN_SCOPE
43
61template <class VALUE>
63 TfTypeInfoMap(const TfTypeInfoMap&) = delete;
64 TfTypeInfoMap& operator=(const TfTypeInfoMap&) = delete;
65public:
66
67 // Default constructor passes 0 to TfHashMap constructors to keep size
68 // small. This is good since each defined TfType has one of these maps in it.
69 TfTypeInfoMap() : _nameMap(0), _stringCache(0) {}
70
72 bool Exists(const std::type_info& key) const {
73 return Find(key) != NULL;
74 }
75
79 bool Exists(const std::string& key) const {
80 return Find(key) != NULL;
81 }
82
85 VALUE* Find(const std::type_info& key) const {
86 typename _TypeInfoCache::const_iterator i = _typeInfoCache.find(&key);
87 if (i != _typeInfoCache.end())
88 return &i->second->value;
89 else if (VALUE* v = Find(key.name())) {
90 return v;
91 }
92 return NULL;
93 }
94
100 template <class Upgrader>
101 VALUE* Find(const std::type_info& key, Upgrader& upgrader) {
102 typename _TypeInfoCache::const_iterator i = _typeInfoCache.find(&key);
103 if (i != _typeInfoCache.end())
104 return &i->second->value;
105 else if (VALUE* v = Find(key.name())) {
106 upgrader();
107 _CreateAlias(key, key.name());
108 return v;
109 }
110 return NULL;
111 }
112
117 VALUE* Find(const std::string& key) const {
118 typename _StringCache::const_iterator i = _stringCache.find(key);
119 return (i == _stringCache.end()) ? NULL : &i->second->value;
120 }
121
128 void Set(const std::type_info& key, const VALUE& value) {
129 if (VALUE* v = Find(key))
130 *v = value;
131 else {
132 Set(key.name(), value);
133 _CreateAlias(key, key.name());
134 }
135 }
136
142 void Set(const std::string& key, const VALUE& value) {
143 typename _StringCache::iterator i = _stringCache.find(key);
144
145 if (i != _stringCache.end())
146 i->second->value = value;
147 else {
148 _Entry* e = &_nameMap[key];
149 e->primaryKey = key;
150 e->value = value;
151
152 _stringCache[key] = e;
153 e->stringAliases.push_back(key);
154 }
155 }
156
164 bool CreateAlias(const std::string& alias, const std::string& key) {
165 typename _StringCache::iterator i = _stringCache.find(key);
166 if (i != _stringCache.end())
167 return (_CreateAlias(alias, i->second), true);
168 else
169 return false;
170 }
171
173 bool CreateAlias(const std::string& alias, const std::type_info& key) {
174 typename _TypeInfoCache::iterator i = _typeInfoCache.find(&key);
175 if (i != _typeInfoCache.end())
176 return (_CreateAlias(alias, i->second), true);
177 else
178 return false;
179 }
180
182 void Remove(const std::type_info& key) {
183 Remove(key.name());
184 }
185
187 void Remove(const std::string& key) {
188 typename _StringCache::iterator i = _stringCache.find(key);
189 if (i == _stringCache.end())
190 return;
191
192 _Entry* e = i->second;
193
194 for (TfIterator<_TypeInfoList> j = e->typeInfoAliases; j; ++j) {
195 _typeInfoCache.erase(*j);
196 }
197
198 for (TfIterator<std::list<std::string> > j = e->stringAliases; j; ++j) {
199 _stringCache.erase(*j);
200 }
201
202 // `e` points into the node owned by _nameMap. Passing
203 // e->primaryKey to erase() would cause the node to be
204 // deleted. However, the implementation of erase may access
205 // the key again after the node has been deleted (at least,
206 // when TfHashMap is __gnu_cxx::hash_map.)
207 const std::string primaryKey = std::move(e->primaryKey);
208 _nameMap.erase(primaryKey);
209 }
210
211private:
212 typedef std::list<const std::type_info*> _TypeInfoList;
213
214 struct _Entry {
215 mutable _TypeInfoList typeInfoAliases;
216 mutable std::list<std::string> stringAliases;
217 std::string primaryKey;
218 VALUE value;
219 };
220
221 void _CreateAlias(const std::type_info& alias, const std::string& key) {
222 typename _StringCache::iterator i = _stringCache.find(key);
223 if (i != _stringCache.end())
224 _CreateAlias(alias, i->second);
225 }
226
227 void _CreateAlias(const std::type_info& alias, _Entry* e) {
228 if (_typeInfoCache.find(&alias) == _typeInfoCache.end()) {
229 _typeInfoCache[&alias] = e;
230 e->typeInfoAliases.push_back(&alias);
231 }
232 }
233
234 void _CreateAlias(const std::string& alias, _Entry* e) {
235 if (_stringCache.find(alias) == _stringCache.end()) {
236 _stringCache[alias] = e;
237 e->stringAliases.push_back(alias);
238 }
239 }
240
241 typedef TfHashMap<std::string, _Entry, TfHash> _NameMap;
242 typedef TfHashMap<const std::type_info*, _Entry*, TfHash>
243 _TypeInfoCache;
244 typedef TfHashMap<std::string, _Entry*, TfHash> _StringCache;
245
246 _NameMap _nameMap;
247
248 _TypeInfoCache _typeInfoCache;
249 _StringCache _stringCache;
250};
251
252PXR_NAMESPACE_CLOSE_SCOPE
253
254#endif // PXR_BASE_TF_TYPE_INFO_MAP_H
A simple iterator adapter for STL containers.
A simple iterator adapter for STL containers.
Definition: iterator.h:176
A map whose key is a const std::type_info&, or a string alias.
Definition: typeInfoMap.h:62
void Set(const std::type_info &key, const VALUE &value)
Set the value for a given key.
Definition: typeInfoMap.h:128
void Remove(const std::type_info &key)
Remove this key (and any aliases associated with it).
Definition: typeInfoMap.h:182
bool Exists(const std::type_info &key) const
Return true if the given key is present in the map.
Definition: typeInfoMap.h:72
void Remove(const std::string &key)
Remove this key (and any aliases associated with it).
Definition: typeInfoMap.h:187
VALUE * Find(const std::string &key) const
Return a pointer to the value stored under key, and NULL if key is not a key in the map.
Definition: typeInfoMap.h:117
void Set(const std::string &key, const VALUE &value)
Set the value for a given key.
Definition: typeInfoMap.h:142
VALUE * Find(const std::type_info &key) const
Return a pointer to the value stored under key, and NULL if key is not a key in the map.
Definition: typeInfoMap.h:85
VALUE * Find(const std::type_info &key, Upgrader &upgrader)
Return a pointer to the value stored under key, and NULL if key is not a key in the map.
Definition: typeInfoMap.h:101
bool CreateAlias(const std::string &alias, const std::type_info &key)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: typeInfoMap.h:173
bool Exists(const std::string &key) const
Return true if the given key is present in the map.
Definition: typeInfoMap.h:79
bool CreateAlias(const std::string &alias, const std::string &key)
Create an alias for a key.
Definition: typeInfoMap.h:164