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_Compiler.h>
21 #include <SYS/SYS_Types.h>
22 #include <SYS/SYS_Hash.h>
23 
24 #include <stdexcept>
25 
26 class UT_JSONValue;
27 class UT_JSONValueArray;
28 class UT_JSONWriter;
29 
30 /// @brief UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects
31 ///
32 /// UT_JSONValueMap stores a map of UT_JSONValue objects. Unlike a standard
33 /// Houdini UT_StringMap, this object maintains the sort order in which
34 /// objects were added.
35 ///
36 /// @see UT_JSONValue, UT_JSONValueArray
38 public:
40  ~UT_JSONValueMap();
41 
42  /// Reference count (for shared maps).
43  void bumpRef(int d)
44  {
45  if (!myRef.add(d))
46  delete this;
47  }
48  /// Get the number of references to this map.
49  SYS_NO_DISCARD_RESULT int getRef() const { return myRef.relaxedLoad(); }
50 
51  /// Save to an output stream
52  bool save(UT_JSONWriter &os) const;
53 
54  /// Dump to stdout (same as save to std::cout)
55  void dump() const;
56 
57  /// Get the list of keys
58  int64 getKeys(UT_StringArray &keys) const;
59 
60  /// Get a list of the keys. However, rather than duplicating the string
61  /// values, this method simply keeps shallow references. While it's more
62  /// efficient, it's also relies on the fact that the map will not change
63  /// while the string array exists.
64  int64 getKeyReferences(UT_StringArray &keys) const;
65 
66  /// Return the number of entries in the map
68  { return myArray.entries(); }
69 
70  /// Clear the entries in the map.
71  void clear();
72 
73  /// Access const entry by index
75  { return get(i); }
76  /// Access entry by index
78  { return get(i); }
79  /// Access const entry by name (may be NULL pointer if k is not a key)
81  { return get(k); }
82  /// Access entry by name (may be NULL pointer if k is not a key)
84  { return get(k); }
85 
86  /// Access const entry by index
88  { return get(i); }
89  /// Access entry by index
91  { return get(i); }
92  /// Access const entry by name (may be NULL pointer if k is not a key)
94  { return get(k); }
95  /// Access entry by name (may be NULL pointer if k is not a key)
97  { return get(k); }
98 
99  /// Access a const entry by index
101  { return myArray(i); }
102  /// Access an entry by index
104  { return myArray(i); }
105  /// Access const entry by name (may be NULL pointer if k is not a key)
106  SYS_NO_DISCARD_RESULT const UT_JSONValue *get(const UT_StringRef &k) const;
107  /// Access entry by name (may be NULL pointer if k is not a key)
109 
110  /// Access entry by name. If not found the default value is returned.
111  SYS_NO_DISCARD_RESULT int32 get(const UT_StringRef& key, int32 def_value) const;
112  /// Access entry by name. If not found the default value is returned.
113  SYS_NO_DISCARD_RESULT int64 get(const UT_StringRef& key, int64 def_value) const;
114  /// Access entry by name. If not found the default value is returned.
115  SYS_NO_DISCARD_RESULT UT_StringHolder get(const UT_StringRef& key, const UT_StringRef& def_value) const;
116  /// Access entry by name. If not found the default value is returned.
117  SYS_NO_DISCARD_RESULT UT_StringArray get(const UT_StringRef& key, const UT_StringArray& def_value) const;
118 
119  /// Access const object entry by name (may be NULL pointer if k is not a key)
120  /// This is a convenience function to get() if we know the key is an object.
121  SYS_NO_DISCARD_RESULT const UT_JSONValueMap *getObject(const UT_StringRef& k) const;
122  /// Access object entry by name (may be NULL pointer if k is not a key)
123  /// This is a convenience function to get() if we know the key is an object.
125  /// Access const array entry by name (may be NULL pointer if k is not a key)
126  /// This is a convenience function to get() if we know the key is an array.
127  SYS_NO_DISCARD_RESULT const UT_JSONValueArray* getArray(const UT_StringRef& k) const;
128  /// Access array entry by name (may be NULL pointer if k is not a key)
129  /// This is a convenience function to get() if we know the key is an array.
131 
132  /// Add key/value pair to the map. If the key already exists, the previous
133  /// value will be deleted.
134  int64 append(const UT_StringHolder &key, UT_JSONValue *v);
135  template <typename T>
136  int64 append(const UT_StringHolder& key, const T& value);
137 
138  /// Remove key/value pair from the map.
139  /// Do nothing if the key does not exist.
140  void remove(const UT_StringHolder &key);
141 
142  /// Rename a key in the map.
143  /// Do nothing if the old key does not exist.
144  /// If the old key exists and another entry uses the new key
145  /// then remove the other entry and rename the entry with the old key.
146  void rename(
147  const UT_StringHolder &old_key,
148  const UT_StringHolder &new_key);
149 
150  /// Adds a new array child to this map.
151  UT_JSONValueArray *addArrayChild(const UT_StringHolder &map_key);
152 
153  /// Adds a new map child to this map.
154  UT_JSONValueMap *addMapChild(const UT_StringHolder &map_key);
155 
156  /// Key/Value imports. See UT_JSONValue for all available import types.
157  /// Returns false if the key isn't in the map, or if the import from the
158  /// UT_JSONValue fails.
159  template <typename T>
160  bool import(const UT_StringRef &key, T &result) const;
161 
162  SYS_NO_DISCARD_RESULT SYS_HashType hash() const;
163  /// @{
164  /// Check equality
165  SYS_NO_DISCARD_RESULT bool operator==(const UT_JSONValueMap &arr) const;
167  { return !(*this == arr); }
168  /// @}
169 
170 
171 private:
172  using BaseMap = UT_ArrayStringMap<UT_JSONValue *>;
173 
175 
176  /// Iterator functionality so we can utilize C++11 range base for loops.
177  /// C++17 has deprecated inheritance from std::iterator.
178  /// We could easily promote myMap's iterators here but then we wouldn't
179  /// respect the 'append' order.
180 public:
181  class iterator
182  {
183  public:
184  iterator(exint _num, UT_JSONValueMap *_obj) : myIndex(_num), myObj(_obj){}
186  {
187  if (myIndex < myObj->myArray.entries())
188  ++myIndex;
189  return *this;
190  }
192  {
193  iterator retval = *this; ++(*this);
194  return retval;
195  }
197  {
198  if (myIndex > 0)
199  --myIndex;
200  return *this;
201  }
203  {
204  iterator retval = *this; --(*this);
205  return retval;
206  }
207  bool operator==(const iterator &other) const {return (myObj == other.myObj) && (myIndex == other.myIndex);}
208  bool operator!=(const iterator &other) const {return !(*this == other);}
210  {
211  return *myObj->getItemAt_(myIndex);
212  }
214  {
215  return myObj->getItemAt_(myIndex);
216  }
217 
218  // iterator traits
221  using pointer = const exint*;
222  using reference = const exint&;
223  using iterator_category = std::forward_iterator_tag;
224  private:
225  exint myIndex = 0;
226  UT_JSONValueMap *myObj;
227  };
228  iterator begin() { return iterator(0, this); }
229  iterator end() { return iterator(this->myArray.entries(), this); }
230  using const_iterator = const iterator;
231 
232 private:
233  UT_Array<UT_JSONValue *> myArray;
234  BaseMap myMap;
235  SYS_AtomicInt32 myRef;
236 
237  friend class iterator;
238 };
239 
240 #endif
241 
void bumpRef(int d)
Reference count (for shared maps).
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
int int32
Definition: SYS_Types.h:39
SYS_NO_DISCARD_RESULT const UT_JSONValue * operator()(int64 i) const
Access const entry by index.
SYS_NO_DISCARD_RESULT UT_JSONValue * operator[](const UT_StringRef &k)
Access entry by name (may be NULL pointer if k is not a key)
SYS_NO_DISCARD_RESULT int64 entries() const
Return the number of entries in the map.
BaseMap::value_type value_type
SYS_NO_DISCARD_RESULT UT_JSONValue * operator[](int64 i)
Access entry by index.
UT_JSONValueArray stores a list of UT_JSONValue objects.
SYS_NO_DISCARD_RESULT bool operator!=(const UT_JSONValueMap &arr) const
int64 exint
Definition: SYS_Types.h:125
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
#define UT_API
Definition: UT_API.h:14
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:35
BaseMap::value_type * operator->()
SYS_NO_DISCARD_RESULT 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
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
SYS_NO_DISCARD_RESULT const UT_JSONValue * operator[](int64 i) const
Access const entry by index.
const GLdouble * v
Definition: glcorearb.h:837
BaseMap::value_type & operator*()
long long int64
Definition: SYS_Types.h:116
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
iterator(exint _num, UT_JSONValueMap *_obj)
SYS_NO_DISCARD_RESULT const UT_JSONValue * operator()(const UT_StringRef &k) const
Access const entry by name (may be NULL pointer if k is not a key)
SYS_NO_DISCARD_RESULT UT_JSONValue * operator()(int64 i)
Access entry by index.
SYS_NO_DISCARD_RESULT UT_JSONValue * operator()(const UT_StringRef &k)
Access entry by name (may be NULL pointer if k is not a key)
OIIO_UTIL_API bool rename(string_view from, string_view to, std::string &err)
GLuint index
Definition: glcorearb.h:786
std::forward_iterator_tag iterator_category
SYS_NO_DISCARD_RESULT int getRef() const
Get the number of references to this map.
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:77
Definition: core.h:1131