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_StringHolder.h"
18 #include <SYS/SYS_AtomicInt.h>
19 #include <SYS/SYS_Types.h>
20 
21 class UT_JSONValue;
22 class UT_JSONValueArray;
23 class UT_JSONWriter;
24 class UT_StringArray;
25 
26 /// @brief UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects
27 ///
28 /// UT_JSONValueMap stores a map of UT_JSONValue objects. Unlike a standard
29 /// Houdini UT_StringMap, this object maintains the sort order in which
30 /// objects were added.
31 ///
32 /// @see UT_JSONValue, UT_JSONValueArray
34 public:
36  ~UT_JSONValueMap();
37 
38  /// Reference count (for shared maps).
39  void bumpRef(int d)
40  {
41  if (!myRef.add(d))
42  delete this;
43  }
44  /// Get the number of references to this map.
45  int getRef() const { return myRef.relaxedLoad(); }
46 
47  /// Save to an output stream
48  bool save(UT_JSONWriter &os) const;
49 
50  /// Dump to stdout (same as save to std::cout)
51  void dump() const;
52 
53  /// Get the list of keys
54  int64 getKeys(UT_StringArray &keys) const;
55 
56  /// Get a list of the keys. However, rather than duplicating the string
57  /// values, this method simply keeps shallow references. While it's more
58  /// efficient, it's also relies on the fact that the map will not change
59  /// while the string array exists.
60  int64 getKeyReferences(UT_StringArray &keys) const;
61 
62  /// Return the number of entries in the map
63  int64 entries() const
64  { return myArray.entries(); }
65 
66  /// Clear the entries in the map.
67  void clear();
68 
69  /// Access const entry by index
71  { return get(i); }
72  /// Access entry by index
74  { return get(i); }
75  /// Access const entry by name (may be NULL pointer if k is not a key)
76  const UT_JSONValue *operator()(const UT_StringRef &k) const
77  { return get(k); }
78  /// Access entry by name (may be NULL pointer if k is not a key)
80  { return get(k); }
81 
82  /// Access const entry by index
84  { return get(i); }
85  /// Access entry by index
87  { return get(i); }
88  /// Access const entry by name (may be NULL pointer if k is not a key)
89  const UT_JSONValue *operator[](const UT_StringRef &k) const
90  { return get(k); }
91  /// Access entry by name (may be NULL pointer if k is not a key)
93  { return get(k); }
94 
95  /// Access a const entry by index
96  const UT_JSONValue *get(int64 i) const
97  { return myArray(i); }
98  /// Access an entry by index
100  { return myArray(i); }
101  /// Access const entry by name (may be NULL pointer if k is not a key)
102  const UT_JSONValue *get(const UT_StringRef &k) const;
103  /// Access entry by name (may be NULL pointer if k is not a key)
104  UT_JSONValue *get(const UT_StringRef &k);
105 
106  /// Add key/value pair to the map. If the key already exists, the previous
107  /// value will be deleted.
108  int64 append(const UT_StringHolder &key, UT_JSONValue *v);
109 
110  /// Remove key/value pair from the map.
111  /// Do nothing if the key does not exist.
112  void remove(const UT_StringHolder &key);
113 
114  /// Rename a key in the map.
115  /// Do nothing if the old key does not exist.
116  /// If the old key exists and another entry uses the new key
117  /// then remove the other entry and rename the entry with the old key.
118  void rename(
119  const UT_StringHolder &old_key,
120  const UT_StringHolder &new_key);
121 
122  /// Adds a new array child to this map.
123  UT_JSONValueArray *addArrayChild(const UT_StringHolder &map_key);
124 
125  /// Adds a new map child to this map.
126  UT_JSONValueMap *addMapChild(const UT_StringHolder &map_key);
127 
128  /// Key/Value imports. This is implemented for T in:
129  /// - bool
130  /// - int64
131  /// - fpreal64
132  /// - UT_WorkBuffer/UT_StringHolder
133  /// - UT_Int64Array
134  /// - UT_Fpreal64Array
135  /// Returns false if the key isn't in the map, or if the import from the
136  /// UT_JSONValue fails.
137  template <typename T>
138  bool import(const UT_StringRef &key, T &result) const;
139 
140 private:
141  UT_Array<UT_JSONValue *> myArray;
143  SYS_AtomicInt32 myRef;
144 };
145 
146 #endif
147 
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.
const GLdouble * v
Definition: glcorearb.h:836
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.
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 UT_JSONValue * operator()(const UT_StringRef &k) const
Access const entry by name (may be NULL pointer if k is not a key)
png_uint_32 i
Definition: png.h:2877
const UT_JSONValue * operator()(int64 i) const
Access const entry by index.
long long int64
Definition: SYS_Types.h:107
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)
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:75
int getRef() const
Get the number of references to this map.