Loading...
Searching...
No Matches
VisitorCommon.hpp
Go to the documentation of this file.
1#pragma once
3#include <score/serialization/JSONVisitor.hpp>
4
5#include <type_traits>
6
22namespace score
23{
24
25template <typename T>
26void serialize_dyn_impl(DataStream::Serializer& v, const T& t)
27{
28 if constexpr(is_custom_serialized<T>::value)
29 TSerializer<DataStream, T>{}.readFrom(v, t);
30 else
31 v.read(t);
32}
33
34template <typename T>
35void serialize_dyn_impl(JSONObject::Serializer& v, const T& t)
36{
37 if constexpr(is_custom_serialized<T>::value)
38 TSerializer<JSONObject, T>{}.readFrom(v, t);
39 else
40 v.read(t);
41}
42
43template <typename TheClass>
44void serialize_dyn(const VisitorVariant& vis, const TheClass& s)
45{
46 if(vis.identifier == DataStream::type())
47 {
48 serialize_dyn_impl(static_cast<DataStream::Serializer&>(vis.visitor), s);
49 return;
50 }
51 else if(vis.identifier == JSONObject::type())
52 {
53 serialize_dyn_impl(static_cast<JSONObject::Serializer&>(vis.visitor), s);
54 return;
55 }
56
57 SCORE_ABORT;
58}
59
60template <typename TheClass>
61TheClass& deserialize_dyn(const VisitorVariant& vis, TheClass& s)
62{
63 switch(vis.identifier)
64 {
65 case DataStream::type(): {
66 static_cast<DataStream::Deserializer&>(vis.visitor).writeTo(s);
67 break;
68 }
69 case JSONObject::type(): {
70 static_cast<JSONObject::Deserializer&>(vis.visitor).writeTo(s);
71 break;
72 }
73 default:
74 SCORE_ABORT;
75 }
76
77 return s;
78}
79
80template <typename TheClass>
81TheClass deserialize_dyn(const VisitorVariant& vis)
82{
83 TheClass s;
84
85 switch(vis.identifier)
86 {
87 case DataStream::type(): {
88 static_cast<DataStream::Deserializer&>(vis.visitor).writeTo(s);
89 break;
90 }
91 case JSONObject::type(): {
92 static_cast<JSONObject::Deserializer&>(vis.visitor).writeTo(s);
93 break;
94 }
95 default:
96 SCORE_ABORT;
97 }
98 return s;
99}
100
101template <typename Functor>
102auto deserialize_dyn(const VisitorVariant& vis, Functor&& fun)
103{
104 switch(vis.identifier)
105 {
106 case DataStream::type(): {
107 return fun(static_cast<DataStream::Deserializer&>(vis.visitor));
108 break;
109 }
110 case JSONObject::type(): {
111 return fun(static_cast<JSONObject::Deserializer&>(vis.visitor));
112 break;
113 }
114 default:
115 SCORE_ABORT;
116 throw;
117 }
118}
119
128template <typename Type, typename Object>
129auto marshall(const Object& obj)
130{
131 return Type::Serializer::marshall(obj);
132}
133
134template <typename Object>
135auto unmarshall(const rapidjson::Value& obj)
136{
137 Object data;
138 JSONWriter wrt{obj};
139 wrt.writeTo(data);
140 return data;
141}
142
143template <typename Object>
144auto unmarshall(const QByteArray& arr)
145{
146 return DataStreamWriter::unmarshall<Object>(arr);
147}
148}
Definition DataStreamVisitor.hpp:27
void read(const T &)
Definition DataStreamVisitor.hpp:202
Definition JSONVisitor.hpp:52
Definition JSONVisitor.hpp:423
Base toolkit upon which the software is built.
Definition Application.cpp:90
auto marshall(const Object &obj)
marshall Serializes a single object
Definition VisitorCommon.hpp:129
Definition VisitorInterface.hpp:13
The VisitorVariant struct.
Definition VisitorInterface.hpp:26