CppWAMP
C++11 client library for the WAMP protocol
|
7 #ifndef CPPWAMP_INTERNAL_VARIANTTRAITS_HPP
8 #define CPPWAMP_INTERNAL_VARIANTTRAITS_HPP
12 #include <type_traits>
15 #include "../blob.hpp"
16 #include "../null.hpp"
17 #include "../traits.hpp"
18 #include "../variantdefs.hpp"
26 template <
typename T>
struct FieldTraits;
28 template <
typename T,
typename Enable =
int>
struct ArgTraits;
30 template <
typename T>
struct Access;
33 template <TypeId typeId>
struct FieldTypeForId {};
34 template <>
struct FieldTypeForId<
TypeId::
null> {
using Type = Null;};
37 template <>
struct FieldTypeForId<
TypeId::
uint> {
using Type =
UInt;};
38 template <>
struct FieldTypeForId<
TypeId::
real> {
using Type =
Real;};
40 template <>
struct FieldTypeForId<
TypeId::
blob> {
using Type = Blob;};
46 template <
typename TField>
49 static constexpr
bool isValid =
false;
50 static String typeName() {
return "<unknown>";}
53 template <>
struct FieldTraits<Null>
55 static constexpr
bool isValid =
true;
57 static String typeName() {
return "Null";}
60 template <>
struct FieldTraits<
Bool>
62 static constexpr
bool isValid =
true;
64 static String typeName() {
return "Bool";}
67 template <>
struct FieldTraits<
Int>
69 static constexpr
bool isValid =
true;
71 static String typeName() {
return "Int";}
74 template <>
struct FieldTraits<
UInt>
76 static constexpr
bool isValid =
true;
78 static String typeName() {
return "UInt";}
81 template <>
struct FieldTraits<
Real>
83 static constexpr
bool isValid =
true;
85 static String typeName() {
return "Real";}
88 template <>
struct FieldTraits<
String>
90 static constexpr
bool isValid =
true;
92 static String typeName() {
return "String";}
95 template <>
struct FieldTraits<Blob>
97 static constexpr
bool isValid =
true;
99 static String typeName() {
return "Blob";}
102 template <>
struct FieldTraits<
Array>
104 static constexpr
bool isValid =
true;
106 static String typeName() {
return "Array";}
109 template <>
struct FieldTraits<
Object>
111 static constexpr
bool isValid =
true;
113 static String typeName() {
return "Object";}
117 template <
typename TField,
typename Enable>
120 static constexpr
bool isValid =
false;
121 static String typeName() {
return "[unknown]";}
124 template <>
struct ArgTraits<Null>
126 static constexpr
bool isValid =
true;
127 static String typeName() {
return "Null";}
128 using FieldType = Null;
131 template <
typename TField>
134 static constexpr
bool isValid =
true;
135 static String typeName() {
return "Bool";}
136 using FieldType =
Bool;
139 template <
typename TField>
142 static constexpr
bool isValid =
true;
143 static String typeName() {
return "[signed integer]";}
144 using FieldType =
Int;
147 template <
typename TField>
150 static constexpr
bool isValid =
true;
151 static String typeName() {
return "[unsigned integer]";}
152 using FieldType =
UInt;
155 template <
typename TField>
156 struct ArgTraits<TField,
EnableIf<std::is_floating_point<TField>::value>>
158 static constexpr
bool isValid =
true;
159 static String typeName() {
return "[floating point]";}
160 using FieldType =
Real;
163 template <>
struct ArgTraits<
String>
165 static constexpr
bool isValid =
true;
166 static String typeName() {
return "String";}
170 template <>
struct ArgTraits<
String::value_type*>
172 static constexpr
bool isValid =
true;
173 static String typeName() {
return "[character array]";}
177 template <>
struct ArgTraits<const
String::value_type*>
179 static constexpr
bool isValid =
true;
180 static String typeName() {
return "[character array]";}
184 template <
size_t N>
struct ArgTraits<
String::value_type[N]>
186 static constexpr
bool isValid =
true;
187 static String typeName() {
return "[character array]";}
192 struct ArgTraits<const
String::value_type[N]>
194 static constexpr
bool isValid =
true;
195 static String typeName() {
return "[character array]";}
199 template <>
struct ArgTraits<Blob>
201 static constexpr
bool isValid =
true;
202 static String typeName() {
return "Blob";}
203 using FieldType = Blob;
206 template <>
struct ArgTraits<
Array>
208 static constexpr
bool isValid =
true;
209 static String typeName() {
return "Array";}
210 using FieldType =
Array;
213 template <
typename TElem>
214 struct ArgTraits<std::vector<TElem>,
DisableIf<isSameType<TElem,Variant>()>>
216 static constexpr
bool isValid = ArgTraits<TElem>::isValid;
217 static String typeName() {
return "std::vector<" +
218 ArgTraits<TElem>::typeName() +
'>';}
219 using FieldType =
Array;
222 template <>
struct ArgTraits<
Object>
224 static constexpr
bool isValid =
true;
225 static String typeName() {
return "Object";}
229 template <
typename TValue>
230 struct ArgTraits<std::map<String, TValue>,
231 DisableIf<isSameType<TValue, Variant>()>>
233 static constexpr
bool isValid = ArgTraits<TValue>::isValid;
234 static String typeName() {
return "std::map<String, "
235 + ArgTraits<TValue>::typeName() +
'>';}
241 template <
typename TField>
struct Access
243 template <
typename U>
static void construct(U&& value,
void* field)
244 {get(field) = value;}
246 static void destruct(
void*) {}
248 static TField& get(
void* field)
249 {
return *
static_cast<TField*
>(field);}
251 static const TField& get(
const void* field)
252 {
return *
static_cast<const TField*
>(field);}
255 template <>
struct Access<
String>
257 template <
typename U>
static void construct(U&& value,
void* field)
258 {
new (field)
String(std::forward<U>(value));}
260 static void destruct(
void* field) {get(field).~String();}
262 static String& get(
void* field)
263 {
return *
static_cast<String*
>(field);}
265 static const String& get(
const void* field)
266 {
return *
static_cast<const String*
>(field);}
269 template <>
struct Access<Blob>
271 template <
typename U>
static void construct(U&& value,
void* field)
272 {ptr(field) =
new Blob(std::forward<U>(value));}
274 static void destruct(
void* field)
276 Blob*& b = ptr(field);
281 static Blob& get(
void* field) {
return *ptr(field);}
283 static const Blob& get(
const void* field)
285 const Blob* b = Access<const Blob*>::get(field);
289 static Blob*& ptr(
void* field) {
return Access<Blob*>::get(field);}
292 template <>
struct Access<
Array>
294 template <
typename U>
static void construct(U&& value,
void* field)
295 {ptr(field) =
new Array(std::forward<U>(value));}
297 static void destruct(
void* field)
299 Array*& a = ptr(field);
304 static Array& get(
void* field) {
return *ptr(field);}
306 static const Array& get(
const void* field)
308 const Array* a = Access<const Array*>::get(field);
312 static Array*& ptr(
void* field) {
return Access<Array*>::get(field);}
315 template <>
struct Access<
Object>
317 template <
typename U>
static void construct(U&& value,
void* field)
319 ptr(field) =
new Object(std::forward<U>(value));
322 static void destruct(
void* field)
329 static Object& get(
void* field) {
return *ptr(field);}
331 static const Object& get(
const void* field)
333 const Object* o = Access<const Object*>::get(field);
337 static Object*& ptr(
void* field) {
return Access<Object*>::get(field);}
344 #endif // CPPWAMP_INTERNAL_VARIANTTRAITS_HPP
typename std::enable_if<!B, T >::type DisableIf
Metafunction used to disable overloads based on a boolean condition.
Definition: traits.hpp:43
constexpr bool isSignedInteger()
Determines if the given type is a signed integer.
Definition: traits.hpp:86
TypeId
Integer ID used to indicate the current dynamic type of a Variant.
Definition: variantdefs.hpp:26
std::uint64_t UInt
Variant bound type for unsigned integers.
Definition: variantdefs.hpp:48
constexpr Null null
Constant Null object that can be assigned to, or compared with a Variant.
Definition: null.hpp:68
@ array
For Variant::Array.
@ string
For Variant::String.
std::map< String, Variant > Object
Variant bound type for maps of variants.
Definition: variantdefs.hpp:52
Definition: anyhandler.hpp:36
constexpr bool isUnsignedInteger()
Determines if the given type is an unsigned integer.
Definition: traits.hpp:96
std::vector< Variant > Array
Variant bound type for arrays of variants.
Definition: variantdefs.hpp:51
std::int64_t Int
Variant bound type for signed integers.
Definition: variantdefs.hpp:47
bool Bool
Variant bound type for boolean values.
Definition: variantdefs.hpp:46
@ boolean
For Variant::Bool.
double Real
Variant bound type for floating-point numbers.
Definition: variantdefs.hpp:49
typename std::enable_if< B, T >::type EnableIf
Metafunction used to enable overloads based on a boolean condition.
Definition: traits.hpp:37
constexpr bool isBool()
Determines if the given type is considered a boolean.
Definition: traits.hpp:63
@ object
For Variant::Object.
std::string String
Variant bound type for text strings.
Definition: variantdefs.hpp:50
@ integer
For Variant::Int.