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  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
65  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
68  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
70  UT_STRINGREF_WRAPPER(size_type, erase, )
71  UT_STRINGREF_WRAPPER(iterator, find, )
72  UT_STRINGREF_WRAPPER(const_iterator, find, const)
73 };
74 
75 /// Same as UT_StringMap, but keeps the strings in a sorted order.
76 template <typename ITEM_T>
78 {
80 
81 public:
83  typedef typename Parent::iterator iterator;
84  typedef typename Parent::size_type size_type;
85 
86  // NOTE: When we compile against a more modern standard library on OSX, the
87  // signature can be changed to return an iterator to the following element
88  // (the signature was changed in C++11).
89  void erase(const_iterator pos) { Parent::erase(pos); }
90  void erase(const_iterator first, const_iterator last)
91  {
92  Parent::erase(first, last);
93  }
94 
95  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
96  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
97  UT_STRINGREF_WRAPPER(bool, contains, const)
98  UT_STRINGREF_WRAPPER(size_type, count, const)
99  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
100  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
101  UT_STRINGREF_WRAPPER(size_type, erase, )
102  UT_STRINGREF_WRAPPER(iterator, find, )
103  UT_STRINGREF_WRAPPER(const_iterator, find, const)
104  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
105  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
106  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
107  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
108 };
109 
110 #undef UT_STRINGREF_WRAPPER
111 #undef UT_STRINGREF_WRAPPER_RANGE
112 
113 #endif
GLint first
Definition: glcorearb.h:404
void erase(const_iterator pos)
Definition: UT_StringMap.h:89
Unsorted map container.
Definition: UT_Map.h:83
Parent::size_type size_type
Definition: UT_StringMap.h:84
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringMap.h:26
Parent::iterator iterator
Definition: UT_StringMap.h:52
Sorted map container.
Definition: UT_Map.h:244
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:59
Parent::iterator iterator
Definition: UT_StringMap.h:83
Same as UT_StringMap, but keeps the strings in a sorted order.
Definition: UT_StringMap.h:77
iterator erase(const_iterator pos)
Definition: UT_StringMap.h:58
void erase(const_iterator first, const_iterator last)
Definition: UT_StringMap.h:90
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:51
GLint GLsizei count
Definition: glcorearb.h:404
Parent::size_type size_type
Definition: UT_StringMap.h:53
#define const
Definition: zconf.h:214
Base::const_iterator const_iterator
Definition: UT_Map.h:94
Parent::const_iterator const_iterator
Definition: UT_StringMap.h:82
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:34