add usd-21-11-win64_py39
This commit is contained in:
1 parent
41d23e71ba
commit
ba5e1f7e67
15684 files changed
+3069957
No files matched your search
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_API_H
|
||||
#define PXR_BASE_JS_API_H
|
||||
|
||||
#include "pxr/base/arch/export.h"
|
||||
|
||||
#if defined(PXR_STATIC)
|
||||
# define JS_API
|
||||
# define JS_API_TEMPLATE_CLASS(...)
|
||||
# define JS_API_TEMPLATE_STRUCT(...)
|
||||
# define JS_LOCAL
|
||||
#else
|
||||
# if defined(JS_EXPORTS)
|
||||
# define JS_API ARCH_EXPORT
|
||||
# define JS_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define JS_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# else
|
||||
# define JS_API ARCH_IMPORT
|
||||
# define JS_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define JS_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# endif
|
||||
# define JS_LOCAL ARCH_HIDDEN
|
||||
#endif
|
||||
|
||||
#endif // PXR_BASE_JS_API_H
|
||||
@@ -0,0 +1,145 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_CONVERTER_H
|
||||
#define PXR_BASE_JS_CONVERTER_H
|
||||
|
||||
/// \file js/converter.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/value.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
|
||||
// holding an \c int64_t.
|
||||
template <class ValueType, class MapType, bool UseInt64 = true>
|
||||
struct Js_ValueToInt {
|
||||
static ValueType Apply(const JsValue& value) {
|
||||
return value.IsUInt64() ?
|
||||
ValueType(value.GetUInt64()) : ValueType(value.GetInt64());
|
||||
}
|
||||
};
|
||||
|
||||
// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
|
||||
// holding an \c int.
|
||||
template <class ValueType, class MapType>
|
||||
struct Js_ValueToInt<ValueType, MapType, false>
|
||||
{
|
||||
static ValueType Apply(const JsValue& value) {
|
||||
return ValueType(value.GetInt());
|
||||
}
|
||||
};
|
||||
|
||||
/// \class JsValueTypeConverter
|
||||
///
|
||||
/// A helper class that can convert recursive JsValue structures to
|
||||
/// identical structures using a different container type. The destination
|
||||
/// container type is determined by the \c ValueType template parameter, while
|
||||
/// the type to map objects to is determined by the \c MapType template
|
||||
/// parameter.
|
||||
///
|
||||
/// It is expected that the class \c ValueType is default constructable. A
|
||||
/// default constructed \c ValueType is used to represent JSON null. The value
|
||||
/// type must also support construction from the fundamental bool, string,
|
||||
/// real and integer types supported by JsValue.
|
||||
///
|
||||
/// JsArray values are converted to std::vector<ValueType>, and JsObject
|
||||
/// values are converted to the MapType. MapType must have a value type of \c
|
||||
/// ValueType, and support operator[] assignment.
|
||||
///
|
||||
/// If the \c UseInt64 template parameter is \c true (default), value types
|
||||
/// converted from JsValue::IntType hold uint64_t or int64_t. If the parameter
|
||||
/// is \c false, all IntType values are converted to int. Note that this may
|
||||
/// cause truncation if the JsValue holds values too large to be stored in an
|
||||
/// int on this platform.
|
||||
///
|
||||
template <class ValueType, class MapType, bool UseInt64 = true>
|
||||
class JsValueTypeConverter
|
||||
{
|
||||
typedef std::vector<ValueType> VectorType;
|
||||
public:
|
||||
/// Converts the given \p value recursively to a structure using the value
|
||||
/// and map types specified by the \c ValueType and \c MapType class
|
||||
/// template parameters.
|
||||
static ValueType Convert(const JsValue& value) {
|
||||
return _ToValueType(value);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Converts \p value to \c ValueType.
|
||||
static ValueType _ToValueType(const JsValue& value) {
|
||||
switch (value.GetType()) {
|
||||
case JsValue::ObjectType:
|
||||
return ValueType(_ObjectToMap(value.GetJsObject()));
|
||||
case JsValue::ArrayType:
|
||||
return ValueType(_ArrayToVector(value.GetJsArray()));
|
||||
case JsValue::BoolType:
|
||||
return ValueType(value.GetBool());
|
||||
case JsValue::StringType:
|
||||
return ValueType(value.GetString());
|
||||
case JsValue::RealType:
|
||||
return ValueType(value.GetReal());
|
||||
case JsValue::IntType:
|
||||
return Js_ValueToInt<ValueType, MapType, UseInt64>::Apply(value);
|
||||
case JsValue::NullType:
|
||||
return ValueType();
|
||||
default: {
|
||||
TF_CODING_ERROR("unknown value type");
|
||||
return ValueType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts \p object to \c MapType.
|
||||
static MapType _ObjectToMap(const JsObject& object) {
|
||||
MapType result;
|
||||
for (const auto& p : object) {
|
||||
result[p.first] = _ToValueType(p.second);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Converts \p array to \c VectorType.
|
||||
static VectorType _ArrayToVector(const JsArray& array) {
|
||||
VectorType result;
|
||||
result.reserve(array.size());
|
||||
for (const auto& value : array) {
|
||||
result.push_back(_ToValueType(value));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/// Returns \p value converted recursively to the template and map types given
|
||||
/// by the \c ValueType and \p MapType parameters.
|
||||
/// \see JsValueTypeConverter
|
||||
template <class ValueType, class MapType>
|
||||
ValueType JsConvertToContainerType(const JsValue& value) {
|
||||
return JsValueTypeConverter<ValueType, MapType>::Convert(value);
|
||||
}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_CONVERTER_H
|
||||
@@ -0,0 +1,235 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
|
||||
#ifndef PXR_BASE_JS_JSON_H
|
||||
#define PXR_BASE_JS_JSON_H
|
||||
|
||||
/// \file js/json.h
|
||||
/// Top-level entrypoints for reading and writing JSON.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/api.h"
|
||||
#include "pxr/base/js/value.h"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \struct JsParseError
|
||||
///
|
||||
/// A struct containing information about a JSON parsing error.
|
||||
///
|
||||
struct JsParseError {
|
||||
JsParseError() : line(0), column(0) { }
|
||||
unsigned int line;
|
||||
unsigned int column;
|
||||
std::string reason;
|
||||
};
|
||||
|
||||
/// Parse the contents of input stream \p istr and return a JsValue. On
|
||||
/// failure, this returns a null JsValue.
|
||||
JS_API
|
||||
JsValue JsParseStream(std::istream& istr, JsParseError* error = 0);
|
||||
|
||||
/// Parse the contents of the JSON string \p data and return it as a JsValue.
|
||||
/// On failure, this returns a null JsValue.
|
||||
JS_API
|
||||
JsValue JsParseString(const std::string& data, JsParseError* error = 0);
|
||||
|
||||
/// Convert the JsValue \p value to JSON and write the result to output stream
|
||||
/// \p ostr.
|
||||
JS_API
|
||||
void JsWriteToStream(const JsValue& value, std::ostream& ostr);
|
||||
|
||||
/// Convert the JsValue \p value to JSON and return it as a string.
|
||||
JS_API
|
||||
std::string JsWriteToString(const JsValue& value);
|
||||
|
||||
/// \class JsWriter
|
||||
///
|
||||
/// This class provides an interface to writing json values directly to a
|
||||
/// stream. This can be much more efficient than constructing a JsValue instance
|
||||
/// and using JsWriteToStream if the data size is significant.
|
||||
///
|
||||
class JsWriter {
|
||||
public:
|
||||
enum class Style {
|
||||
Compact,
|
||||
Pretty
|
||||
};
|
||||
|
||||
/// Constructor. The lifetime of the /p ostr parameter is assumed to be
|
||||
/// longer than the JsWriter instance.
|
||||
JS_API JsWriter(std::ostream& ostr, Style style = Style::Compact);
|
||||
|
||||
/// Destructor.
|
||||
JS_API ~JsWriter();
|
||||
|
||||
/// Disable copies.
|
||||
JsWriter(const JsWriter&) = delete;
|
||||
JsWriter& operator=(const JsWriter&) = delete;
|
||||
|
||||
/// Write a null value.
|
||||
JS_API bool WriteValue(std::nullptr_t);
|
||||
|
||||
/// Write a boolean value.
|
||||
JS_API bool WriteValue(bool b);
|
||||
|
||||
/// Write an integer value.
|
||||
JS_API bool WriteValue(int i);
|
||||
|
||||
/// Write an unsigned integer value.
|
||||
JS_API bool WriteValue(unsigned u);
|
||||
|
||||
/// Write a 64-bit integer value.
|
||||
JS_API bool WriteValue(int64_t i);
|
||||
|
||||
/// Write a 64-bit unsigned integer value.
|
||||
JS_API bool WriteValue(uint64_t u);
|
||||
|
||||
/// Write a double value.
|
||||
JS_API bool WriteValue(double d);
|
||||
|
||||
/// Write a string value.
|
||||
JS_API bool WriteValue(const std::string& s);
|
||||
|
||||
/// Write a string value.
|
||||
JS_API bool WriteValue(const char* s);
|
||||
|
||||
/// Write a string value.
|
||||
template< size_t N>
|
||||
bool WriteValue(const char(&s)[N]) { return _String(s, N-1); }
|
||||
|
||||
/// Write the start of an object.
|
||||
JS_API bool BeginObject();
|
||||
|
||||
/// Write an object key.
|
||||
JS_API bool WriteKey(const std::string&);
|
||||
|
||||
/// Write an object key.
|
||||
JS_API bool WriteKey(const char*);
|
||||
|
||||
/// Write a string literal object key.
|
||||
template< size_t N>
|
||||
bool WriteKey(const char(&s)[N]) { return _Key(s, N-1); }
|
||||
|
||||
/// Convenience function to write an object key and value.
|
||||
template<class K, class V>
|
||||
void WriteKeyValue(K&& k, V&& v) {
|
||||
_WriteObjectFields(std::forward<K>(k), std::forward<V>(v));
|
||||
}
|
||||
|
||||
/// Write the end of an object.
|
||||
JS_API bool EndObject();
|
||||
|
||||
/// Write the start of an array.
|
||||
JS_API bool BeginArray();
|
||||
|
||||
/// Write the end of an array.
|
||||
JS_API bool EndArray();
|
||||
|
||||
/// Convenience function to write an array of values.
|
||||
template <class Container>
|
||||
void WriteArray(const Container& c) {
|
||||
BeginArray();
|
||||
for (const auto& i : c) {
|
||||
WriteValue(i);
|
||||
}
|
||||
EndArray();
|
||||
}
|
||||
|
||||
/// Convenience function to write an array of values by calling the given
|
||||
/// functor for each item in the container.
|
||||
template <class Container, class ItemWriteFn>
|
||||
void WriteArray(const Container& c, const ItemWriteFn& f) {
|
||||
BeginArray();
|
||||
for (const auto& i : c) {
|
||||
f(*this, i);
|
||||
}
|
||||
EndArray();
|
||||
}
|
||||
|
||||
/// Convenience function to write an array of values given two iterators by
|
||||
/// calling the given functor for each item in the container.
|
||||
template <class Iterator, class ItemWriteFn>
|
||||
void WriteArray(
|
||||
const Iterator& begin, const Iterator& end, const ItemWriteFn& f) {
|
||||
BeginArray();
|
||||
for (Iterator i = begin; i != end; ++i) {
|
||||
f(*this, i);
|
||||
}
|
||||
EndArray();
|
||||
}
|
||||
|
||||
/// Convenience function to write an object given key value pair arguments.
|
||||
/// key arguments must be convertable to strings, value argruments must be
|
||||
/// either a writable type, or a callablable type taking a JsWriter&.
|
||||
template< class ...T>
|
||||
void WriteObject(T&&... f) {
|
||||
static_assert(sizeof...(T) %2 == 0,
|
||||
"Arguments must come in key value pairs");
|
||||
BeginObject();
|
||||
_WriteObjectFields(std::forward<T>(f)...);
|
||||
EndObject();
|
||||
}
|
||||
|
||||
private:
|
||||
// Don't want implicit casts to write functions, its better to get an error.
|
||||
template <class T>
|
||||
bool WriteValue(T) = delete;
|
||||
|
||||
JS_API bool _String(const char* s, size_t len);
|
||||
JS_API bool _Key(const char* s, size_t len);
|
||||
|
||||
template <class KeyType, class T>
|
||||
auto _WriteObjectFields(KeyType&& key, T&& v)
|
||||
-> decltype(WriteValue(std::forward<T>(v)), void()) {
|
||||
WriteKey(std::forward<KeyType>(key));
|
||||
WriteValue(std::forward<T>(v));
|
||||
}
|
||||
|
||||
template <class KeyType, class T>
|
||||
auto _WriteObjectFields(KeyType&& key, T&& v)
|
||||
-> decltype(v(std::declval<JsWriter&>()), void()) {
|
||||
WriteKey(std::forward<KeyType>(key));
|
||||
v(*this);
|
||||
}
|
||||
|
||||
template< class Key0, class T0, class ...T>
|
||||
void _WriteObjectFields(Key0&& key0, T0&& f0, T&&...f){
|
||||
_WriteObjectFields(std::forward<Key0>(key0), std::forward<T0>(f0));
|
||||
_WriteObjectFields(std::forward<T>(f)...);
|
||||
}
|
||||
|
||||
class _Impl;
|
||||
std::unique_ptr<_Impl> _impl;
|
||||
};
|
||||
|
||||
/// Write a json value.
|
||||
JS_API void JsWriteValue(JsWriter* writer, const JsValue& value);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_JSON_H
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_TYPES_H
|
||||
#define PXR_BASE_JS_TYPES_H
|
||||
|
||||
/// \file js/types.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class JsValue;
|
||||
typedef std::map<std::string, JsValue> JsObject;
|
||||
typedef std::vector<JsValue> JsArray;
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_TYPES_H
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_UTILS_H
|
||||
#define PXR_BASE_JS_UTILS_H
|
||||
|
||||
/// \file js/utils.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/api.h"
|
||||
#include "pxr/base/js/value.h"
|
||||
|
||||
#include <boost/none.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
typedef boost::optional<JsValue> JsOptionalValue;
|
||||
|
||||
/// Returns the value associated with \p key in the given \p object. If no
|
||||
/// such key exists, and the supplied default is not supplied, this method
|
||||
/// returns an uninitialized optional JsValue. Otherwise, the \p
|
||||
/// defaultValue is returned.
|
||||
JS_API
|
||||
JsOptionalValue JsFindValue(
|
||||
const JsObject& object,
|
||||
const std::string& key,
|
||||
const JsOptionalValue& defaultValue = boost::none);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_UTILS_H
|
||||
@@ -0,0 +1,296 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_VALUE_H
|
||||
#define PXR_BASE_JS_VALUE_H
|
||||
|
||||
/// \file js/value.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/api.h"
|
||||
#include "pxr/base/js/types.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
// Value API Version
|
||||
// 1 (or undefined) - Initial version.
|
||||
// 2 - Changed Get{Array,Object} to GetJs{Array,Object}.
|
||||
#define JS_VALUE_API_VERSION 2
|
||||
|
||||
/// \class JsValue
|
||||
///
|
||||
/// A discriminated union type for JSON values. A JsValue may contain one of
|
||||
/// the following types:
|
||||
///
|
||||
/// \li JsObject, a dictionary type
|
||||
/// \li JsArray, a vector type
|
||||
/// \li std::string
|
||||
/// \li bool
|
||||
/// \li int64_t
|
||||
/// \li uint64_t
|
||||
/// \li double
|
||||
/// \li null
|
||||
///
|
||||
class JsValue
|
||||
{
|
||||
public:
|
||||
/// Type held by this JSON value.
|
||||
enum Type {
|
||||
ObjectType,
|
||||
ArrayType,
|
||||
StringType,
|
||||
BoolType,
|
||||
IntType,
|
||||
RealType,
|
||||
NullType
|
||||
};
|
||||
|
||||
/// Constructs a null value.
|
||||
JS_API JsValue();
|
||||
|
||||
/// Constructs a value holding the given object.
|
||||
JS_API JsValue(const JsObject& value);
|
||||
|
||||
/// Constructs a value holding the given object rvalue reference.
|
||||
JS_API JsValue(JsObject&& value);
|
||||
|
||||
/// Constructs a value holding the given array.
|
||||
JS_API JsValue(const JsArray& value);
|
||||
|
||||
/// Constructs a value holding the given array rvalue reference.
|
||||
JS_API JsValue(JsArray&& value);
|
||||
|
||||
/// Constructs a value holding the given char array as a std::string.
|
||||
JS_API explicit JsValue(const char* value);
|
||||
|
||||
/// Constructs a value holding the given std::string.
|
||||
JS_API explicit JsValue(const std::string& value);
|
||||
|
||||
/// Constructs a value holding the given std::string rvalue reference.
|
||||
JS_API explicit JsValue(std::string&& value);
|
||||
|
||||
/// Constructs a value holding a bool.
|
||||
JS_API explicit JsValue(bool value);
|
||||
|
||||
/// Constructs a value holding a signed integer.
|
||||
JS_API explicit JsValue(int value);
|
||||
|
||||
/// Constructs a value holding a 64-bit signed integer.
|
||||
JS_API explicit JsValue(int64_t value);
|
||||
|
||||
/// Constructs a value holding a 64-bit unsigned integer.
|
||||
JS_API explicit JsValue(uint64_t value);
|
||||
|
||||
/// Constructs a value holding a double.
|
||||
JS_API explicit JsValue(double value);
|
||||
|
||||
/// Returns the object held by this value. If this value is not holding an
|
||||
/// object, this method raises a coding error and an empty object is
|
||||
/// returned.
|
||||
JS_API const JsObject& GetJsObject() const;
|
||||
|
||||
/// Returns the array held by this value. If this value is not holding an
|
||||
/// array, this method raises a coding error and an empty array is
|
||||
/// returned.
|
||||
JS_API const JsArray& GetJsArray() const;
|
||||
|
||||
/// Returns the string held by this value. If this value is not holding a
|
||||
/// string, this method raises a coding error and an empty string is
|
||||
/// returned.
|
||||
JS_API const std::string& GetString() const;
|
||||
|
||||
/// Returns the bool held by this value. If this value is not holding a
|
||||
/// bool, this method raises a coding error and false is returned.
|
||||
JS_API bool GetBool() const;
|
||||
|
||||
/// Returns the integer held by this value. If this value is not holding
|
||||
/// an int, this method raises a coding error and zero is returned. If the
|
||||
/// value is holding a 64-bit integer larger than the platform int may
|
||||
/// hold, the value is truncated.
|
||||
JS_API int GetInt() const;
|
||||
|
||||
/// Returns the 64-bit integer held by this value. If this value is not
|
||||
/// holding a 64-bit integer, this method raises a coding error and zero
|
||||
/// is returned.
|
||||
JS_API int64_t GetInt64() const;
|
||||
|
||||
/// Returns the 64-bit unsigned integer held by this value. If this value
|
||||
/// is not holding a 64-bit unsigned integer, this method raises a coding
|
||||
/// error and zero is returned.
|
||||
JS_API uint64_t GetUInt64() const;
|
||||
|
||||
/// Returns the double held by this value. If this value is not holding a
|
||||
/// double, this method raises a coding error and zero is returned.
|
||||
JS_API double GetReal() const;
|
||||
|
||||
/// Returns the value corresponding to the C++ type specified in the
|
||||
/// template parameter if it is holding such a value. Calling this
|
||||
/// function with C++ type T is equivalent to calling the specific Get
|
||||
/// function above that returns a value or reference to a type T.
|
||||
///
|
||||
/// If a value corresponding to the C++ type is not being held, this
|
||||
/// method raises a coding error. See Get functions above for default
|
||||
/// value returned in this case.
|
||||
template <typename T,
|
||||
typename ReturnType = typename std::conditional<
|
||||
std::is_same<T, JsObject>::value ||
|
||||
std::is_same<T, JsArray>::value ||
|
||||
std::is_same<T, std::string>::value,
|
||||
const T&, T>::type>
|
||||
ReturnType Get() const {
|
||||
return _Get(static_cast<T*>(nullptr));
|
||||
}
|
||||
|
||||
/// Returns a vector holding the elements of this value's array that
|
||||
/// correspond to the C++ type specified as the template parameter.
|
||||
/// If this value is not holding an array, an empty vector is returned.
|
||||
/// If any of the array's elements does not correspond to the C++ type,
|
||||
/// it is replaced with the default value used by the Get functions above.
|
||||
/// In both cases, a coding error will be raised.
|
||||
template <typename T>
|
||||
std::vector<T> GetArrayOf() const;
|
||||
|
||||
/// Returns the type of this value.
|
||||
JS_API Type GetType() const;
|
||||
|
||||
/// Returns a display name for the type of this value.
|
||||
JS_API std::string GetTypeName() const;
|
||||
|
||||
/// Returns true if this value is holding an object type.
|
||||
JS_API bool IsObject() const;
|
||||
|
||||
/// Returns true if this value is holding an array type.
|
||||
JS_API bool IsArray() const;
|
||||
|
||||
/// Returns true if this value is holding a string type.
|
||||
JS_API bool IsString() const;
|
||||
|
||||
/// Returns true if this value is holding a boolean type.
|
||||
JS_API bool IsBool() const;
|
||||
|
||||
/// Returns true if this value is holding an integer type.
|
||||
JS_API bool IsInt() const;
|
||||
|
||||
/// Returns true if this value is holding a real type.
|
||||
JS_API bool IsReal() const;
|
||||
|
||||
/// Returns true if this value is holding a 64-bit unsigned integer.
|
||||
JS_API bool IsUInt64() const;
|
||||
|
||||
/// Returns true if this value is holding a type that corresponds
|
||||
/// to the C++ type specified as the template parameter.
|
||||
template <typename T>
|
||||
bool Is() const {
|
||||
return _Is(static_cast<T*>(nullptr));
|
||||
}
|
||||
|
||||
/// Returns true if this value is holding an array whose elements all
|
||||
/// correspond to the C++ type specified as the template parameter.
|
||||
template <typename T>
|
||||
bool IsArrayOf() const;
|
||||
|
||||
/// Returns true if this value is null, false otherwise.
|
||||
JS_API bool IsNull() const;
|
||||
|
||||
/// Evaluates to true if this value is not null.
|
||||
JS_API explicit operator bool() const;
|
||||
|
||||
/// Returns true of both values hold the same type and the underlying held
|
||||
/// values are equal.
|
||||
JS_API bool operator==(const JsValue& other) const;
|
||||
|
||||
/// Returns true if values are of different type, or the underlying held
|
||||
/// values are not equal.
|
||||
JS_API bool operator!=(const JsValue& other) const;
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
struct _InvalidTypeHelper : public std::false_type { };
|
||||
|
||||
template <class T>
|
||||
T _Get(T*) const {
|
||||
static_assert(_InvalidTypeHelper<T>::value,
|
||||
"Invalid type for JsValue");
|
||||
return T();
|
||||
}
|
||||
|
||||
const JsObject& _Get(JsObject*) const { return GetJsObject(); }
|
||||
const JsArray& _Get(JsArray*) const { return GetJsArray(); }
|
||||
const std::string& _Get(std::string*) const { return GetString(); }
|
||||
bool _Get(bool*) const { return GetBool(); }
|
||||
int _Get(int*) const { return GetInt(); }
|
||||
int64_t _Get(int64_t*) const { return GetInt64(); }
|
||||
uint64_t _Get(uint64_t*) const { return GetUInt64(); }
|
||||
double _Get(double*) const { return GetReal(); }
|
||||
|
||||
template <class T>
|
||||
bool _Is(T*) const {
|
||||
static_assert(_InvalidTypeHelper<T>::value,
|
||||
"Invalid type for JsValue");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _Is(JsObject*) const { return IsObject(); }
|
||||
bool _Is(JsArray*) const { return IsArray(); }
|
||||
bool _Is(std::string*) const { return IsString(); }
|
||||
bool _Is(bool*) const { return IsBool(); }
|
||||
bool _Is(int*) const { return IsInt(); }
|
||||
bool _Is(int64_t*) const { return IsInt(); }
|
||||
bool _Is(uint64_t*) const { return IsUInt64(); }
|
||||
bool _Is(double*) const { return IsReal(); }
|
||||
|
||||
struct _Holder;
|
||||
std::shared_ptr<_Holder> _holder;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<T> JsValue::GetArrayOf() const
|
||||
{
|
||||
const JsArray& array = GetJsArray();
|
||||
std::vector<T> result(array.size());
|
||||
std::transform(array.begin(), array.end(), result.begin(),
|
||||
[](const JsValue& v) { return v.Get<T>(); });
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool JsValue::IsArrayOf() const
|
||||
{
|
||||
if (!IsArray()) {
|
||||
return false;
|
||||
}
|
||||
const JsArray& array = GetJsArray();
|
||||
return std::all_of(array.begin(), array.end(),
|
||||
[](const JsValue& v) { return v.Is<T>(); });
|
||||
}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_VALUE_H
|
||||
Reference in new issue
Block a user