24 #ifndef PXR_BASE_VT_DICTIONARY_H 25 #define PXR_BASE_VT_DICTIONARY_H 30 #include "pxr/base/vt/api.h" 31 #include "pxr/base/vt/value.h" 37 #include <boost/iterator/iterator_adaptor.hpp> 39 #include <initializer_list> 44 PXR_NAMESPACE_OPEN_SCOPE
63 typedef std::map<std::string, VtValue, std::less<>> _Map;
64 std::unique_ptr<_Map> _dictMap;
76 template<
class UnderlyingMapPtr,
class UnderlyingIterator>
77 class Iterator :
public boost::iterator_adaptor<Iterator<UnderlyingMapPtr,
78 UnderlyingIterator>, UnderlyingIterator> {
83 : Iterator::iterator_adaptor_(UnderlyingIterator())
84 , _underlyingMap(0) {}
87 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
88 Iterator(Iterator<OtherUnderlyingMapPtr,
89 OtherUnderlyingIterator>
const &other)
90 : Iterator::iterator_adaptor_(other.base())
91 , _underlyingMap(other._underlyingMap) {}
96 Iterator(UnderlyingMapPtr m, UnderlyingIterator i)
97 : Iterator::iterator_adaptor_(i)
99 if (m && i == m->end())
103 friend class boost::iterator_core_access;
106 UnderlyingIterator GetUnderlyingIterator(UnderlyingMapPtr map)
108 TF_AXIOM(!_underlyingMap || _underlyingMap == map);
109 return (!_underlyingMap) ? map->end() : this->base();
119 if (!_underlyingMap) {
121 "VtDictionary iterator");
124 if (++this->base_reference() == _underlyingMap->end()) {
136 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
137 bool equal(Iterator<OtherUnderlyingMapPtr,
138 OtherUnderlyingIterator>
const& i)
const {
139 if (_underlyingMap == i._underlyingMap)
140 if (!_underlyingMap || this->base() == i.base())
145 UnderlyingMapPtr _underlyingMap;
148 TF_MALLOC_TAG_NEW(
"Vt",
"VtDictionary");
150 typedef _Map::key_type key_type;
151 typedef _Map::mapped_type mapped_type;
152 typedef _Map::value_type value_type;
153 typedef _Map::allocator_type allocator_type;
154 typedef _Map::size_type size_type;
156 typedef Iterator<_Map*, _Map::iterator> iterator;
157 typedef Iterator<_Map const*, _Map::const_iterator> const_iterator;
166 template<
class _InputIterator>
199 size_type
count(
const std::string& key)
const;
203 size_type
count(
const char* key)
const;
207 size_type
erase(
const std::string& key);
211 void erase(iterator it);
215 void erase(iterator f, iterator l);
223 iterator
find(
const std::string& key);
227 iterator
find(
const char* key);
231 const_iterator
find(
const std::string& key)
const;
235 const_iterator
find(
const char* key)
const;
243 const_iterator
begin()
const;
251 const_iterator
end()
const;
255 size_type
size()
const;
275 return TfHash()(*dict._dictMap);
279 template<
class _InputIterator>
280 void insert(_InputIterator f, _InputIterator l) {
283 _CreateDictIfNeeded();
284 _dictMap->insert(f, l);
290 std::pair<iterator, bool>
insert(
const value_type& obj);
300 char const *delimiters =
":")
const;
317 VtValue const &value,
char const *delimiters =
":");
334 char const *delimiters =
":");
344 _SetValueAtPathImpl(std::vector<std::string>::const_iterator curKeyElem,
345 std::vector<std::string>::const_iterator keyElemEnd,
348 void _EraseValueAtPathImpl(
349 std::vector<std::string>::const_iterator curKeyElem,
350 std::vector<std::string>::const_iterator keyElemEnd);
352 void _CreateDictIfNeeded();
373 template <
typename T>
376 const std::string &key )
378 VtDictionary::const_iterator i = dictionary.
find(key);
379 if ( i == dictionary.
end() ) {
383 return i->second.IsHolding<T>();
387 template <
typename T>
392 VtDictionary::const_iterator i = dictionary.
find(key);
393 if ( i == dictionary.
end() ) {
397 return i->second.IsHolding<T>();
411 template <
typename T>
414 const std::string &key )
416 VtDictionary::const_iterator i = dictionary.
find(key);
417 if (ARCH_UNLIKELY(i == dictionary.
end())) {
419 "', which is not in the dictionary.");
422 return i->second.Get<T>();
426 template <
typename T>
431 VtDictionary::const_iterator i = dictionary.
find(key);
432 if (ARCH_UNLIKELY(i == dictionary.
end())) {
434 "which is not in the dictionary.", key);
437 return i->second.Get<T>();
444 struct Vt_DefaultHolder {
445 explicit Vt_DefaultHolder(T
const &t) : val(t) {}
452 struct Vt_DefaultGenerator {
454 Vt_DefaultHolder<T> operator=(T
const &t) {
455 return Vt_DefaultHolder<T>(t);
461 extern VT_API Vt_DefaultGenerator VtDefault;
475 template <
class T,
class U>
477 const std::string &key,
478 Vt_DefaultHolder<U>
const &def )
480 VtDictionary::const_iterator i = dictionary.
find(key);
481 if (i == dictionary.
end() || !i->second.IsHolding<T>())
483 return i->second.UncheckedGet<T>();
487 template <
class T,
class U>
490 Vt_DefaultHolder<U>
const &def )
492 VtDictionary::const_iterator i = dictionary.
find(key);
493 if (i == dictionary.
end() || !i->second.IsHolding<T>())
495 return i->second.UncheckedGet<T>();
513 bool coerceToWeakerOpinionType =
false);
528 bool coerceToWeakerOpinionType =
false);
543 bool coerceToWeakerOpinionType =
false);
565 bool coerceToWeakerOpinionType =
false);
586 bool coerceToWeakerOpinionType =
false);
610 bool coerceToWeakerOpinionType =
false);
613 struct VtDictionaryHash {
614 inline size_t operator()(
VtDictionary const &dict)
const {
619 PXR_NAMESPACE_CLOSE_SCOPE
VT_API iterator end()
Returns an iterator pointing to the end of the VtDictionary.
VT_API VtDictionary VtDictionaryOverRecursive(const VtDictionary &strong, const VtDictionary &weak, bool coerceToWeakerOpinionType=false)
Returns a dictionary containing strong recursively composed over weak.
VT_API void swap(VtDictionary &dict)
Swaps the contents of two VtDictionaries.
VT_API VtValue & operator[](const std::string &key)
Returns a reference to the VtValue that is associated with a particular key.
VT_API iterator find(const std::string &key)
Finds an element whose key is key.
A map with string keys and VtValue values.
AR_API bool operator!=(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
Low-level utilities for informing users of various internal and external diagnostic conditions.
AR_API bool operator==(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
VT_API size_type size() const
Returns the size of the VtDictionary.
VT_API iterator begin()
Returns an iterator pointing to the beginning of the VtDictionary.
VT_API size_type erase(const std::string &key)
Erases the element whose key is key.
void insert(_InputIterator f, _InputIterator l)
Inserts a range into the VtDictionary.
VtDictionary(int size)
Creates an empty VtDictionary with at least size buckets.
const T & VtDictionaryGet(const VtDictionary &dictionary, const std::string &key)
Return a value held in a VtDictionary by reference.
VT_API bool empty() const
true if the VtDictionary's size is 0.
A user-extensible hashing mechanism for use with runtime hash tables.
VT_API VtValue const * GetValueAtPath(std::string const &keyPath, char const *delimiters=":") const
Return a pointer to the value at keyPath if one exists.
VT_API std::ostream & operator<<(std::ostream &, VtDictionary const &)
Write the contents of a VtDictionary to a stream, formatted like "{ 'key1': value1,...
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
VtDictionary()
Creates an empty VtDictionary.
VT_API VtDictionary VtDictionaryOver(const VtDictionary &strong, const VtDictionary &weak, bool coerceToWeakerOpinionType=false)
Creates a dictionary containing strong composed over weak.
VT_API size_type count(const std::string &key) const
Counts the number of elements whose key is key.
size_t hash_value(const half h)
Overload hash_value for half.
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
VT_API void EraseValueAtPath(std::string const &keyPath, char const *delimiters=":")
Erase the value at keyPath.
VtDictionary(_InputIterator f, _InputIterator l)
Creates a VtDictionary with a copy of a range.
VT_API void clear()
Erases all of the elements.
VT_API void SetValueAtPath(std::string const &keyPath, VtValue const &value, char const *delimiters=":")
Set the value at keyPath to value.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
VT_API VtDictionary & operator=(VtDictionary const &other)
Copy assignment operator.
bool VtDictionaryIsHolding(const VtDictionary &dictionary, const std::string &key)
Returns true if dictionary contains key and the corresponding value is of type T.