HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
27 /// \file js/converter.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/base/js/value.h"
31 #include "pxr/base/tf/diagnostic.h"
32 
34 
35 // Converts a \c JsValue \p value holding an \c int value to a \c ValueType
36 // holding an \c int64_t.
37 template <class ValueType, class MapType, bool UseInt64 = true>
38 struct 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.
47 template <class ValueType, class MapType>
48 struct Js_ValueToInt<ValueType, MapType, false>
49 {
50  static ValueType Apply(const JsValue& value) {
51  return ValueType(value.GetInt());
52  }
53 };
54 
55 /// \class JsValueTypeConverter
56 ///
57 /// A helper class that can convert recursive JsValue structures to
58 /// identical structures using a different container type. The destination
59 /// container type is determined by the \c ValueType template parameter, while
60 /// the type to map objects to is determined by the \c MapType template
61 /// parameter.
62 ///
63 /// It is expected that the class \c ValueType is default constructable. A
64 /// default constructed \c ValueType is used to represent JSON null. The value
65 /// type must also support construction from the fundamental bool, string,
66 /// real and integer types supported by JsValue.
67 ///
68 /// JsArray values are converted to std::vector<ValueType>, and JsObject
69 /// values are converted to the MapType. MapType must have a value type of \c
70 /// ValueType, and support operator[] assignment.
71 ///
72 /// If the \c UseInt64 template parameter is \c true (default), value types
73 /// converted from JsValue::IntType hold uint64_t or int64_t. If the parameter
74 /// is \c false, all IntType values are converted to int. Note that this may
75 /// cause truncation if the JsValue holds values too large to be stored in an
76 /// int on this platform.
77 ///
78 template <class ValueType, class MapType, bool UseInt64 = true>
80 {
81  typedef std::vector<ValueType> VectorType;
82 public:
83  /// Converts the given \p value recursively to a structure using the value
84  /// and map types specified by the \c ValueType and \c MapType class
85  /// template parameters.
86  static ValueType Convert(const JsValue& value) {
87  return _ToValueType(value);
88  }
89 
90 private:
91  /// Converts \p value to \c ValueType.
92  static ValueType _ToValueType(const JsValue& value) {
93  switch (value.GetType()) {
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:
106  case JsValue::NullType:
107  return ValueType();
108  default: {
109  TF_CODING_ERROR("unknown value type");
110  return ValueType();
111  }
112  }
113  }
114 
115  /// Converts \p object to \c MapType.
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 
124  /// Converts \p array to \c VectorType.
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 
135 /// Returns \p value converted recursively to the template and map types given
136 /// by the \c ValueType and \p MapType parameters.
137 /// \see JsValueTypeConverter
138 template <class ValueType, class MapType>
139 ValueType JsConvertToContainerType(const JsValue& value) {
141 }
142 
144 
145 #endif // PXR_BASE_JS_CONVERTER_H
JS_API int GetInt() const
JS_API int64_t GetInt64() const
ValueType JsConvertToContainerType(const JsValue &value)
Definition: converter.h:139
#define TF_CODING_ERROR
JS_API double GetReal() const
**But if you need a result
Definition: thread.h:613
static ValueType Apply(const JsValue &value)
Definition: converter.h:50
std::vector< JsValue > JsArray
Definition: types.h:39
JS_API const std::string & GetString() const
static ValueType Convert(const JsValue &value)
Definition: converter.h:86
Definition: value.h:61
std::map< std::string, JsValue > JsObject
Definition: types.h:37
static ValueType Apply(const JsValue &value)
Definition: converter.h:39
JS_API Type GetType() const
Returns the type of this value.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
JS_API uint64_t GetUInt64() const
JS_API bool GetBool() const
JS_API const JsArray & GetJsArray() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:1131
JS_API const JsObject & GetJsObject() const
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.