Loading...
Searching...
No Matches
value.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_JS_VALUE_H
25#define PXR_BASE_JS_VALUE_H
26
28
29#include "pxr/pxr.h"
30#include "pxr/base/js/api.h"
31#include "pxr/base/js/types.h"
32
33#include <algorithm>
34#include <cstdint>
35#include <memory>
36#include <string>
37#include <type_traits>
38#include <vector>
39
40PXR_NAMESPACE_OPEN_SCOPE
41
42// Value API Version
43// 1 (or undefined) - Initial version.
44// 2 - Changed Get{Array,Object} to GetJs{Array,Object}.
45#define JS_VALUE_API_VERSION 2
46
62{
63public:
65 enum Type {
66 ObjectType,
67 ArrayType,
68 StringType,
69 BoolType,
70 IntType,
71 RealType,
72 NullType
73 };
74
76 JS_API JsValue();
77
79 JS_API JsValue(const JsObject& value);
80
82 JS_API JsValue(JsObject&& value);
83
85 JS_API JsValue(const JsArray& value);
86
88 JS_API JsValue(JsArray&& value);
89
91 JS_API explicit JsValue(const char* value);
92
94 JS_API explicit JsValue(const std::string& value);
95
97 JS_API explicit JsValue(std::string&& value);
98
100 JS_API explicit JsValue(bool value);
101
103 JS_API explicit JsValue(int value);
104
106 JS_API explicit JsValue(int64_t value);
107
109 JS_API explicit JsValue(uint64_t value);
110
112 JS_API explicit JsValue(double value);
113
117 JS_API const JsObject& GetJsObject() const;
118
122 JS_API const JsArray& GetJsArray() const;
123
127 JS_API const std::string& GetString() const;
128
131 JS_API bool GetBool() const;
132
137 JS_API int GetInt() const;
138
142 JS_API int64_t GetInt64() const;
143
147 JS_API uint64_t GetUInt64() const;
148
151 JS_API double GetReal() const;
152
161 template <typename T,
162 typename ReturnType = typename std::conditional<
163 std::is_same<T, JsObject>::value ||
164 std::is_same<T, JsArray>::value ||
165 std::is_same<T, std::string>::value,
166 const T&, T>::type>
167 ReturnType Get() const {
168 return _Get(static_cast<T*>(nullptr));
169 }
170
177 template <typename T>
178 std::vector<T> GetArrayOf() const;
179
181 JS_API Type GetType() const;
182
184 JS_API std::string GetTypeName() const;
185
187 JS_API bool IsObject() const;
188
190 JS_API bool IsArray() const;
191
193 JS_API bool IsString() const;
194
196 JS_API bool IsBool() const;
197
199 JS_API bool IsInt() const;
200
202 JS_API bool IsReal() const;
203
205 JS_API bool IsUInt64() const;
206
209 template <typename T>
210 bool Is() const {
211 return _Is(static_cast<T*>(nullptr));
212 }
213
216 template <typename T>
217 bool IsArrayOf() const;
218
220 JS_API bool IsNull() const;
221
223 JS_API explicit operator bool() const;
224
227 JS_API bool operator==(const JsValue& other) const;
228
231 JS_API bool operator!=(const JsValue& other) const;
232
233private:
234 template <typename T>
235 struct _InvalidTypeHelper : public std::false_type { };
236
237 template <class T>
238 T _Get(T*) const {
239 static_assert(_InvalidTypeHelper<T>::value,
240 "Invalid type for JsValue");
241 return T();
242 }
243
244 const JsObject& _Get(JsObject*) const { return GetJsObject(); }
245 const JsArray& _Get(JsArray*) const { return GetJsArray(); }
246 const std::string& _Get(std::string*) const { return GetString(); }
247 bool _Get(bool*) const { return GetBool(); }
248 int _Get(int*) const { return GetInt(); }
249 int64_t _Get(int64_t*) const { return GetInt64(); }
250 uint64_t _Get(uint64_t*) const { return GetUInt64(); }
251 double _Get(double*) const { return GetReal(); }
252
253 template <class T>
254 bool _Is(T*) const {
255 static_assert(_InvalidTypeHelper<T>::value,
256 "Invalid type for JsValue");
257 return false;
258 }
259
260 bool _Is(JsObject*) const { return IsObject(); }
261 bool _Is(JsArray*) const { return IsArray(); }
262 bool _Is(std::string*) const { return IsString(); }
263 bool _Is(bool*) const { return IsBool(); }
264 bool _Is(int*) const { return IsInt(); }
265 bool _Is(int64_t*) const { return IsInt(); }
266 bool _Is(uint64_t*) const { return IsUInt64(); }
267 bool _Is(double*) const { return IsReal(); }
268
269 struct _Holder;
270 std::shared_ptr<_Holder> _holder;
271};
272
273template <typename T>
274inline std::vector<T> JsValue::GetArrayOf() const
275{
276 const JsArray& array = GetJsArray();
277 std::vector<T> result(array.size());
278 std::transform(array.begin(), array.end(), result.begin(),
279 [](const JsValue& v) { return v.Get<T>(); });
280 return result;
281}
282
283template <typename T>
284inline bool JsValue::IsArrayOf() const
285{
286 if (!IsArray()) {
287 return false;
288 }
289 const JsArray& array = GetJsArray();
290 return std::all_of(array.begin(), array.end(),
291 [](const JsValue& v) { return v.Is<T>(); });
292}
293
294PXR_NAMESPACE_CLOSE_SCOPE
295
296#endif // PXR_BASE_JS_VALUE_H
A discriminated union type for JSON values.
Definition: value.h:62
JS_API int64_t GetInt64() const
Returns the 64-bit integer held by this value.
JS_API JsValue(int64_t value)
Constructs a value holding a 64-bit signed integer.
Type
Type held by this JSON value.
Definition: value.h:65
JS_API JsValue(int value)
Constructs a value holding a signed integer.
JS_API JsValue(const JsObject &value)
Constructs a value holding the given object.
JS_API bool IsInt() const
Returns true if this value is holding an integer type.
JS_API JsValue(const std::string &value)
Constructs a value holding the given std::string.
ReturnType Get() const
Returns the value corresponding to the C++ type specified in the template parameter if it is holding ...
Definition: value.h:167
JS_API bool IsReal() const
Returns true if this value is holding a real type.
JS_API JsValue(const JsArray &value)
Constructs a value holding the given array.
JS_API const JsArray & GetJsArray() const
Returns the array held by this value.
JS_API JsValue(std::string &&value)
Constructs a value holding the given std::string rvalue reference.
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.
JS_API bool IsObject() const
Returns true if this value is holding an object type.
JS_API Type GetType() const
Returns the type of this value.
JS_API std::string GetTypeName() const
Returns a display name for the type of this value.
JS_API double GetReal() const
Returns the double held by this value.
JS_API uint64_t GetUInt64() const
Returns the 64-bit unsigned integer held by this value.
JS_API bool operator!=(const JsValue &other) const
Returns true if values are of different type, or the underlying held values are not equal.
bool Is() const
Returns true if this value is holding a type that corresponds to the C++ type specified as the templa...
Definition: value.h:210
JS_API JsValue(uint64_t value)
Constructs a value holding a 64-bit unsigned integer.
std::vector< T > GetArrayOf() const
Returns a vector holding the elements of this value's array that correspond to the C++ type specified...
Definition: value.h:274
JS_API JsValue(double value)
Constructs a value holding a double.
JS_API JsValue(bool value)
Constructs a value holding a bool.
JS_API const JsObject & GetJsObject() const
Returns the object held by this value.
JS_API JsValue(JsArray &&value)
Constructs a value holding the given array rvalue reference.
JS_API JsValue(JsObject &&value)
Constructs a value holding the given object rvalue reference.
JS_API bool IsNull() const
Returns true if this value is null, false otherwise.
JS_API bool GetBool() const
Returns the bool held by this value.
JS_API bool operator==(const JsValue &other) const
Returns true of both values hold the same type and the underlying held values are equal.
JS_API JsValue(const char *value)
Constructs a value holding the given char array as a std::string.
JS_API bool IsBool() const
Returns true if this value is holding a boolean type.
JS_API const std::string & GetString() const
Returns the string held by this value.
bool IsArrayOf() const
Returns true if this value is holding an array whose elements all correspond to the C++ type specifie...
Definition: value.h:284
JS_API int GetInt() const
Returns the integer held by this value.
JS_API bool IsString() const
Returns true if this value is holding a string type.
JS_API JsValue()
Constructs a null value.
JS_API bool IsArray() const
Returns true if this value is holding an array type.