HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StringMap.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_StringMap.h
7  *
8  * COMMENTS: No UT_API here because there's no lib implementation!
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_StringMap_h__
14 #define __UT_StringMap_h__
15 
16 #include "UT_Map.h"
17 #include "UT_StringHolder.h"
18 
19 /// We want methods like find() to take a const UT_StringRef& instead of a
20 /// const UT_StringHolder& for the following reasons:
21 /// - This allows a const char* to be passed in without forcing a copy of the string.
22 /// - A UT_StringRef can be used without converting it to a UT_StringHolder
23 /// (which hardens the string if necessary).
24 /// - A UT_StringHolder can still be directly passed in.
25 #define UT_STRINGREF_WRAPPER(return_type, name, qualifier) \
26  SYS_FORCE_INLINE return_type name(const UT_StringRef &key) qualifier \
27  { \
28  return Parent::name(UTmakeUnsafeRef(key)); \
29  }
30 /// Specialization of the above macro for methods that return an iterator
31 /// range, since something like std::pair<iterator, iterator> is interpreted as
32 /// two arguments when being passed to a macro (due to the comma).
33 #define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier) \
34  SYS_FORCE_INLINE std::pair<iterator_type, iterator_type> \
35  name(const UT_StringRef &key) qualifier \
36  { \
37  return Parent::name(UTmakeUnsafeRef(key)); \
38  }
39 
40 /// UT_StringMap is a simple specialization of a UT_Map that has
41 /// UT_StringHolder as its key type which allows C strings to be used.
42 /// If you know that a string will not be destroyed during the map's
43 /// lifetime, UTmakeUnsafeRef can be used to insert a shallow reference.
44 template <typename ITEM_T>
45 class UT_StringMap : public UT_Map<UT_StringHolder, ITEM_T>
46 {
47  typedef UT_Map<UT_StringHolder, ITEM_T> Parent;
48 
49 public:
51  typedef typename Parent::iterator iterator;
52  typedef typename Parent::size_type size_type;
53 
54  // Expose the erase() overloads that take iterators.
55  // The erase(const UT_StringHolder&) overload is hidden in favour of
56  // erase(const UT_StringRef &) from UT_STRINGREF_WRAPPER.
57  iterator erase(const_iterator pos) { return Parent::erase(pos); }
59  {
60  return Parent::erase(first, last);
61  }
62 
63  SYS_FORCE_INLINE ITEM_T get(const UT_StringRef &key, const ITEM_T &defval) const
64  {
65  return Parent::get(UTmakeUnsafeRef(key), defval);
66  }
67 
68  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
69  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
70  UT_STRINGREF_WRAPPER(bool, contains, const)
72  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
73  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
74  UT_STRINGREF_WRAPPER(size_type, erase, )
75  UT_STRINGREF_WRAPPER(iterator, find, )
76  UT_STRINGREF_WRAPPER(const_iterator, find, const)
77 };
78 
79 /// Same as UT_StringMap, but keeps the strings in a sorted order.
80 template <typename ITEM_T>
82 {
84 
85 public:
87  typedef typename Parent::iterator iterator;
88  typedef typename Parent::size_type size_type;
89 
90  // NOTE: When we compile against a more modern standard library on OSX, the
91  // signature can be changed to return an iterator to the following element
92  // (the signature was changed in C++11).
93  void erase(const_iterator pos) { Parent::erase(pos); }
94  void erase(const_iterator first, const_iterator last)
95  {
96  Parent::erase(first, last);
97  }
98 
99  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
100  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
101  UT_STRINGREF_WRAPPER(bool, contains, const)
102  UT_STRINGREF_WRAPPER(size_type, count, const)
103  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
104  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
105  UT_STRINGREF_WRAPPER(size_type, erase, )
106  UT_STRINGREF_WRAPPER(iterator, find, )
107  UT_STRINGREF_WRAPPER(const_iterator, find, const)
108  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
109  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
110  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
111  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
112 };
113 
114 #undef UT_STRINGREF_WRAPPER
115 #undef UT_STRINGREF_WRAPPER_RANGE
116 
117 #endif
GLint first
Definition: glcorearb.h:405
void erase(const_iterator pos)
Definition: UT_StringMap.h:93
Unsorted map container.
Definition: UT_Map.h:109
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
Parent::size_type size_type
Definition: UT_StringMap.h:88
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringMap.h:25
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
ITEM_T get(const key_type &key, const ITEM_T &defval) const
Definition: UT_Map.h:171
Parent::iterator iterator
Definition: UT_StringMap.h:51
Sorted map container.
Definition: UT_Map.h:283
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:58
Parent::iterator iterator
Definition: UT_StringMap.h:87
Same as UT_StringMap, but keeps the strings in a sorted order.
Definition: UT_StringMap.h:81
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
iterator erase(const_iterator pos)
Definition: UT_StringMap.h:57
void erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:94
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:50
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
Parent::size_type size_type
Definition: UT_StringMap.h:52
Base::const_iterator const_iterator
Definition: UT_Map.h:120
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:86
GLint GLsizei count
Definition: glcorearb.h:405
bool contains(const key_type &key) const
Returns true if a value with the key is contained in the map.
Definition: UT_Map.h:161
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
Definition: UT_StringMap.h:33