HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
value.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_JS_VALUE_H
8 #define PXR_BASE_JS_VALUE_H
9 
10 /// \file js/value.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/js/api.h"
14 #include "pxr/base/js/types.h"
15 
16 #include <algorithm>
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <type_traits>
21 #include <vector>
22 
24 
25 // Value API Version
26 // 1 (or undefined) - Initial version.
27 // 2 - Changed Get{Array,Object} to GetJs{Array,Object}.
28 #define JS_VALUE_API_VERSION 2
29 
30 /// \class JsValue
31 ///
32 /// A discriminated union type for JSON values. A JsValue may contain one of
33 /// the following types:
34 ///
35 /// \li JsObject, a dictionary type
36 /// \li JsArray, a vector type
37 /// \li std::string
38 /// \li bool
39 /// \li int64_t
40 /// \li uint64_t
41 /// \li double
42 /// \li null
43 ///
44 class JsValue
45 {
46 public:
47  /// Type held by this JSON value.
48  enum Type {
56  };
57 
58  /// Constructs a null value.
59  JS_API JsValue();
60 
61  /// Constructs a value holding the given object.
62  JS_API JsValue(const JsObject& value);
63 
64  /// Constructs a value holding the given object rvalue reference.
66 
67  /// Constructs a value holding the given array.
68  JS_API JsValue(const JsArray& value);
69 
70  /// Constructs a value holding the given array rvalue reference.
72 
73  /// Constructs a value holding the given char array as a std::string.
74  JS_API explicit JsValue(const char* value);
75 
76  /// Constructs a value holding the given std::string.
77  JS_API explicit JsValue(const std::string& value);
78 
79  /// Constructs a value holding the given std::string rvalue reference.
80  JS_API explicit JsValue(std::string&& value);
81 
82  /// Constructs a value holding a bool.
83  JS_API explicit JsValue(bool value);
84 
85  /// Constructs a value holding a signed integer.
86  JS_API explicit JsValue(int value);
87 
88  /// Constructs a value holding a 64-bit signed integer.
89  JS_API explicit JsValue(int64_t value);
90 
91  /// Constructs a value holding a 64-bit unsigned integer.
92  JS_API explicit JsValue(uint64_t value);
93 
94  /// Constructs a value holding a double.
95  JS_API explicit JsValue(double value);
96 
97  /// Returns the object held by this value. If this value is not holding an
98  /// object, this method raises a coding error and an empty object is
99  /// returned.
100  JS_API const JsObject& GetJsObject() const;
101 
102  /// Returns the array held by this value. If this value is not holding an
103  /// array, this method raises a coding error and an empty array is
104  /// returned.
105  JS_API const JsArray& GetJsArray() const;
106 
107  /// Returns the string held by this value. If this value is not holding a
108  /// string, this method raises a coding error and an empty string is
109  /// returned.
110  JS_API const std::string& GetString() const;
111 
112  /// Returns the bool held by this value. If this value is not holding a
113  /// bool, this method raises a coding error and false is returned.
114  JS_API bool GetBool() const;
115 
116  /// Returns the integer held by this value. If this value is not holding
117  /// an int, this method raises a coding error and zero is returned. If the
118  /// value is holding a 64-bit integer larger than the platform int may
119  /// hold, the value is truncated.
120  JS_API int GetInt() const;
121 
122  /// Returns the 64-bit integer held by this value. If this value is not
123  /// holding a 64-bit integer, this method raises a coding error and zero
124  /// is returned.
125  JS_API int64_t GetInt64() const;
126 
127  /// Returns the 64-bit unsigned integer held by this value. If this value
128  /// is not holding a 64-bit unsigned integer, this method raises a coding
129  /// error and zero is returned.
130  JS_API uint64_t GetUInt64() const;
131 
132  /// Returns the double held by this value. If this value is not holding a
133  /// double, this method raises a coding error and zero is returned.
134  JS_API double GetReal() const;
135 
136  /// Returns the value corresponding to the C++ type specified in the
137  /// template parameter if it is holding such a value. Calling this
138  /// function with C++ type T is equivalent to calling the specific Get
139  /// function above that returns a value or reference to a type T.
140  ///
141  /// If a value corresponding to the C++ type is not being held, this
142  /// method raises a coding error. See Get functions above for default
143  /// value returned in this case.
144  template <typename T,
145  typename ReturnType = typename std::conditional<
149  const T&, T>::type>
150  ReturnType Get() const {
151  return _Get(static_cast<T*>(nullptr));
152  }
153 
154  /// Returns a vector holding the elements of this value's array that
155  /// correspond to the C++ type specified as the template parameter.
156  /// If this value is not holding an array, an empty vector is returned.
157  /// If any of the array's elements does not correspond to the C++ type,
158  /// it is replaced with the default value used by the Get functions above.
159  /// In both cases, a coding error will be raised.
160  template <typename T>
161  std::vector<T> GetArrayOf() const;
162 
163  /// Returns the type of this value.
164  JS_API Type GetType() const;
165 
166  /// Returns a display name for the type of this value.
167  JS_API std::string GetTypeName() const;
168 
169  /// Returns true if this value is holding an object type.
170  JS_API bool IsObject() const;
171 
172  /// Returns true if this value is holding an array type.
173  JS_API bool IsArray() const;
174 
175  /// Returns true if this value is holding a string type.
176  JS_API bool IsString() const;
177 
178  /// Returns true if this value is holding a boolean type.
179  JS_API bool IsBool() const;
180 
181  /// Returns true if this value is holding an integer type.
182  JS_API bool IsInt() const;
183 
184  /// Returns true if this value is holding a real type.
185  JS_API bool IsReal() const;
186 
187  /// Returns true if this value is holding a 64-bit unsigned integer.
188  JS_API bool IsUInt64() const;
189 
190  /// Returns true if this value is holding a type that corresponds
191  /// to the C++ type specified as the template parameter.
192  template <typename T>
193  bool Is() const {
194  return _Is(static_cast<T*>(nullptr));
195  }
196 
197  /// Returns true if this value is holding an array whose elements all
198  /// correspond to the C++ type specified as the template parameter.
199  template <typename T>
200  bool IsArrayOf() const;
201 
202  /// Returns true if this value is null, false otherwise.
203  JS_API bool IsNull() const;
204 
205  /// Evaluates to true if this value is not null.
206  JS_API explicit operator bool() const;
207 
208  /// Returns true of both values hold the same type and the underlying held
209  /// values are equal.
210  JS_API bool operator==(const JsValue& other) const;
211 
212  /// Returns true if values are of different type, or the underlying held
213  /// values are not equal.
214  JS_API bool operator!=(const JsValue& other) const;
215 
216 private:
217  template <typename T>
218  struct _InvalidTypeHelper : public std::false_type { };
219 
220  template <class T>
221  T _Get(T*) const {
222  static_assert(_InvalidTypeHelper<T>::value,
223  "Invalid type for JsValue");
224  return T();
225  }
226 
227  const JsObject& _Get(JsObject*) const { return GetJsObject(); }
228  const JsArray& _Get(JsArray*) const { return GetJsArray(); }
229  const std::string& _Get(std::string*) const { return GetString(); }
230  bool _Get(bool*) const { return GetBool(); }
231  int _Get(int*) const { return GetInt(); }
232  int64_t _Get(int64_t*) const { return GetInt64(); }
233  uint64_t _Get(uint64_t*) const { return GetUInt64(); }
234  double _Get(double*) const { return GetReal(); }
235 
236  template <class T>
237  bool _Is(T*) const {
238  static_assert(_InvalidTypeHelper<T>::value,
239  "Invalid type for JsValue");
240  return false;
241  }
242 
243  bool _Is(JsObject*) const { return IsObject(); }
244  bool _Is(JsArray*) const { return IsArray(); }
245  bool _Is(std::string*) const { return IsString(); }
246  bool _Is(bool*) const { return IsBool(); }
247  bool _Is(int*) const { return IsInt(); }
248  bool _Is(int64_t*) const { return IsInt(); }
249  bool _Is(uint64_t*) const { return IsUInt64(); }
250  bool _Is(double*) const { return IsReal(); }
251 
252  struct _Holder;
253  std::shared_ptr<_Holder> _holder;
254 };
255 
256 template <typename T>
257 inline std::vector<T> JsValue::GetArrayOf() const
258 {
259  const JsArray& array = GetJsArray();
260  std::vector<T> result(array.size());
261  std::transform(array.begin(), array.end(), result.begin(),
262  [](const JsValue& v) { return v.Get<T>(); });
263  return result;
264 }
265 
266 template <typename T>
267 inline bool JsValue::IsArrayOf() const
268 {
269  if (!IsArray()) {
270  return false;
271  }
272  const JsArray& array = GetJsArray();
273  return std::all_of(array.begin(), array.end(),
274  [](const JsValue& v) { return v.Is<T>(); });
275 }
276 
278 
279 #endif // PXR_BASE_JS_VALUE_H
JS_API int GetInt() const
JS_API int64_t GetInt64() const
JS_API bool IsString() const
Returns true if this value is holding a string type.
ReturnType Get() const
Definition: value.h:150
JS_API bool IsObject() const
Returns true if this value is holding an object type.
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
JS_API bool operator!=(const JsValue &other) const
JS_API bool IsArray() const
Returns true if this value is holding an array type.
JS_API double GetReal() const
**But if you need a result
Definition: thread.h:622
std::vector< JsValue > JsArray
Definition: types.h:22
OutGridT const XformOp bool bool
JS_API const std::string & GetString() const
JS_API bool IsInt() const
Returns true if this value is holding an integer type.
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
Definition: value.h:44
JS_API bool IsBool() const
Returns true if this value is holding a boolean type.
#define JS_API
Definition: api.h:23
JS_API bool operator==(const JsValue &other) const
JS_API JsValue()
Constructs a null value.
std::map< std::string, JsValue > JsObject
Definition: types.h:20
GA_API const UT_StringHolder transform
JS_API bool IsNull() const
Returns true if this value is null, false otherwise.
JS_API Type GetType() const
Returns the type of this value.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
JS_API bool IsReal() const
Returns true if this value is holding a real type.
JS_API uint64_t GetUInt64() const
JS_API bool GetBool() const
JS_API const JsArray & GetJsArray() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Type
Type held by this JSON value.
Definition: value.h:48
bool IsArrayOf() const
Definition: value.h:267
JS_API std::string GetTypeName() const
Returns a display name for the type of this value.
JS_API const JsObject & GetJsObject() const
std::vector< T > GetArrayOf() const
Definition: value.h:257
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.
bool Is() const
Definition: value.h:193