-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.cpp
46 lines (42 loc) · 1.32 KB
/
types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <typeinfo>
#include <stdexcept>
namespace tokox::json
{
value_type any_to_value_type(const std::any& v)
{
if (!v.has_value())
return Null;
else if (v.type() == typeid(object_type_from_value_type<Bool>))
return Bool;
else if (v.type() == typeid(object_type_from_value_type<Int>))
return Int;
else if (v.type() == typeid(object_type_from_value_type<Float>))
return Float;
else if (v.type() == typeid(object_type_from_value_type<String>))
return String;
else if (v.type() == typeid(object_type_from_value_type<Vector>))
return Vector;
else if (v.type() == typeid(object_type_from_value_type<Map>))
return Map;
else
throw std::runtime_error("Unknown value_type!");
}
template<object_type T>
value_type value_type_from_object_type()
{
if constexpr (std::is_same_v<T, std::nullptr_t>)
return Null;
else if constexpr (std::is_same_v<T, bool>)
return Bool;
else if constexpr (std::is_same_v<T, object_type_from_value_type<Int>>)
return Int;
else if constexpr (std::is_same_v<T, object_type_from_value_type<Float>>)
return Float;
else if constexpr (std::is_same_v<T, object_type_from_value_type<String>>)
return String;
else if constexpr (std::is_same_v<T, object_type_from_value_type<Vector>>)
return Vector;
else if constexpr (std::is_same_v<T, object_type_from_value_type<Map>>)
return Map;
}
}