All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
converter.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_CONVERTER_H
8#define PXR_BASE_JS_CONVERTER_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/base/js/value.h"
15
16PXR_NAMESPACE_OPEN_SCOPE
17
18// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
19// holding an \c int64_t.
20template <class ValueType, class MapType, bool UseInt64 = true>
21struct Js_ValueToInt {
22 static ValueType Apply(const JsValue& value) {
23 return value.IsUInt64() ?
24 ValueType(value.GetUInt64()) : ValueType(value.GetInt64());
25 }
26};
27
28// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
29// holding an \c int.
30template <class ValueType, class MapType>
31struct Js_ValueToInt<ValueType, MapType, false>
32{
33 static ValueType Apply(const JsValue& value) {
34 return ValueType(value.GetInt());
35 }
36};
37
61template <class ValueType, class MapType, bool UseInt64 = true>
63{
64 typedef std::vector<ValueType> VectorType;
65public:
69 static ValueType Convert(const JsValue& value) {
70 return _ToValueType(value);
71 }
72
73private:
75 static ValueType _ToValueType(const JsValue& value) {
76 switch (value.GetType()) {
77 case JsValue::ObjectType:
78 return ValueType(_ObjectToMap(value.GetJsObject()));
79 case JsValue::ArrayType:
80 return ValueType(_ArrayToVector(value.GetJsArray()));
81 case JsValue::BoolType:
82 return ValueType(value.GetBool());
83 case JsValue::StringType:
84 return ValueType(value.GetString());
85 case JsValue::RealType:
86 return ValueType(value.GetReal());
87 case JsValue::IntType:
88 return Js_ValueToInt<ValueType, MapType, UseInt64>::Apply(value);
89 case JsValue::NullType:
90 return ValueType();
91 default: {
92 TF_CODING_ERROR("unknown value type");
93 return ValueType();
94 }
95 }
96 }
97
99 static MapType _ObjectToMap(const JsObject& object) {
100 MapType result;
101 for (const auto& p : object) {
102 result[p.first] = _ToValueType(p.second);
103 }
104 return result;
105 }
106
108 static VectorType _ArrayToVector(const JsArray& array) {
109 VectorType result;
110 result.reserve(array.size());
111 for (const auto& value : array) {
112 result.push_back(_ToValueType(value));
113 }
114 return result;
115 }
116};
117
121template <class ValueType, class MapType>
122ValueType JsConvertToContainerType(const JsValue& value) {
124}
125
126PXR_NAMESPACE_CLOSE_SCOPE
127
128#endif // PXR_BASE_JS_CONVERTER_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
A discriminated union type for JSON values.
Definition: value.h:45
JS_API const JsArray & GetJsArray() const
Returns the array held by this value.
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.
JS_API Type GetType() const
Returns 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 const JsObject & GetJsObject() const
Returns the object held by this value.
JS_API bool GetBool() const
Returns the bool held by this value.
JS_API const std::string & GetString() const
Returns the string held by this value.
JS_API int GetInt() const
Returns the integer held by this value.
A helper class that can convert recursive JsValue structures to identical structures using a differen...
Definition: converter.h:63
static ValueType Convert(const JsValue &value)
Converts the given value recursively to a structure using the value and map types specified by the Va...
Definition: converter.h:69
ValueType JsConvertToContainerType(const JsValue &value)
Returns value converted recursively to the template and map types given by the ValueType and MapType ...
Definition: converter.h:122
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:68