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 <initializer_list>
42PXR_NAMESPACE_OPEN_SCOPE
61 typedef std::map<std::string, VtValue, std::less<>> _Map;
62 std::unique_ptr<_Map> _dictMap;
74 template<
class UnderlyingMapPtr,
class UnderlyingIterator>
77 using iterator_category = std::bidirectional_iterator_tag;
78 using value_type =
typename UnderlyingIterator::value_type;
79 using reference =
typename UnderlyingIterator::reference;
80 using pointer =
typename UnderlyingIterator::pointer;
81 using difference_type =
typename UnderlyingIterator::difference_type;
89 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
90 Iterator(Iterator<OtherUnderlyingMapPtr,
91 OtherUnderlyingIterator>
const &other)
92 : _underlyingIterator(other._underlyingIterator),
93 _underlyingMap(other._underlyingMap) {}
95 reference operator*()
const {
return *_underlyingIterator; }
96 pointer operator->()
const {
return _underlyingIterator.operator->(); }
98 Iterator& operator++() {
103 Iterator operator++(
int) {
104 Iterator result = *
this;
109 Iterator& operator--() {
110 --_underlyingIterator;
114 Iterator operator--(
int) {
115 Iterator result = *
this;
116 --_underlyingIterator;
120 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
121 bool operator==(
const Iterator<OtherUnderlyingMapPtr,
122 OtherUnderlyingIterator>& other)
const {
126 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
127 bool operator!=(
const Iterator<OtherUnderlyingMapPtr,
128 OtherUnderlyingIterator>& other)
const {
129 return !equal(other);
136 Iterator(UnderlyingMapPtr m, UnderlyingIterator i)
137 : _underlyingIterator(i),
139 if (m && i == m->end())
140 _underlyingMap =
nullptr;
145 UnderlyingIterator GetUnderlyingIterator(UnderlyingMapPtr map)
147 TF_AXIOM(!_underlyingMap || _underlyingMap == map);
148 return (!_underlyingMap) ? map->end() : _underlyingIterator;
158 if (!_underlyingMap) {
160 "VtDictionary iterator");
163 if (++_underlyingIterator == _underlyingMap->end()) {
164 _underlyingMap =
nullptr;
175 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
176 bool equal(Iterator<OtherUnderlyingMapPtr,
177 OtherUnderlyingIterator>
const& other)
const {
178 if (_underlyingMap == other._underlyingMap)
179 if (!_underlyingMap ||
180 (_underlyingIterator == other._underlyingIterator))
185 UnderlyingIterator _underlyingIterator;
186 UnderlyingMapPtr _underlyingMap =
nullptr;
191 typedef _Map::key_type key_type;
192 typedef _Map::mapped_type mapped_type;
193 typedef _Map::value_type value_type;
194 typedef _Map::allocator_type allocator_type;
195 typedef _Map::size_type size_type;
197 typedef Iterator<_Map*, _Map::iterator> iterator;
198 typedef Iterator<_Map const*, _Map::const_iterator> const_iterator;
207 template<
class _InputIterator>
240 size_type
count(
const std::string& key)
const;
244 size_type
count(
const char* key)
const;
248 size_type
erase(
const std::string& key);
264 iterator
find(
const std::string& key);
268 iterator
find(
const char* key);
272 const_iterator
find(
const std::string& key)
const;
276 const_iterator
find(
const char* key)
const;
292 const_iterator
end()
const;
316 return TfHash()(*dict._dictMap);
320 template<
class _InputIterator>
321 void insert(_InputIterator f, _InputIterator l) {
324 _CreateDictIfNeeded();
325 _dictMap->insert(f, l);
331 std::pair<iterator, bool>
insert(
const value_type& obj);
341 char const *delimiters =
":")
const;
358 VtValue const &value,
char const *delimiters =
":");
375 char const *delimiters =
":");
385 _SetValueAtPathImpl(std::vector<std::string>::const_iterator curKeyElem,
386 std::vector<std::string>::const_iterator keyElemEnd,
389 void _EraseValueAtPathImpl(
390 std::vector<std::string>::const_iterator curKeyElem,
391 std::vector<std::string>::const_iterator keyElemEnd);
393 void _CreateDictIfNeeded();
417 const std::string &key )
419 VtDictionary::const_iterator i = dictionary.
find(key);
420 if ( i == dictionary.
end() ) {
424 return i->second.IsHolding<T>();
433 VtDictionary::const_iterator i = dictionary.
find(key);
434 if ( i == dictionary.
end() ) {
438 return i->second.IsHolding<T>();
455 const std::string &key )
457 VtDictionary::const_iterator i = dictionary.
find(key);
458 if (ARCH_UNLIKELY(i == dictionary.
end())) {
460 "', which is not in the dictionary.");
463 return i->second.Get<T>();
472 VtDictionary::const_iterator i = dictionary.
find(key);
473 if (ARCH_UNLIKELY(i == dictionary.
end())) {
475 "which is not in the dictionary.", key);
478 return i->second.Get<T>();
485struct Vt_DefaultHolder {
486 explicit Vt_DefaultHolder(T
const &t) : val(t) {}
493struct Vt_DefaultGenerator {
495 Vt_DefaultHolder<T> operator=(T
const &t) {
496 return Vt_DefaultHolder<T>(t);
502extern VT_API Vt_DefaultGenerator VtDefault;
516template <
class T,
class U>
518 const std::string &key,
519 Vt_DefaultHolder<U>
const &def )
521 VtDictionary::const_iterator i = dictionary.
find(key);
522 if (i == dictionary.
end() || !i->second.IsHolding<T>())
524 return i->second.UncheckedGet<T>();
528template <
class T,
class U>
531 Vt_DefaultHolder<U>
const &def )
533 VtDictionary::const_iterator i = dictionary.
find(key);
534 if (i == dictionary.
end() || !i->second.IsHolding<T>())
536 return i->second.UncheckedGet<T>();
554 bool coerceToWeakerOpinionType =
false);
569 bool coerceToWeakerOpinionType =
false);
584 bool coerceToWeakerOpinionType =
false);
606 bool coerceToWeakerOpinionType =
false);
627 bool coerceToWeakerOpinionType =
false);
651 bool coerceToWeakerOpinionType =
false);
654struct VtDictionaryHash {
655 inline size_t operator()(
VtDictionary const &dict)
const {
660PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
A user-extensible hashing mechanism for use with runtime hash tables.
A map with string keys and VtValue values.
VT_API void SetValueAtPath(std::string const &keyPath, VtValue const &value, char const *delimiters=":")
Set the value at keyPath to value.
VT_API size_type size() const
Returns the size of the VtDictionary.
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 size_type count(const std::string &key) const
Counts the number of elements whose key is key.
VT_API const_iterator end() const
Returns an iterator pointing to the end of the VtDictionary.
VT_API void clear()
Erases all of the elements.
VT_API void SetValueAtPath(std::vector< std::string > const &keyPath, VtValue const &value)
Set the value at keyPath to value.
VT_API iterator begin()
Returns an iterator pointing to the beginning of the VtDictionary.
VT_API std::pair< iterator, bool > insert(const value_type &obj)
Inserts obj into the VtDictionary.
VT_API VtDictionary(std::initializer_list< value_type > init)
Creates a new VtDictionary from a braced initializer list.
VT_API void swap(VtDictionary &dict)
Swaps the contents of two VtDictionaries.
VtDictionary()
Creates an empty VtDictionary.
VT_API void erase(iterator it)
Erases the element pointed to by it.
VtDictionary(_InputIterator f, _InputIterator l)
Creates a VtDictionary with a copy of a range.
VT_API void erase(iterator f, iterator l)
Erases all elements in a range.
VT_API void EraseValueAtPath(std::string const &keyPath, char const *delimiters=":")
Erase the value at keyPath.
VT_API iterator find(const char *key)
Finds an element whose key is key.
void insert(_InputIterator f, _InputIterator l)
Inserts a range into the VtDictionary.
VT_API iterator end()
Returns an iterator pointing to the end of the VtDictionary.
VT_API const_iterator find(const std::string &key) const
Finds an element whose key is key.
VT_API size_type erase(const std::string &key)
Erases the element whose key is key.
VT_API void EraseValueAtPath(std::vector< std::string > const &keyPath)
Erase the value at keyPath.
VT_API iterator find(const std::string &key)
Finds an element whose key is key.
VT_API VtDictionary(VtDictionary const &other)
Creates a copy of the supplied VtDictionary.
VtDictionary(int size)
Creates an empty VtDictionary with at least size buckets.
VT_API bool empty() const
true if the VtDictionary's size is 0.
VT_API VtValue & operator[](const std::string &key)
Returns a reference to the VtValue that is associated with a particular key.
VT_API size_type count(const char *key) const
Counts the number of elements whose key is key.
VT_API VtDictionary & operator=(VtDictionary const &other)
Copy assignment operator.
VT_API VtDictionary(VtDictionary &&other)=default
Creates a new VtDictionary by moving the supplied VtDictionary.
VT_API const_iterator begin() const
Returns an iterator pointing to the beginning of the VtDictionary.
VT_API const_iterator find(const char *key) const
Finds an element whose key is key.
VT_API VtDictionary & operator=(VtDictionary &&other)=default
Move assignment operator.
VT_API VtValue const * GetValueAtPath(std::vector< std::string > const &keyPath) const
Return a pointer to the value at keyPath if one exists.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
VT_API VtDictionary VtDictionaryOverRecursive(const VtDictionary &strong, const VtDictionary &weak, bool coerceToWeakerOpinionType=false)
Returns a dictionary containing strong recursively composed over weak.
const T & VtDictionaryGet(const VtDictionary &dictionary, const std::string &key)
Return a value held in a VtDictionary by reference.
VT_API VtDictionary VtDictionaryOver(const VtDictionary &strong, const VtDictionary &weak, bool coerceToWeakerOpinionType=false)
Creates a dictionary containing strong composed over weak.
bool VtDictionaryIsHolding(const VtDictionary &dictionary, const std::string &key)
Returns true if dictionary contains key and the corresponding value is of type T.
#define TF_MALLOC_TAG_NEW(name1, name2)
Enable lib/tf memory management.
size_t hash_value(const TfToken &x)
Overload hash_value for TfToken.