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 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 
10 /// \file js/converter.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/js/value.h"
14 #include "pxr/base/tf/diagnostic.h"
15 
17 
18 // Converts a \c JsValue \p value holding an \c int value to a \c ValueType
19 // holding an \c int64_t.
20 template <class ValueType, class MapType, bool UseInt64 = true>
21 struct 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.
30 template <class ValueType, class MapType>
31 struct Js_ValueToInt<ValueType, MapType, false>
32 {
33  static ValueType Apply(const JsValue& value) {
34  return ValueType(value.GetInt());
35  }
36 };
37 
38 /// \class JsValueTypeConverter
39 ///
40 /// A helper class that can convert recursive JsValue structures to
41 /// identical structures using a different container type. The destination
42 /// container type is determined by the \c ValueType template parameter, while
43 /// the type to map objects to is determined by the \c MapType template
44 /// parameter.
45 ///
46 /// It is expected that the class \c ValueType is default constructable. A
47 /// default constructed \c ValueType is used to represent JSON null. The value
48 /// type must also support construction from the fundamental bool, string,
49 /// real and integer types supported by JsValue.
50 ///
51 /// JsArray values are converted to std::vector<ValueType>, and JsObject
52 /// values are converted to the MapType. MapType must have a value type of \c
53 /// ValueType, and support operator[] assignment.
54 ///
55 /// If the \c UseInt64 template parameter is \c true (default), value types
56 /// converted from JsValue::IntType hold uint64_t or int64_t. If the parameter
57 /// is \c false, all IntType values are converted to int. Note that this may
58 /// cause truncation if the JsValue holds values too large to be stored in an
59 /// int on this platform.
60 ///
61 template <class ValueType, class MapType, bool UseInt64 = true>
63 {
64  typedef std::vector<ValueType> VectorType;
65 public:
66  /// Converts the given \p value recursively to a structure using the value
67  /// and map types specified by the \c ValueType and \c MapType class
68  /// template parameters.
69  static ValueType Convert(const JsValue& value) {
70  return _ToValueType(value);
71  }
72 
73 private:
74  /// Converts \p value to \c ValueType.
75  static ValueType _ToValueType(const JsValue& value) {
76  switch (value.GetType()) {
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());
84  return ValueType(value.GetString());
85  case JsValue::RealType:
86  return ValueType(value.GetReal());
87  case JsValue::IntType:
89  case JsValue::NullType:
90  return ValueType();
91  default: {
92  TF_CODING_ERROR("unknown value type");
93  return ValueType();
94  }
95  }
96  }
97 
98  /// Converts \p object to \c MapType.
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 
107  /// Converts \p array to \c VectorType.
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 
118 /// Returns \p value converted recursively to the template and map types given
119 /// by the \c ValueType and \p MapType parameters.
120 /// \see JsValueTypeConverter
121 template <class ValueType, class MapType>
124 }
125 
127 
128 #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:122
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define TF_CODING_ERROR
JS_API double GetReal() const
**But if you need a result
Definition: thread.h:622
static ValueType Apply(const JsValue &value)
Definition: converter.h:33
std::vector< JsValue > JsArray
Definition: types.h:22
JS_API const std::string & GetString() const
static ValueType Convert(const JsValue &value)
Definition: converter.h:69
Definition: value.h:44
std::map< std::string, JsValue > JsObject
Definition: types.h:20
static ValueType Apply(const JsValue &value)
Definition: converter.h:22
JS_API Type GetType() const
Returns the type of this value.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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:74
bool ValueType
Definition: NanoVDB.h:5729
JS_API const JsObject & GetJsObject() const
JS_API bool IsUInt64() const
Returns true if this value is holding a 64-bit unsigned integer.