Loading...
Searching...
No Matches
converter.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_CONVERTER_H
25#define PXR_BASE_JS_CONVERTER_H
26
28
29#include "pxr/pxr.h"
30#include "pxr/base/js/value.h"
32
33PXR_NAMESPACE_OPEN_SCOPE
34
35// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
36// holding an \c int64_t.
37template <class ValueType, class MapType, bool UseInt64 = true>
38struct Js_ValueToInt {
39 static ValueType Apply(const JsValue& value) {
40 return value.IsUInt64() ?
41 ValueType(value.GetUInt64()) : ValueType(value.GetInt64());
42 }
43};
44
45// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
46// holding an \c int.
47template <class ValueType, class MapType>
48struct Js_ValueToInt<ValueType, MapType, false>
49{
50 static ValueType Apply(const JsValue& value) {
51 return ValueType(value.GetInt());
52 }
53};
54
78template <class ValueType, class MapType, bool UseInt64 = true>
80{
81 typedef std::vector<ValueType> VectorType;
82public:
86 static ValueType Convert(const JsValue& value) {
87 return _ToValueType(value);
88 }
89
90private:
92 static ValueType _ToValueType(const JsValue& value) {
93 switch (value.GetType()) {
94 case JsValue::ObjectType:
95 return ValueType(_ObjectToMap(value.GetJsObject()));
96 case JsValue::ArrayType:
97 return ValueType(_ArrayToVector(value.GetJsArray()));
98 case JsValue::BoolType:
99 return ValueType(value.GetBool());
100 case JsValue::StringType:
101 return ValueType(value.GetString());
102 case JsValue::RealType:
103 return ValueType(value.GetReal());
104 case JsValue::IntType:
105 return Js_ValueToInt<ValueType, MapType, UseInt64>::Apply(value);
106 case JsValue::NullType:
107 return ValueType();
108 default: {
109 TF_CODING_ERROR("unknown value type");
110 return ValueType();
111 }
112 }
113 }
114
116 static MapType _ObjectToMap(const JsObject& object) {
117 MapType result;
118 for (const auto& p : object) {
119 result[p.first] = _ToValueType(p.second);
120 }
121 return result;
122 }
123
125 static VectorType _ArrayToVector(const JsArray& array) {
126 VectorType result;
127 result.reserve(array.size());
128 for (const auto& value : array) {
129 result.push_back(_ToValueType(value));
130 }
131 return result;
132 }
133};
134
138template <class ValueType, class MapType>
139ValueType JsConvertToContainerType(const JsValue& value) {
141}
142
143PXR_NAMESPACE_CLOSE_SCOPE
144
145#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:62
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:80
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:86
ValueType JsConvertToContainerType(const JsValue &value)
Returns value converted recursively to the template and map types given by the ValueType and MapType ...
Definition: converter.h:139
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:85