All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
typeInfoMap.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_TYPE_INFO_MAP_H
8#define PXR_BASE_TF_TYPE_INFO_MAP_H
9
13
14#include "pxr/pxr.h"
15
16#include "pxr/base/tf/hash.h"
18
19#include "pxr/base/tf/hashmap.h"
20
21#include <typeinfo>
22#include <string>
23#include <list>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
44template <class VALUE>
46 TfTypeInfoMap(const TfTypeInfoMap&) = delete;
47 TfTypeInfoMap& operator=(const TfTypeInfoMap&) = delete;
48public:
49
50 // Default constructor passes 0 to TfHashMap constructors to keep size
51 // small. This is good since each defined TfType has one of these maps in it.
52 TfTypeInfoMap() : _nameMap(0), _stringCache(0) {}
53
55 bool Exists(const std::type_info& key) const {
56 return Find(key) != NULL;
57 }
58
62 bool Exists(const std::string& key) const {
63 return Find(key) != NULL;
64 }
65
68 VALUE* Find(const std::type_info& key) const {
69 typename _TypeInfoCache::const_iterator i = _typeInfoCache.find(&key);
70 if (i != _typeInfoCache.end())
71 return &i->second->value;
72 else if (VALUE* v = Find(key.name())) {
73 return v;
74 }
75 return NULL;
76 }
77
83 template <class Upgrader>
84 VALUE* Find(const std::type_info& key, Upgrader& upgrader) {
85 typename _TypeInfoCache::const_iterator i = _typeInfoCache.find(&key);
86 if (i != _typeInfoCache.end())
87 return &i->second->value;
88 else if (VALUE* v = Find(key.name())) {
89 upgrader();
90 _CreateAlias(key, key.name());
91 return v;
92 }
93 return NULL;
94 }
95
100 VALUE* Find(const std::string& key) const {
101 typename _StringCache::const_iterator i = _stringCache.find(key);
102 return (i == _stringCache.end()) ? NULL : &i->second->value;
103 }
104
111 void Set(const std::type_info& key, const VALUE& value) {
112 if (VALUE* v = Find(key))
113 *v = value;
114 else {
115 Set(key.name(), value);
116 _CreateAlias(key, key.name());
117 }
118 }
119
125 void Set(const std::string& key, const VALUE& value) {
126 typename _StringCache::iterator i = _stringCache.find(key);
127
128 if (i != _stringCache.end())
129 i->second->value = value;
130 else {
131 _Entry* e = &_nameMap[key];
132 e->primaryKey = key;
133 e->value = value;
134
135 _stringCache[key] = e;
136 e->stringAliases.push_back(key);
137 }
138 }
139
147 bool CreateAlias(const std::string& alias, const std::string& key) {
148 typename _StringCache::iterator i = _stringCache.find(key);
149 if (i != _stringCache.end())
150 return (_CreateAlias(alias, i->second), true);
151 else
152 return false;
153 }
154
156 bool CreateAlias(const std::string& alias, const std::type_info& key) {
157 typename _TypeInfoCache::iterator i = _typeInfoCache.find(&key);
158 if (i != _typeInfoCache.end())
159 return (_CreateAlias(alias, i->second), true);
160 else
161 return false;
162 }
163
165 void Remove(const std::type_info& key) {
166 Remove(key.name());
167 }
168
170 void Remove(const std::string& key) {
171 typename _StringCache::iterator i = _stringCache.find(key);
172 if (i == _stringCache.end())
173 return;
174
175 _Entry* e = i->second;
176
177 for (TfIterator<_TypeInfoList> j = e->typeInfoAliases; j; ++j) {
178 _typeInfoCache.erase(*j);
179 }
180
181 for (TfIterator<std::list<std::string> > j = e->stringAliases; j; ++j) {
182 _stringCache.erase(*j);
183 }
184
185 // `e` points into the node owned by _nameMap. Passing
186 // e->primaryKey to erase() would cause the node to be
187 // deleted. However, the implementation of erase may access
188 // the key again after the node has been deleted (at least,
189 // when TfHashMap is __gnu_cxx::hash_map.)
190 const std::string primaryKey = std::move(e->primaryKey);
191 _nameMap.erase(primaryKey);
192 }
193
194private:
195 typedef std::list<const std::type_info*> _TypeInfoList;
196
197 struct _Entry {
198 mutable _TypeInfoList typeInfoAliases;
199 mutable std::list<std::string> stringAliases;
200 std::string primaryKey;
201 VALUE value;
202 };
203
204 void _CreateAlias(const std::type_info& alias, const std::string& key) {
205 typename _StringCache::iterator i = _stringCache.find(key);
206 if (i != _stringCache.end())
207 _CreateAlias(alias, i->second);
208 }
209
210 void _CreateAlias(const std::type_info& alias, _Entry* e) {
211 if (_typeInfoCache.find(&alias) == _typeInfoCache.end()) {
212 _typeInfoCache[&alias] = e;
213 e->typeInfoAliases.push_back(&alias);
214 }
215 }
216
217 void _CreateAlias(const std::string& alias, _Entry* e) {
218 if (_stringCache.find(alias) == _stringCache.end()) {
219 _stringCache[alias] = e;
220 e->stringAliases.push_back(alias);
221 }
222 }
223
224 typedef TfHashMap<std::string, _Entry, TfHash> _NameMap;
225 typedef TfHashMap<const std::type_info*, _Entry*, TfHash>
226 _TypeInfoCache;
227 typedef TfHashMap<std::string, _Entry*, TfHash> _StringCache;
228
229 _NameMap _nameMap;
230
231 _TypeInfoCache _typeInfoCache;
232 _StringCache _stringCache;
233};
234
235PXR_NAMESPACE_CLOSE_SCOPE
236
237#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:159
A map whose key is a const std::type_info&, or a string alias.
Definition: typeInfoMap.h:45
void Set(const std::type_info &key, const VALUE &value)
Set the value for a given key.
Definition: typeInfoMap.h:111
void Remove(const std::type_info &key)
Remove this key (and any aliases associated with it).
Definition: typeInfoMap.h:165
bool Exists(const std::type_info &key) const
Return true if the given key is present in the map.
Definition: typeInfoMap.h:55
void Remove(const std::string &key)
Remove this key (and any aliases associated with it).
Definition: typeInfoMap.h:170
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:100
void Set(const std::string &key, const VALUE &value)
Set the value for a given key.
Definition: typeInfoMap.h:125
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:68
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:84
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:156
bool Exists(const std::string &key) const
Return true if the given key is present in the map.
Definition: typeInfoMap.h:62
bool CreateAlias(const std::string &alias, const std::string &key)
Create an alias for a key.
Definition: typeInfoMap.h:147