24 #ifndef PXR_BASE_JS_VALUE_H
25 #define PXR_BASE_JS_VALUE_H
30 #include "pxr/base/js/api.h"
37 #include <type_traits>
40 PXR_NAMESPACE_OPEN_SCOPE
45 #define JS_VALUE_API_VERSION 2
79 JS_API
JsValue(
const JsObject& value);
82 JS_API
JsValue(JsObject&& value);
85 JS_API
JsValue(
const JsArray& value);
88 JS_API
JsValue(JsArray&& value);
91 JS_API
explicit JsValue(
const char* value);
94 JS_API
explicit JsValue(
const std::string& value);
97 JS_API
explicit JsValue(std::string&& value);
100 JS_API
explicit JsValue(
bool value);
103 JS_API
explicit JsValue(
int value);
106 JS_API
explicit JsValue(int64_t value);
109 JS_API
explicit JsValue(uint64_t value);
112 JS_API
explicit JsValue(
double value);
127 JS_API
const std::string&
GetString()
const;
137 JS_API
int GetInt()
const;
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,
168 return _Get(static_cast<T*>(
nullptr));
177 template <
typename T>
196 JS_API
bool IsBool()
const;
199 JS_API
bool IsInt()
const;
202 JS_API
bool IsReal()
const;
209 template <
typename T>
211 return _Is(static_cast<T*>(
nullptr));
216 template <
typename T>
220 JS_API
bool IsNull()
const;
223 JS_API
explicit operator bool()
const;
234 template <
typename T>
235 struct _InvalidTypeHelper :
public std::false_type { };
239 static_assert(_InvalidTypeHelper<T>::value,
240 "Invalid type for JsValue");
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(); }
255 static_assert(_InvalidTypeHelper<T>::value,
256 "Invalid type for JsValue");
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(); }
270 std::shared_ptr<_Holder> _holder;
273 template <
typename T>
277 std::vector<T> result(array.size());
278 std::transform(array.begin(), array.end(), result.begin(),
279 [](
const JsValue& v) {
return v.Get<T>(); });
283 template <
typename T>
290 return std::all_of(array.begin(), array.end(),
291 [](
const JsValue& v) {
return v.Is<T>(); });
294 PXR_NAMESPACE_CLOSE_SCOPE
296 #endif // PXR_BASE_JS_VALUE_H
JS_API bool operator!=(const JsValue &other) const
Returns true if values are of different type, or the underlying held values are not equal...
JS_API bool IsObject() const
Returns true if this value is holding an object type.
JS_API bool IsNull() const
Returns true if this value is null, false otherwise.
JS_API int GetInt() const
Returns the integer held by this value.
Type
Type held by this JSON value.
JS_API bool IsArray() const
Returns true if this value is holding an array type.
std::vector< T > GetArrayOf() const
Returns a vector holding the elements of this value's array that correspond to the C++ type specified...
JS_API const std::string & GetString() const
Returns the string held by this value.
JS_API JsValue()
Constructs a null value.
bool IsArrayOf() const
Returns true if this value is holding an array whose elements all correspond to the C++ type specifie...
A discriminated union type for JSON values.
JS_API const JsObject & GetJsObject() const
Returns the object held by this value.
JS_API std::string GetTypeName() const
Returns a display name for the type of 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 bool IsInt() const
Returns true if this value is holding an integer type.
bool Is() const
Returns true if this value is holding a type that corresponds to the C++ type specified as the templa...
JS_API uint64_t GetUInt64() const
Returns the 64-bit unsigned integer held by this value.
JS_API bool IsString() const
Returns true if this value is holding a string type.
JS_API int64_t GetInt64() const
Returns the 64-bit integer held by this value.
JS_API const JsArray & GetJsArray() const
Returns the array held by this value.
ReturnType Get() const
Returns the value corresponding to the C++ type specified in the template parameter if it is holding ...
JS_API bool GetBool() const
Returns the bool held by this value.
JS_API bool IsReal() const
Returns true if this value is holding a real type.
JS_API Type GetType() const
Returns the type of this value.
JS_API bool IsBool() const
Returns true if this value is holding a boolean type.
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.
JS_API double GetReal() const
Returns the double held by this value.