HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_JSONValueMap.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_JSONValueMap.h ( UT Library, C++)
7  *
8  * COMMENTS: The order is maintained as objects are added to the map
9  */
10 
11 #ifndef __UT_JSONValueMap__
12 #define __UT_JSONValueMap__
13 
14 #include "UT_API.h"
15 #include "UT_Array.h"
16 #include "UT_ArrayStringMap.h"
17 #include "UT_StringArray.h"
18 #include "UT_StringHolder.h"
19 #include <SYS/SYS_AtomicInt.h>
20 #include <SYS/SYS_Types.h>
21 #include <SYS/SYS_Hash.h>
22 
23 class UT_JSONValue;
24 class UT_JSONValueArray;
25 class UT_JSONWriter;
26 
27 /// @brief UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects
28 ///
29 /// UT_JSONValueMap stores a map of UT_JSONValue objects. Unlike a standard
30 /// Houdini UT_StringMap, this object maintains the sort order in which
31 /// objects were added.
32 ///
33 /// @see UT_JSONValue, UT_JSONValueArray
35 public:
37  ~UT_JSONValueMap();
38 
39  /// Reference count (for shared maps).
40  void bumpRef(int d)
41  {
42  if (!myRef.add(d))
43  delete this;
44  }
45  /// Get the number of references to this map.
46  int getRef() const { return myRef.relaxedLoad(); }
47 
48  /// Save to an output stream
49  bool save(UT_JSONWriter &os) const;
50 
51  /// Dump to stdout (same as save to std::cout)
52  void dump() const;
53 
54  /// Get the list of keys
55  int64 getKeys(UT_StringArray &keys) const;
56 
57  /// Get a list of the keys. However, rather than duplicating the string
58  /// values, this method simply keeps shallow references. While it's more
59  /// efficient, it's also relies on the fact that the map will not change
60  /// while the string array exists.
61  int64 getKeyReferences(UT_StringArray &keys) const;
62 
63  /// Return the number of entries in the map
64  int64 entries() const
65  { return myArray.entries(); }
66 
67  /// Clear the entries in the map.
68  void clear();
69 
70  /// Access const entry by index
71  const UT_JSONValue *operator()(int64 i) const
72  { return get(i); }
73  /// Access entry by index
75  { return get(i); }
76  /// Access const entry by name (may be NULL pointer if k is not a key)
77  const UT_JSONValue *operator()(const UT_StringRef &k) const
78  { return get(k); }
79  /// Access entry by name (may be NULL pointer if k is not a key)
81  { return get(k); }
82 
83  /// Access const entry by index
84  const UT_JSONValue *operator[](int64 i) const
85  { return get(i); }
86  /// Access entry by index
88  { return get(i); }
89  /// Access const entry by name (may be NULL pointer if k is not a key)
90  const UT_JSONValue *operator[](const UT_StringRef &k) const
91  { return get(k); }
92  /// Access entry by name (may be NULL pointer if k is not a key)
94  { return get(k); }
95 
96  /// Access a const entry by index
97  const UT_JSONValue *get(int64 i) const
98  { return myArray(i); }
99  /// Access an entry by index
101  { return myArray(i); }
102  /// Access const entry by name (may be NULL pointer if k is not a key)
103  const UT_JSONValue *get(const UT_StringRef &k) const;
104  /// Access entry by name (may be NULL pointer if k is not a key)
105  UT_JSONValue *get(const UT_StringRef &k);
106 
107  /// Add key/value pair to the map. If the key already exists, the previous
108  /// value will be deleted.
109  int64 append(const UT_StringHolder &key, UT_JSONValue *v);
110 
111  /// Remove key/value pair from the map.
112  /// Do nothing if the key does not exist.
113  void remove(const UT_StringHolder &key);
114 
115  /// Rename a key in the map.
116  /// Do nothing if the old key does not exist.
117  /// If the old key exists and another entry uses the new key
118  /// then remove the other entry and rename the entry with the old key.
119  void rename(
120  const UT_StringHolder &old_key,
121  const UT_StringHolder &new_key);
122 
123  /// Adds a new array child to this map.
124  UT_JSONValueArray *addArrayChild(const UT_StringHolder &map_key);
125 
126  /// Adds a new map child to this map.
127  UT_JSONValueMap *addMapChild(const UT_StringHolder &map_key);
128 
129  /// Key/Value imports. This is implemented for T in:
130  /// - bool
131  /// - int64
132  /// - fpreal64
133  /// - UT_WorkBuffer/UT_StringHolder
134  /// - UT_Int64Array
135  /// - UT_Fpreal64Array
136  /// Returns false if the key isn't in the map, or if the import from the
137  /// UT_JSONValue fails.
138  template <typename T>
139  bool import(const UT_StringRef &key, T &result) const;
140 
141  SYS_HashType hash() const;
142  /// @{
143  /// Check equality
144  bool operator==(const UT_JSONValueMap &arr) const;
145  bool operator!=(const UT_JSONValueMap &arr) const
146  { return !(*this == arr); }
147  /// @}
148 
149 
150 private:
151  using BaseMap = UT_ArrayStringMap<UT_JSONValue *>;
152 
153  /// Iterator functionality so we can utilize C++11 range base for loops.
154  /// C++17 has deprecated inheritance from std::iterator.
155  /// We could easily promote myMap's iterators here but then we wouldn't
156  /// respect the 'append' order.
157 public:
158  class iterator
159  {
160  public:
161  iterator(exint _num, UT_JSONValueMap *_obj) : myIndex(_num), myObj(_obj){}
163  {
164  if (myIndex < myObj->myArray.entries())
165  ++myIndex;
166  return *this;
167  }
169  {
170  iterator retval = *this; ++(*this);
171  return retval;
172  }
174  {
175  if (myIndex > 0)
176  --myIndex;
177  return *this;
178  }
180  {
181  iterator retval = *this; --(*this);
182  return retval;
183  }
184  bool operator==(const iterator &other) const {return (myObj == other.myObj) && (myIndex == other.myIndex);}
185  bool operator!=(const iterator &other) const {return !(*this == other);}
187  {
188  // ordering is strictly based on the myArray order.
189  UT_StringArray keys;
190  myObj->getKeys(keys);
191  return *myObj->myMap.find(keys[myIndex]);
192  }
194  {
195  // ordering is strictly based on the myArray order.
196  UT_StringArray keys;
197  myObj->getKeys(keys);
198  return myObj->myMap.find(keys[myIndex]);
199  }
200 
201  // iterator traits
204  using pointer = const exint*;
205  using reference = const exint&;
206  using iterator_category = std::forward_iterator_tag;
207  private:
208  exint myIndex = 0;
209  UT_JSONValueMap *myObj;
210  };
211  iterator begin() { return iterator(0, this); }
212  iterator end() { return iterator(this->myArray.entries(), this); }
213  using const_iterator = const iterator;
214 
215 private:
216  UT_Array<UT_JSONValue *> myArray;
218  SYS_AtomicInt32 myRef;
219 
220  friend class iterator;
221 };
222 
223 #endif
224 
UT_JSONValue * operator()(int64 i)
Access entry by index.
UT_JSONValue * operator[](const UT_StringRef &k)
Access entry by name (may be NULL pointer if k is not a key)
void bumpRef(int d)
Reference count (for shared maps).
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
BaseMap::value_type value_type
OIIO_API bool rename(string_view from, string_view to, std::string &err)
UT_JSONValue * operator[](int64 i)
Access entry by index.
UT_JSONValue * operator()(const UT_StringRef &k)
Access entry by name (may be NULL pointer if k is not a key)
UT_JSONValueArray stores a list of UT_JSONValue objects.
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
const UT_JSONValue * operator[](int64 i) const
Access const entry by index.
#define UT_API
Definition: UT_API.h:13
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
const GLdouble * v
Definition: glew.h:1391
BaseMap::value_type * operator->()
const UT_JSONValue * operator()(const UT_StringRef &k) const
Access const entry by name (may be NULL pointer if k is not a key)
uint64 value_type
Definition: GA_PrimCompat.h:29
const UT_JSONValue * operator()(int64 i) const
Access const entry by index.
exint find(const UT_StringRef &str, exint s=0) const
bool operator==(const iterator &other) const
bool operator!=(const iterator &other) const
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
long long int64
Definition: SYS_Types.h:111
int64 exint
Definition: SYS_Types.h:120
BaseMap::value_type & operator*()
iterator(exint _num, UT_JSONValueMap *_obj)
int64 entries() const
Return the number of entries in the map.
const UT_JSONValue * operator[](const UT_StringRef &k) const
Access const entry by name (may be NULL pointer if k is not a key)
std::forward_iterator_tag iterator_category
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:76
GLuint64EXT * result
Definition: glew.h:14007
bool operator!=(const UT_JSONValueMap &arr) const
int getRef() const
Get the number of references to this map.