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_ArrayStringMap.h" // This is only included to avoid sweeping for it right now.
17 #include "UT_Map.h"
18 #include "UT_StringHolder.h"
19 
20 /// We want methods like find() to take a const UT_StringRef& instead of a
21 /// const UT_StringHolder& for the following reasons:
22 /// - This allows a const char* to be passed in without forcing a copy of the string.
23 /// - A UT_StringRef can be used without converting it to a UT_StringHolder
24 /// (which hardens the string if necessary).
25 /// - A UT_StringHolder can still be directly passed in.
26 #define UT_STRINGREF_WRAPPER(return_type, name, qualifier) \
27  SYS_FORCE_INLINE return_type name(const UT_StringRef &key) qualifier \
28  { \
29  return Parent::name(UTmakeUnsafeRef(key)); \
30  }
31 /// Specialization of the above macro for methods that return an iterator
32 /// range, since something like std::pair<iterator, iterator> is interpreted as
33 /// two arguments when being passed to a macro (due to the comma).
34 #define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier) \
35  SYS_FORCE_INLINE std::pair<iterator_type, iterator_type> \
36  name(const UT_StringRef &key) qualifier \
37  { \
38  return Parent::name(UTmakeUnsafeRef(key)); \
39  }
40 
41 /// UT_StringMap is a simple specialization of a UT_Map that has
42 /// UT_StringHolder as its key type which allows C strings to be used.
43 /// If you know that a string will not be destroyed during the map's
44 /// lifetime, UTmakeUnsafeRef can be used to insert a shallow reference.
45 template <typename ITEM_T>
46 class UT_StringMap : public UT_Map<UT_StringHolder, ITEM_T>
47 {
48  typedef UT_Map<UT_StringHolder, ITEM_T> Parent;
49 
50 public:
52  typedef typename Parent::iterator iterator;
53  typedef typename Parent::size_type size_type;
54 
55  // Expose the erase() overloads that take iterators.
56  // The erase(const UT_StringHolder&) overload is hidden in favour of
57  // erase(const UT_StringRef &) from UT_STRINGREF_WRAPPER.
58  iterator erase(const_iterator pos) { return Parent::erase(pos); }
60  {
61  return Parent::erase(first, last);
62  }
63 
64  SYS_FORCE_INLINE ITEM_T get(const UT_StringRef &key, const ITEM_T &defval) const
65  {
66  return Parent::get(UTmakeUnsafeRef(key), defval);
67  }
68 
69  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
70  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
71  UT_STRINGREF_WRAPPER(bool, contains, const)
73  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
74  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
75  UT_STRINGREF_WRAPPER(size_type, erase, )
76  UT_STRINGREF_WRAPPER(iterator, find, )
77  UT_STRINGREF_WRAPPER(const_iterator, find, const)
78 };
79 
80 /// Same as UT_StringMap, but keeps the strings in a sorted order.
81 template <typename ITEM_T>
83 {
85 
86 public:
88  typedef typename Parent::iterator iterator;
89  typedef typename Parent::size_type size_type;
90 
91  // NOTE: When we compile against a more modern standard library on OSX, the
92  // signature can be changed to return an iterator to the following element
93  // (the signature was changed in C++11).
94  void erase(const_iterator pos) { Parent::erase(pos); }
95  void erase(const_iterator first, const_iterator last)
96  {
97  Parent::erase(first, last);
98  }
99 
100  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
101  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
102  UT_STRINGREF_WRAPPER(bool, contains, const)
103  UT_STRINGREF_WRAPPER(size_type, count, const)
104  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
105  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
106  UT_STRINGREF_WRAPPER(size_type, erase, )
107  UT_STRINGREF_WRAPPER(iterator, find, )
108  UT_STRINGREF_WRAPPER(const_iterator, find, const)
109  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
110  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
111  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
112  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
113 };
114 
115 #undef UT_STRINGREF_WRAPPER
116 #undef UT_STRINGREF_WRAPPER_RANGE
117 
118 #endif
GLint first
Definition: glcorearb.h:405
void erase(const_iterator pos)
Definition: UT_StringMap.h:94
Unsorted map container.
Definition: UT_Map.h:107
*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:623
Parent::size_type size_type
Definition: UT_StringMap.h:89
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringMap.h:26
ITEM_T get(const key_type &key, const ITEM_T &defval) const
Definition: UT_Map.h:169
Parent::iterator iterator
Definition: UT_StringMap.h:52
Sorted map container.
Definition: UT_Map.h:281
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:59
Parent::iterator iterator
Definition: UT_StringMap.h:88
Same as UT_StringMap, but keeps the strings in a sorted order.
Definition: UT_StringMap.h:82
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:58
void erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:95
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:51
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
Parent::size_type size_type
Definition: UT_StringMap.h:53
Base::const_iterator const_iterator
Definition: UT_Map.h:118
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:87
GLint GLsizei count
Definition: glcorearb.h:405
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
bool contains(const key_type &key) const
Returns true if a value with the key is contained in the map.
Definition: UT_Map.h:159
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
Definition: UT_StringMap.h:34