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