7#ifndef PXR_BASE_JS_VALUE_H
8#define PXR_BASE_JS_VALUE_H
13#include "pxr/base/js/api.h"
15#include "pxr/base/tf/delegatedCountPtr.h"
25PXR_NAMESPACE_OPEN_SCOPE
30#define JS_VALUE_API_VERSION 2
76 JS_API
explicit JsValue(
const char* value);
79 JS_API
explicit JsValue(
const std::string& value);
82 JS_API
explicit JsValue(std::string&& value);
146 template <
typename T,
147 typename ReturnType =
typename std::conditional<
148 std::is_same<T, JsObject>::value ||
149 std::is_same<T, JsArray>::value ||
150 std::is_same<T, std::string>::value,
153 return _Get(
static_cast<T*
>(
nullptr));
162 template <
typename T>
194 template <
typename T>
196 return _Is(
static_cast<T*
>(
nullptr));
201 template <
typename T>
208 JS_API
explicit operator bool()
const;
220 lhs._value.swap(rhs._value);
224 template <
typename T>
225 struct _InvalidTypeHelper :
public std::false_type { };
229 static_assert(_InvalidTypeHelper<T>::value,
230 "Invalid type for JsValue");
234 const JsObject& _Get(JsObject*)
const {
return GetJsObject(); }
235 const JsArray& _Get(JsArray*)
const {
return GetJsArray(); }
236 const std::string& _Get(std::string*)
const {
return GetString(); }
237 bool _Get(
bool*)
const {
return GetBool(); }
238 int _Get(
int*)
const {
return GetInt(); }
239 int64_t _Get(int64_t*)
const {
return GetInt64(); }
240 uint64_t _Get(uint64_t*)
const {
return GetUInt64(); }
241 double _Get(
double*)
const {
return GetReal(); }
245 static_assert(_InvalidTypeHelper<T>::value,
246 "Invalid type for JsValue");
250 bool _Is(JsObject*)
const {
return IsObject(); }
251 bool _Is(JsArray*)
const {
return IsArray(); }
252 bool _Is(std::string*)
const {
return IsString(); }
253 bool _Is(
bool*)
const {
return IsBool(); }
254 bool _Is(
int*)
const {
return IsInt(); }
255 bool _Is(int64_t*)
const {
return IsInt(); }
256 bool _Is(uint64_t*)
const {
return IsUInt64(); }
257 bool _Is(
double*)
const {
return IsReal(); }
269 template <enum Type EnumValue>
273 _HolderBase() =
default;
274 ~_HolderBase() =
default;
277 friend void TfDelegatedCountIncrement(
const _HolderBase *h)
noexcept {
278 h->_refCount.fetch_add(1, std::memory_order_relaxed);
281 friend void TfDelegatedCountDecrement(
const _HolderBase *h)
noexcept {
282 const int rc = h->_refCount.fetch_sub(1, std::memory_order_release);
284 std::atomic_thread_fence(std::memory_order_acquire);
289 static JS_API
void _Delete(
const _HolderBase *)
noexcept;
292 mutable std::atomic<int> _refCount = 1;
295 template <
typename>
struct _Holder;
296 struct _IsValueEqualVisitor;
302 bool operator==(
const _JsNull& v)
const {
305 bool operator!=(
const _JsNull& v)
const {
313 using _Variant = std::variant<
317 bool, int64_t, double, _JsNull, uint64_t>;
326 std::vector<T> result(array.size());
327 std::transform(array.begin(), array.end(), result.begin(),
328 [](
const JsValue& v) { return v.Get<T>(); });
339 return std::all_of(array.begin(), array.end(),
340 [](
const JsValue& v) { return v.Is<T>(); });
343PXR_NAMESPACE_CLOSE_SCOPE
A discriminated union type for JSON values.
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.
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 ...
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.
friend void swap(JsValue &lhs, JsValue &rhs)
Swap the value held by lhs with the value held by rhs.
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...
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...
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...
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.
Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to...