7#ifndef PXR_BASE_VT_DICTIONARY_H
8#define PXR_BASE_VT_DICTIONARY_H
13#include "pxr/base/vt/api.h"
14#include "pxr/base/vt/value.h"
20#include <initializer_list>
25PXR_NAMESPACE_OPEN_SCOPE
44 typedef std::map<std::string, VtValue, std::less<>> _Map;
45 std::unique_ptr<_Map> _dictMap;
57 template<
class UnderlyingMapPtr,
class UnderlyingIterator>
60 using iterator_category = std::bidirectional_iterator_tag;
61 using value_type =
typename UnderlyingIterator::value_type;
62 using reference =
typename UnderlyingIterator::reference;
63 using pointer =
typename UnderlyingIterator::pointer;
64 using difference_type =
typename UnderlyingIterator::difference_type;
72 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
73 Iterator(Iterator<OtherUnderlyingMapPtr,
74 OtherUnderlyingIterator>
const &other)
75 : _underlyingIterator(other._underlyingIterator),
76 _underlyingMap(other._underlyingMap) {}
78 reference operator*()
const {
return *_underlyingIterator; }
79 pointer operator->()
const {
return _underlyingIterator.operator->(); }
81 Iterator& operator++() {
86 Iterator operator++(
int) {
87 Iterator result = *
this;
92 Iterator& operator--() {
93 --_underlyingIterator;
97 Iterator operator--(
int) {
98 Iterator result = *
this;
99 --_underlyingIterator;
103 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
104 bool operator==(
const Iterator<OtherUnderlyingMapPtr,
105 OtherUnderlyingIterator>& other)
const {
109 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
110 bool operator!=(
const Iterator<OtherUnderlyingMapPtr,
111 OtherUnderlyingIterator>& other)
const {
112 return !equal(other);
119 Iterator(UnderlyingMapPtr m, UnderlyingIterator i)
120 : _underlyingIterator(i),
122 if (m && i == m->end())
123 _underlyingMap =
nullptr;
128 UnderlyingIterator GetUnderlyingIterator(UnderlyingMapPtr map)
130 TF_AXIOM(!_underlyingMap || _underlyingMap == map);
131 return (!_underlyingMap) ? map->end() : _underlyingIterator;
141 if (!_underlyingMap) {
143 "VtDictionary iterator");
146 if (++_underlyingIterator == _underlyingMap->end()) {
147 _underlyingMap =
nullptr;
158 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
159 bool equal(Iterator<OtherUnderlyingMapPtr,
160 OtherUnderlyingIterator>
const& other)
const {
161 if (_underlyingMap == other._underlyingMap)
162 if (!_underlyingMap ||
163 (_underlyingIterator == other._underlyingIterator))
168 UnderlyingIterator _underlyingIterator;
169 UnderlyingMapPtr _underlyingMap =
nullptr;
174 typedef _Map::key_type key_type;
175 typedef _Map::mapped_type mapped_type;
176 typedef _Map::value_type value_type;
177 typedef _Map::allocator_type allocator_type;
178 typedef _Map::size_type size_type;
180 typedef Iterator<_Map*, _Map::iterator> iterator;
181 typedef Iterator<_Map const*, _Map::const_iterator> const_iterator;
190 template<
class _InputIterator>
223 size_type
count(
const std::string& key)
const;
227 size_type
count(
const char* key)
const;
231 size_type
erase(
const std::string& key);
239 iterator
erase(iterator f, iterator l);
247 iterator
find(
const std::string& key);
251 iterator
find(
const char* key);
255 const_iterator
find(
const std::string& key)
const;
259 const_iterator
find(
const char* key)
const;
275 const_iterator
end()
const;
299 return TfHash()(*dict._dictMap);
303 template<
class _InputIterator>
304 void insert(_InputIterator f, _InputIterator l) {
307 _CreateDictIfNeeded();
308 _dictMap->insert(f, l);
314 std::pair<iterator, bool>
insert(
const value_type& obj);
324 char const *delimiters =
":")
const;
341 VtValue const &value,
char const *delimiters =
":");
358 char const *delimiters =
":");
368 _SetValueAtPathImpl(std::vector<std::string>::const_iterator curKeyElem,
369 std::vector<std::string>::const_iterator keyElemEnd,
372 void _EraseValueAtPathImpl(
373 std::vector<std::string>::const_iterator curKeyElem,
374 std::vector<std::string>::const_iterator keyElemEnd);
376 void _CreateDictIfNeeded();
400 const std::string &key )
402 VtDictionary::const_iterator i = dictionary.
find(key);
403 if ( i == dictionary.
end() ) {
407 return i->second.IsHolding<T>();
416 VtDictionary::const_iterator i = dictionary.
find(key);
417 if ( i == dictionary.
end() ) {
421 return i->second.IsHolding<T>();
438 const std::string &key )
440 VtDictionary::const_iterator i = dictionary.
find(key);
441 if (ARCH_UNLIKELY(i == dictionary.
end())) {
443 "', which is not in the dictionary.");
446 return i->second.Get<T>();
455 VtDictionary::const_iterator i = dictionary.
find(key);
456 if (ARCH_UNLIKELY(i == dictionary.
end())) {
458 "which is not in the dictionary.", key);
461 return i->second.Get<T>();
468struct Vt_DefaultHolder {
469 explicit Vt_DefaultHolder(T
const &t) : val(t) {}
476struct Vt_DefaultGenerator {
478 Vt_DefaultHolder<T> operator=(T
const &t) {
479 return Vt_DefaultHolder<T>(t);
485extern VT_API Vt_DefaultGenerator VtDefault;
499template <
class T,
class U>
501 const std::string &key,
502 Vt_DefaultHolder<U>
const &def )
504 VtDictionary::const_iterator i = dictionary.
find(key);
505 if (i == dictionary.
end() || !i->second.IsHolding<T>())
507 return i->second.UncheckedGet<T>();
511template <
class T,
class U>
514 Vt_DefaultHolder<U>
const &def )
516 VtDictionary::const_iterator i = dictionary.
find(key);
517 if (i == dictionary.
end() || !i->second.IsHolding<T>())
519 return i->second.UncheckedGet<T>();
537 bool coerceToWeakerOpinionType =
false);
552 bool coerceToWeakerOpinionType =
false);
567 bool coerceToWeakerOpinionType =
false);
622struct VtDictionaryHash {
623 inline size_t operator()(
VtDictionary const &dict)
const {
628PXR_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.
VT_API iterator erase(iterator f, iterator l)
Erases all elements in a range.
VtDictionary()
Creates an empty VtDictionary.
VtDictionary(_InputIterator f, _InputIterator l)
Creates a VtDictionary with a copy of 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.
VT_API iterator erase(iterator it)
Erases the element pointed to by it.
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)
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.