11 #ifndef __NET_ConvertToType_H__
12 #define __NET_ConvertToType_H__
27 #include <type_traits>
29 #define NET_REQUIRE(_cond_) std::enable_if_t<_cond_>
38 return obj.serialize(writer);
44 return obj.serialize(data);
51 #define NET_DEFINE_INT_SERIALIZER(_T_) \
53 inline bool NETserialize<_T_>(UT_JSONWriter& writer, const _T_& obj) \
55 writer.jsonValue(obj); \
59 inline bool NETserialize<_T_>(const UT_JSONValue& data, _T_& item) \
62 bool res = data.import(v); \
63 item = static_cast<_T_>(v); \
69 #undef NET_DEFINE_INT_SERIALIZER
74 writer.jsonValue(obj);
81 return data.import(obj);
87 writer.jsonValue(static_cast<int32>(obj));
95 bool res =
data.import(tmp);
96 obj =
static_cast<uint32>(tmp);
107 writer.jsonValue(obj);
114 return data.import(item);
124 writer.jsonValue(obj);
131 return data.import(obj);
137 writer.jsonValue(obj);
150 writer.jsonValue(obj);
165 writer.jsonValue(obj.asRef());
179 writer.jsonValue(obj);
186 return data.import(obj);
196 writer.jsonValue(
id.toString());
204 if (!
data.import(str))
207 return id.setString(str);
214 template <
typename T,
template <
typename>
class ARRAY>
218 for (
auto&& item : items)
219 NETserialize<T>(writer, item);
220 writer.jsonEndArray();
224 template <
typename T,
template <
typename>
class ARRAY>
246 #define NET_DEFINE_ARRAY_SERIALIZER(_ARRAY_) \
247 template <typename T> \
248 inline bool NETserialize(UT_JSONWriter& writer, const _ARRAY_<typename T::value_type>& items) \
250 return NETserializeArray<typename T::value_type, _ARRAY_>(writer, items); \
252 template <typename T> \
253 inline bool NETserialize(const UT_JSONValue& data, _ARRAY_<typename T::value_type>& items) \
255 return NETserializeArray<typename T::value_type, _ARRAY_>(data, items); \
259 #undef NET_DEFINE_ARRAY_SERIALIZER
264 return writer.jsonStringArray(items);
278 return data.import(items);
285 template <
typename SetT>
290 for (
auto&& item : items)
291 NETserialize<T>(writer, item);
292 writer.jsonEndArray();
296 template <
typename SetT>
301 std::is_default_constructible_v<T>,
302 "Serialize of a set requires the type to be default constructibe");
313 for (
auto&&
value : data)
316 NETserialize<T>(
value, items_value);
317 items.emplace(items_value);
322 #define NET_DEFINE_SET_SERIALIZER(_SET_) \
323 template <typename T> \
324 inline bool NETserialize(UT_JSONWriter& writer, const _SET_<typename T::value_type>& items) \
326 return NETserializeSet<T>(writer, items); \
328 template <typename T> \
329 inline bool NETserialize(const UT_JSONValue& data, _SET_<typename T::value_type>& items) \
331 return NETserializeSet<T>(data, items); \
334 #undef NET_DEFINE_SET_SERIALIZER
340 return writer.jsonSetAsArray(items);
356 return NETserializeSet<UT_StringSet>(
data, items);
366 writer.jsonValue(
value);
380 template <
typename T1,
typename T2>
384 const std::pair<T1, T2> &obj)
387 NETserialize<T1>(writer, obj.first);
388 NETserialize<T2>(writer, obj.second);
389 writer.jsonEndArray();
393 template <
typename T1,
typename T2>
397 std::pair<T1, T2> &obj)
401 return NETserialize<T1>(data[0], obj.first)
402 && NETserialize<T2>(data[1], obj.second);
407 template <
typename T>
411 const std::pair<typename T::first_type, typename T::second_type>& p)
415 template <
typename T>
419 std::pair<typename T::first_type, typename T::second_type>& p)
460 namespace NET_ConvertToType
466 typedef std::remove_const_t<std::remove_reference_t<T>>
type;
474 template <
typename Tto,
typename Enable =
void>
480 UT_ASSERT(!
"This code should never be entered. You have not setup a proper cast for this type.");
485 template <
typename Tto>
488 NET_remove_cref_t<Tto>>::value>>
494 template <
typename Tto>
497 NET_remove_cref_t<Tto>>::value>>
516 for (
auto &&value : values)
525 template <
typename Tto>
528 std::
enable_if_t<std::is_same<UT_IntArray, NET_remove_cref_t<Tto>>::value>>
537 for (
auto &&
value : values)
547 template <
typename Tto>
550 !std::is_same<Tto, bool>::value>>
557 return source.
toInt();
561 template <
typename Tto>
573 template <
typename Tto>
581 return source.
toInt();
584 return low_source ==
"true";
590 template <
typename Tto,
typename Tfrom>
597 #endif // __NET_ConvertToType_H__
static Tto cast(const UT_StringHolder &source)
typename std::enable_if< B, T >::type enable_if_t
Define Imath::enable_if_t to be std for C++14, equivalent for C++11.
bool NETserialize< UT_String >(UT_JSONWriter &writer, const UT_String &obj)
bool NETserialize< UT_WorkBuffer >(UT_JSONWriter &writer, const UT_WorkBuffer &obj)
bool parseValue(UT_JSONParser &parser, UT_IStream *is=0, bool record_source_offsets=false)
bool NETserializeSet(UT_JSONWriter &writer, const SetT &items)
GLsizei const GLfloat * value
bool isEmpty() const
Same as !isstring()
bool isFloat(bool skip_spaces=false, bool loose=false) const
Determine if string can be seen as a single floating point number.
typename NET_remove_cref< T >::type NET_remove_cref_t
bool NETserialize< NET_EmptySerializer >(UT_JSONWriter &writer, const NET_EmptySerializer &)
void setCapacity(exint new_capacity)
bool NETserialize< bool >(UT_JSONWriter &writer, const bool &obj)
Tto convert(const Tfrom &source)
Class which writes ASCII or binary JSON streams.
bool NETserialize< UT_SGuid >(UT_JSONWriter &writer, const UT_SGuid &id)
**But if you need a result
bool NETserialize< UT_StringArray >(UT_JSONWriter &writer, const UT_StringArray &items)
bool NETserialize< UT_StringLit >(UT_JSONWriter &writer, const UT_StringLit &obj)
bool isInteger(bool skip_spaces=false) const
Determine if string can be seen as a single integer number.
A utility class to do read-only operations on a subset of an existing string.
bool NETserializePair(UT_JSONWriter &writer, const std::pair< T1, T2 > &obj)
exint emplace_back(S &&...s)
SYS_FORCE_INLINE const char * buffer() const
bool NETserialize< int64 >(UT_JSONWriter &writer, const int64 &obj)
bool NETserialize(UT_JSONWriter &writer, const T &obj)
GLsizei GLsizei GLchar * source
SYS_NO_DISCARD_RESULT indexed_proxy enumerate()
SYS_NO_DISCARD_RESULT bool isArray() const
#define NET_DEFINE_SET_SERIALIZER(_SET_)
#define NET_DEFINE_INT_SERIALIZER(_T_)
SYS_NO_DISCARD_RESULT const UT_StringHolder * getStringHolder() const
Return the string value.
static Tto cast(const UT_StringHolder &source)
static Tto cast(const UT_StringHolder &source)
static Tto cast(const UT_StringHolder &source)
bool NETserialize< UT_JSONValue >(UT_JSONWriter &writer, const UT_JSONValue &value)
GLenum GLsizei GLsizei GLint * values
SYS_NO_DISCARD_RESULT exint count() const
Number of elements contained inside the json value.
SYS_NO_DISCARD_RESULT UT_StringRef toLower() const
static Tto cast(const UT_StringHolder &source)
#define NET_DEFINE_ARRAY_SERIALIZER(_ARRAY_)
bool jsonBeginArray()
Begin a generic array object.
SYS_NO_DISCARD_RESULT Type getType() const
Get the type of data stored in the object.
Class to store JSON objects as C++ objects.
static Tto cast(const UT_StringHolder &source)
bool NETserialize< UT_StringRef >(UT_JSONWriter &writer, const UT_StringRef &obj)
SYS_NO_DISCARD_RESULT const UT_StringHolder & getString() const
static Tto cast(const UT_StringHolder &source)
std::remove_const_t< std::remove_reference_t< T > > type
bool NETserialize< UT_StringHolder >(UT_JSONWriter &writer, const UT_StringHolder &obj)
bool NETserialize< UT_StringSet >(UT_JSONWriter &writer, const UT_StringSet &items)
bool NETserialize< uint32 >(UT_JSONWriter &writer, const uint32 &obj)
bool NETserializeArray(UT_JSONWriter &writer, const ARRAY< T > &items)