HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #ifndef __UT_StringMap_h__
12 #define __UT_StringMap_h__
13 
14 #include "UT_API.h"
15 #include "UT_ArrayMap.h"
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 {
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  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
64  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
67  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
69  UT_STRINGREF_WRAPPER(size_type, erase, )
70  UT_STRINGREF_WRAPPER(iterator, find, )
71  UT_STRINGREF_WRAPPER(const_iterator, find, const)
72 };
73 
74 /// Same as UT_StringMap, but keeps the strings in a sorted order.
75 template <typename ITEM_T>
77 {
79 
80 public:
82  typedef typename Parent::iterator iterator;
83  typedef typename Parent::size_type size_type;
84 
85  // NOTE: When we compile against a more modern standard library on OSX, the
86  // signature can be changed to return an iterator to the following element
87  // (the signature was changed in C++11).
88  void erase(const_iterator pos) { Parent::erase(pos); }
89  void erase(const_iterator first, const_iterator last)
90  {
91  Parent::erase(first, last);
92  }
93 
94  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
95  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
96  UT_STRINGREF_WRAPPER(bool, contains, const)
97  UT_STRINGREF_WRAPPER(size_type, count, const)
98  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
99  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
100  UT_STRINGREF_WRAPPER(size_type, erase, )
101  UT_STRINGREF_WRAPPER(iterator, find, )
102  UT_STRINGREF_WRAPPER(const_iterator, find, const)
103  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
104  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
105  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
106  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
107 };
108 
109 template <typename ITEM_T>
110 class UT_ArrayStringMap : public UT::ArrayMap<UT_StringHolder, ITEM_T>
111 {
112 public:
114 
116  typedef typename Parent::iterator iterator;
117  typedef typename Parent::size_type size_type;
118 
119  // Expose the erase() overloads that take iterators.
120  // The erase(const UT_StringHolder&) overload is hidden in favour of
121  // erase(const UT_StringRef &) from UT_STRINGREF_WRAPPER.
122  iterator erase(iterator pos) { return Parent::erase(pos); }
123  iterator erase(iterator first, iterator last)
124  {
125  return Parent::erase(first, last);
126  }
127 
128  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
129  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
130  UT_STRINGREF_WRAPPER(bool, contains, const)
131  UT_STRINGREF_WRAPPER(size_type, count, const)
132  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
133  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
134  UT_STRINGREF_WRAPPER(size_type, erase, )
135  UT_STRINGREF_WRAPPER(iterator, find, )
136  UT_STRINGREF_WRAPPER(const_iterator, find, const)
137 };
138 
139 namespace UT {
140 template<typename ITEM_T>
142  : public DefaultClearer<typename UT_ArrayStringMap<ITEM_T>::Parent>
143 {};
144 }
145 
146 #undef UT_STRINGREF_WRAPPER
147 #undef UT_STRINGREF_WRAPPER_RANGE
148 
149 #endif
GLint first
Definition: glcorearb.h:404
void erase(const_iterator pos)
Definition: UT_StringMap.h:88
Unsorted map container.
Definition: UT_Map.h:83
Parent::size_type size_type
Definition: UT_StringMap.h:83
iterator erase(const_iterator pos)
Definition: UT_StringMap.h:57
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringMap.h:25
Parent::size_type size_type
Definition: UT_StringMap.h:52
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:58
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:115
iterator erase(iterator first, iterator last)
Definition: UT_StringMap.h:123
typename set_type::size_type size_type
Definition: UT_ArrayMap.h:82
Sorted map container.
Definition: UT_Map.h:244
Parent::iterator iterator
Definition: UT_StringMap.h:82
Same as UT_StringMap, but keeps the strings in a sorted order.
Definition: UT_StringMap.h:76
Parent::iterator iterator
Definition: UT_StringMap.h:116
void erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:89
GLint GLsizei count
Definition: glcorearb.h:404
UT::ArrayMap< UT_StringHolder, ITEM_T > Parent
Definition: UT_StringMap.h:113
typename set_type::iterator iterator
Inherit iterator and const_iterator.
Definition: UT_ArrayMap.h:85
iterator erase(iterator pos)
Definition: UT_StringMap.h:122
Parent::size_type size_type
Definition: UT_StringMap.h:117
#define const
Definition: zconf.h:214
Parent::iterator iterator
Definition: UT_StringMap.h:51
typename set_type::const_iterator const_iterator
Definition: UT_ArrayMap.h:88
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:81
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:50
bool contains(const key_type &key) const
Returns true if a value with the key is contained in the map.
Definition: UT_Map.h:141
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
Definition: UT_StringMap.h:33