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 public:
38 
39  /// Reference count (for shared arrays).
40  void bumpRef(int d)
41  {
42  if (!myRef.add(d))
43  delete this;
44  }
45  /// Get the number of references to this array
46  SYS_FORCE_INLINE 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  SYS_HashType hash() const;
55  /// @{
56  /// Check equality
57  bool operator==(const UT_JSONValueArray &arr) const;
58  bool operator!=(const UT_JSONValueArray &arr) const
59  { return !(*this == arr); }
60  /// @}
61 
62  /// Arrays are typically stored as shared pointers. This method will make a
63  /// unique copy. May return "this" (if the reference count is already unique).
64  UT_JSONValueArray *makeUnique() const;
65 
66  /// Return size of the array
67  SYS_FORCE_INLINE int64 size() const { return myArray.size(); }
68  /// Return the number of entries in the array
69  SYS_FORCE_INLINE int64 entries() const { return myArray.size(); }
70  /// Access a const entry by index
71  SYS_FORCE_INLINE const UT_JSONValue *operator()(int64 i) const { return myArray[i]; }
72  /// Access an entry by index
73  SYS_FORCE_INLINE UT_JSONValue *operator()(int64 i) { return myArray[i]; }
74  /// Access a const entry by index
75  SYS_FORCE_INLINE const UT_JSONValue *operator[](int64 i) const { return myArray[i]; }
76  /// Access an entry by index
77  SYS_FORCE_INLINE UT_JSONValue *operator[](int64 i) { return myArray[i]; }
78 
79  /// Access a const entry by index
80  SYS_FORCE_INLINE const UT_JSONValue *get(int64 i) const { return myArray[i]; }
81  /// Access an entry by index
82  SYS_FORCE_INLINE UT_JSONValue *get(int64 i) { return myArray[i]; }
83  /// Access a const object by index
84  SYS_NO_DISCARD_RESULT const UT_JSONValueMap* getObject(int64 i) const;
85  /// Access an object by index
87  /// Access a const array by index
88  SYS_NO_DISCARD_RESULT const UT_JSONValueArray* getArray(int64 i) const;
89  /// Access an array by index
91 
92  /// Import an array of integer values (fails if the array size is too small
93  /// or if one of the elements of the array cannot be interpreted as an
94  /// integer).
95  bool import(int64 *result, int64 size) const;
96  /// Import an array of float values (fails if the array size is too small
97  /// or if one of the elements of the array cannot be interpreted as an
98  /// integer).
99  bool import(fpreal64 *result, int64 size) const;
100 
101  /// Import an array of integer values (fails if the elements of the array
102  /// cannot be interpreted as an integer).
103  bool import(UT_Array<int64> &array) const;
104  /// Import an array of real values (fails if the elements of the array
105  /// cannot be interpreted as such).
106  bool import(UT_Array<fpreal64> &array) const;
107  /// Import an array of string values (fails if the elements of the array
108  /// cannot be interpreted as such).
109  bool import(UT_StringArray &array) const;
110 
112  {
113  UT_JSONValue *operator->() { return myValue; }
114  UT_JSONValue &operator*() { return *myValue; }
115  int64 index() const { return myIndex; }
116  UT_JSONValue *value() const { return myValue; }
117 
120  };
121 
122  /// Add an element to the array
123  AppendResult append(const UT_JSONValue &v);
124 
125  /// Emplace an element in the array
126  template <typename... ARGS>
128  {
129  return append(UT_JSONValue(std::forward<ARGS>(args)...));
130  }
131 
132 
133  /// Remove and delete an element by index.
134  void remove(int64 index);
135 
136  /// Remove and delete an element.
137  void remove(const UT_JSONFindResult &v);
138 
139  /// Remove and delete the last added element.
140  void removeLast();
141 
142  /// Remove and delete all elements.
143  void clear();
144 
145  /// Inserts a value v before the given element after_value.
146  AppendResult insertBefore(const UT_JSONValue &v,
147  const UT_JSONFindResult &after_value);
148 
149  /// Adds a new array child to this map.
150  UT_JSONValueArray *appendArrayChild();
151 
152  /// Adds a new map child to this map.
153  UT_JSONValueMap *appendMapChild();
154 
156  const_iterator begin() const { return myArray.begin(); }
157  const_iterator end() const { return myArray.end(); }
158 
159 private:
160  template <typename ARRAY_T>
161  bool importArray(ARRAY_T &array) const;
162 
163 private:
164  UT_Array<UT_JSONValue *> myArray;
165  SYS_AtomicInt32 myRef;
166 };
167 
168 #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:613
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
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
**If you just want to fire and args
Definition: thread.h:609
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.