HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_JSONValueArray.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_JSONValueArray.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_JSONValueArray__
12 #define __UT_JSONValueArray__
13 
14 #include "UT_API.h"
15 #include "UT_Array.h"
16 #include "UT_JSONValue.h"
17 #include <SYS/SYS_AtomicInt.h>
18 #include <SYS/SYS_Compiler.h>
19 #include <SYS/SYS_Hash.h>
20 #include <SYS/SYS_Inline.h>
21 #include <SYS/SYS_Types.h>
22 
23 class UT_JSONValue;
24 class UT_JSONWriter;
25 class UT_JSONValueMap;
26 struct UT_JSONFindResult;
27 class UT_StringArray;
28 
29 /// @brief UT_JSONValueArray stores a list of UT_JSONValue objects
30 ///
31 /// UT_JSONValueArray stores a list of UT_JSONValue objects.
32 ///
33 /// @see UT_JSONValue, UT_JSONValueMap
35 {
36 public:
39 
40  /// Reference count (for shared arrays).
41  void bumpRef(int d)
42  {
43  if (!myRef.add(d))
44  delete this;
45  }
46  /// Get the number of references to this array
47  SYS_FORCE_INLINE int getRef() const { return myRef.relaxedLoad(); }
48 
49  /// Save to an output stream
50  bool save(UT_JSONWriter &os) const;
51 
52  /// Dump to stdout (same as save to std::cout)
53  void dump() const;
54 
55  SYS_HashType hash() const;
56  /// @{
57  /// Check equality
58  bool operator==(const UT_JSONValueArray &arr) const;
59  bool operator!=(const UT_JSONValueArray &arr) const
60  { return !(*this == arr); }
61  /// @}
62 
63  /// Arrays are typically stored as shared pointers. This method will make a
64  /// unique copy. May return "this" (if the reference count is already unique).
65  UT_JSONValueArray *makeUnique() const;
66 
67  /// Return size of the array
68  SYS_FORCE_INLINE int64 size() const { return myArray.size(); }
69  /// Return the number of entries in the array
70  SYS_FORCE_INLINE int64 entries() const { return myArray.size(); }
71  /// Access a const entry by index
72  SYS_FORCE_INLINE const UT_JSONValue *operator()(int64 i) const { return myArray[i]; }
73  /// Access an entry by index
74  SYS_FORCE_INLINE UT_JSONValue *operator()(int64 i) { return myArray[i]; }
75  /// Access a const entry by index
76  SYS_FORCE_INLINE const UT_JSONValue *operator[](int64 i) const { return myArray[i]; }
77  /// Access an entry by index
78  SYS_FORCE_INLINE UT_JSONValue *operator[](int64 i) { return myArray[i]; }
79 
80  /// Access a const entry by index
81  SYS_FORCE_INLINE const UT_JSONValue *get(int64 i) const { return myArray[i]; }
82  /// Access an entry by index
83  SYS_FORCE_INLINE UT_JSONValue *get(int64 i) { return myArray[i]; }
84  /// Access a const object by index
85  SYS_NO_DISCARD_RESULT const UT_JSONValueMap* getObject(int64 i) const;
86  /// Access an object by index
88  /// Access a const array by index
89  SYS_NO_DISCARD_RESULT const UT_JSONValueArray* getArray(int64 i) const;
90  /// Access an array by index
92 
93  /// Import an array of integer values (fails if the array size is too small
94  /// or if one of the elements of the array cannot be interpreted as an
95  /// integer).
96  bool import(int64 *result, int64 size) const;
97  /// Import an array of float values (fails if the array size is too small
98  /// or if one of the elements of the array cannot be interpreted as an
99  /// integer).
100  bool import(fpreal64 *result, int64 size) const;
101 
102  /// Import an array of integer values (fails if the elements of the array
103  /// cannot be interpreted as an integer).
104  bool import(UT_Array<int64> &array) const;
105  /// Import an array of real values (fails if the elements of the array
106  /// cannot be interpreted as such).
107  bool import(UT_Array<fpreal64> &array) const;
108  /// Import an array of string values (fails if the elements of the array
109  /// cannot be interpreted as such).
110  bool import(UT_StringArray &array) const;
111 
113  {
114  UT_JSONValue *operator->() { return myValue; }
115  UT_JSONValue &operator*() { return *myValue; }
116  int64 index() const { return myIndex; }
117  UT_JSONValue *value() const { return myValue; }
118 
121  };
122 
123  /// Add an element to the array
124  AppendResult append(const UT_JSONValue &v);
125 
126  /// Emplace an element in the array
127  template <typename... ARGS>
129  {
130  return append(UT_JSONValue(std::forward<ARGS>(args)...));
131  }
132 
133 
134  /// Remove and delete an element by index.
135  void remove(int64 index);
136 
137  /// Remove and delete an element.
138  void remove(const UT_JSONFindResult &v);
139 
140  /// Remove and delete the last added element.
141  void removeLast();
142 
143  /// Remove and delete all elements.
144  void clear();
145 
146  /// Inserts a value v before the given element after_value.
147  AppendResult insertBefore(const UT_JSONValue &v,
148  const UT_JSONFindResult &after_value);
149 
150  /// Adds a new array child to this map.
151  UT_JSONValueArray *appendArrayChild();
152 
153  /// Adds a new map child to this map.
154  UT_JSONValueMap *appendMapChild();
155 
157  const_iterator begin() const { return myArray.begin(); }
158  const_iterator end() const { return myArray.end(); }
159 
160 private:
161  template <typename ARRAY_T>
162  bool importArray(ARRAY_T &array) const;
163 
164 private:
165  UT_Array<UT_JSONValue *> myArray;
166  SYS_AtomicInt32 myRef;
167 };
168 
169 #endif
void bumpRef(int d)
Reference count (for shared arrays).
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
const GLdouble * v
Definition: glcorearb.h:837
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
#define UT_API
Definition: UT_API.h:14
SYS_FORCE_INLINE const UT_JSONValue * operator()(int64 i) const
Access a const entry by index.
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
**But if you need a result
Definition: thread.h:622
bool operator!=(const UT_JSONValueArray &arr) const
SYS_FORCE_INLINE const UT_JSONValue * operator[](int64 i) const
Access a const entry by index.
const_iterator end() const
AppendResult emplace(ARGS &&...args)
Emplace an element in the array.
double fpreal64
Definition: SYS_Types.h:201
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
const_iterator begin() const
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
UT_JSONValue * value() const
SYS_FORCE_INLINE int64 entries() const
Return the number of entries in the array.
SYS_FORCE_INLINE UT_JSONValue * operator[](int64 i)
Access an entry by index.
GLsizeiptr size
Definition: glcorearb.h:664
GLuint index
Definition: glcorearb.h:786
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
**If you just want to fire and args
Definition: thread.h:618
SYS_FORCE_INLINE UT_JSONValue * operator()(int64 i)
Access an entry by index.
UT_Array< UT_JSONValue * >::const_iterator const_iterator
SYS_FORCE_INLINE int getRef() const
Get the number of references to this array.
SYS_FORCE_INLINE int64 size() const
Return size of the array.