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/functional/hash.hpp>
38 #include <boost/iterator/iterator_adaptor.hpp>
40 #include <initializer_list>
45 PXR_NAMESPACE_OPEN_SCOPE
64 typedef std::map<std::string, VtValue, std::less<>> _Map;
65 std::unique_ptr<_Map> _dictMap;
77 template<
class UnderlyingMapPtr,
class UnderlyingIterator>
78 class Iterator :
public boost::iterator_adaptor<Iterator<UnderlyingMapPtr,
79 UnderlyingIterator>, UnderlyingIterator> {
84 : Iterator::iterator_adaptor_(UnderlyingIterator())
85 , _underlyingMap(0) {}
88 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
89 Iterator(Iterator<OtherUnderlyingMapPtr,
90 OtherUnderlyingIterator>
const &other)
91 : Iterator::iterator_adaptor_(other.base())
92 , _underlyingMap(other._underlyingMap) {}
97 Iterator(UnderlyingMapPtr m, UnderlyingIterator i)
98 : Iterator::iterator_adaptor_(i)
100 if (m && i == m->end())
104 friend class boost::iterator_core_access;
107 UnderlyingIterator GetUnderlyingIterator(UnderlyingMapPtr map)
109 TF_AXIOM(!_underlyingMap || _underlyingMap == map);
110 return (!_underlyingMap) ? map->end() : this->base();
120 if (!_underlyingMap) {
122 "VtDictionary iterator");
125 if (++this->base_reference() == _underlyingMap->end()) {
137 template <
class OtherUnderlyingMapPtr,
class OtherUnderlyingIterator>
138 bool equal(Iterator<OtherUnderlyingMapPtr,
139 OtherUnderlyingIterator>
const& i)
const {
140 if (_underlyingMap == i._underlyingMap)
141 if (!_underlyingMap || this->base() == i.base())
146 UnderlyingMapPtr _underlyingMap;
149 TF_MALLOC_TAG_NEW(
"Vt",
"VtDictionary");
151 typedef _Map::key_type key_type;
152 typedef _Map::mapped_type mapped_type;
153 typedef _Map::value_type value_type;
154 typedef _Map::allocator_type allocator_type;
155 typedef _Map::size_type size_type;
157 typedef Iterator<_Map*, _Map::iterator> iterator;
158 typedef Iterator<_Map const*, _Map::const_iterator> const_iterator;
167 template<
class _InputIterator>
200 size_type
count(
const std::string& key)
const;
204 size_type
count(
const char* key)
const;
208 size_type
erase(
const std::string& key);
212 void erase(iterator it);
216 void erase(iterator f, iterator l);
224 iterator
find(
const std::string& key);
228 iterator
find(
const char* key);
232 const_iterator
find(
const std::string& key)
const;
236 const_iterator
find(
const char* key)
const;
244 const_iterator
begin()
const;
252 const_iterator
end()
const;
256 size_type
size()
const;
276 return boost::hash_value(*dict._dictMap);
280 template<
class _InputIterator>
281 void insert(_InputIterator f, _InputIterator l) {
284 _CreateDictIfNeeded();
285 _dictMap->insert(f, l);
291 std::pair<iterator, bool>
insert(
const value_type& obj);
301 char const *delimiters =
":")
const;
318 VtValue const &value,
char const *delimiters =
":");
335 char const *delimiters =
":");
345 _SetValueAtPathImpl(std::vector<std::string>::const_iterator curKeyElem,
346 std::vector<std::string>::const_iterator keyElemEnd,
349 void _EraseValueAtPathImpl(
350 std::vector<std::string>::const_iterator curKeyElem,
351 std::vector<std::string>::const_iterator keyElemEnd);
353 void _CreateDictIfNeeded();
374 template <
typename T>
377 const std::string &key )
379 VtDictionary::const_iterator i = dictionary.
find(key);
380 if ( i == dictionary.
end() ) {
384 return i->second.IsHolding<T>();
388 template <
typename T>
393 VtDictionary::const_iterator i = dictionary.
find(key);
394 if ( i == dictionary.
end() ) {
398 return i->second.IsHolding<T>();
412 template <
typename T>
415 const std::string &key )
417 VtDictionary::const_iterator i = dictionary.
find(key);
418 if (ARCH_UNLIKELY(i == dictionary.
end())) {
420 "', which is not in the dictionary.");
423 return i->second.Get<T>();
427 template <
typename T>
432 VtDictionary::const_iterator i = dictionary.
find(key);
433 if (ARCH_UNLIKELY(i == dictionary.
end())) {
435 "which is not in the dictionary.", key);
438 return i->second.Get<T>();
445 struct Vt_DefaultHolder {
446 explicit Vt_DefaultHolder(T
const &t) : val(t) {}
453 struct Vt_DefaultGenerator {
455 Vt_DefaultHolder<T> operator=(T
const &t) {
456 return Vt_DefaultHolder<T>(t);
462 extern VT_API Vt_DefaultGenerator VtDefault;
476 template <
class T,
class U>
478 const std::string &key,
479 Vt_DefaultHolder<U>
const &def )
481 VtDictionary::const_iterator i = dictionary.
find(key);
482 if (i == dictionary.
end() || !i->second.IsHolding<T>())
484 return i->second.UncheckedGet<T>();
488 template <
class T,
class U>
491 Vt_DefaultHolder<U>
const &def )
493 VtDictionary::const_iterator i = dictionary.
find(key);
494 if (i == dictionary.
end() || !i->second.IsHolding<T>())
496 return i->second.UncheckedGet<T>();
514 bool coerceToWeakerOpinionType =
false);
529 bool coerceToWeakerOpinionType =
false);
544 bool coerceToWeakerOpinionType =
false);
566 bool coerceToWeakerOpinionType =
false);
587 bool coerceToWeakerOpinionType =
false);
611 bool coerceToWeakerOpinionType =
false);
614 struct VtDictionaryHash {
615 inline size_t operator()(
VtDictionary const &dict)
const {
616 return hash_value(dict);
620 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 size_type size() const
Returns the size of the VtDictionary.
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 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 size_type count(const std::string &key) const
Counts the number of elements whose key is key.
#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.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
VT_API VtValue const * GetValueAtPath(std::string const &keyPath, char const *delimiters=":") const
Return a pointer to the value at keyPath if one exists.
#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. ...
VT_API bool empty() const
true if the VtDictionary's size is 0.