StdVariantSerialization.hpp
1 #pragma once
2 #include <score/serialization/BoostVariant2Serialization.hpp>
3 //
4 // #include <score/serialization/DataStreamVisitor.hpp>
5 // #include <score/serialization/JSONVisitor.hpp>
6 // #include <score/tools/Metadata.hpp>
7 //
8 // #include <ossia/detail/for_each.hpp>
9 //
10 // #include <variant>
11 //
12 // /**
13 // * @file StdVariantSerialization
14 // *
15 // * @brief Used for serialization of std::variant classes.
16 // *
17 // * This saves the index and the current element, for both JSON and QDataStream,
18 // * by iterating at compile time up to the "right" point in the variant.
19 // *
20 // */
21 //
22 // /**
23 // * @class StdVariantDataStreamSerializer
24 // * @see StdVariantSerialization
25 // */
26 //
27 // /**
28 // * @class StdVariantDataStreamDeserializer
29 // * @see StdVariantSerialization
30 // */
31 //
32 // /**
33 // * @class StdVariantJSONSerializer
34 // * @see StdVariantSerialization
35 // */
36 //
37 // /**
38 // * @class StdVariantJSONDeserializer
39 // * @see StdVariantSerialization
40 // */
41 //
42 // template <typename T>
43 // struct StdVariantDataStreamSerializer
44 // {
45 // StdVariantDataStreamSerializer(DataStream::Serializer& s_p, const T& var_p)
46 // : s{s_p}
47 // , var{var_p}
48 // {
49 // }
50 //
51 // DataStream::Serializer& s;
52 // const T& var;
53 //
54 // bool done = false;
55 //
56 // template <typename TheClass>
57 // void operator()();
58 // };
59 //
60 // template <typename T>
61 // template <typename TheClass>
62 // void StdVariantDataStreamSerializer<T>::operator()()
63 // {
64 // // This trickery iterates over all the types in Args...
65 // // A single type should be serialized, even if we cannot break.
66 // if (done)
67 // return;
68 // if (auto res = std::get_if<TheClass>(&var))
69 // {
70 // s.stream() << *res;
71 // done = true;
72 // }
73 // }
74 //
75 // template <typename T>
76 // struct StdVariantDataStreamDeserializer
77 // {
78 // StdVariantDataStreamDeserializer(
79 // DataStream::Deserializer& s_p,
80 // quint64 which_p,
81 // T& var_p)
82 // : s{s_p}
83 // , which{which_p}
84 // , var{var_p}
85 // {
86 // }
87 //
88 // DataStream::Deserializer& s;
89 // quint64 which;
90 // T& var;
91 //
92 // quint64 i = 0;
93 // template <typename TheClass>
94 // void operator()();
95 // };
96 //
97 // template <typename T>
98 // template <typename TheClass>
99 // void StdVariantDataStreamDeserializer<T>::operator()()
100 // {
101 // // Here we iterate until we are on the correct type, and we deserialize it.
102 // if (i++ != which)
103 // return;
104 //
105 // TheClass data;
106 // s.stream() >> data;
107 // var = std::move(data);
108 // }
109 //
110 // template <typename... Args>
111 // struct TSerializer<DataStream, std::variant<Args...>>
112 // {
113 // using var_t = std::variant<Args...>;
114 // static void readFrom(DataStream::Serializer& s, const var_t& var)
115 // {
116 // s.stream() << (quint64)var.index();
117 //
118 // ossia::for_each_type<Args...>(
119 // StdVariantDataStreamSerializer<var_t>{s, var});
120 //
121 // s.insertDelimiter();
122 // }
123 //
124 // static void writeTo(DataStream::Deserializer& s, var_t& var)
125 // {
126 // quint64 which;
127 // s.stream() >> which;
128 //
129 // ossia::for_each_type<Args...>(
130 // StdVariantDataStreamDeserializer<var_t>{s, which, var});
131 // s.checkDelimiter();
132 // }
133 // };
134 //
135 // // This part is required because it isn't as straightforward to save variant
136 // // data
137 // // in JSON as it is to save it in a DataStream.
138 // // Basically, each variant member has an associated name that will be the
139 // // key in the JSON parent object. This name is defined by specializing
140 // // template<> class Metadata<Json_k, T>.
141 // // For instance:
142 // // template<> class Metadata<Json_k, score::Address>
143 // // { public: static constexpr const char * get() { return "Address"; } };
144 // // A JSON_METADATA macro is provided for this.
145 //
146 // // This allows easy store and retrieval under a familiar name
147 //
148 // // TODO add some ASSERT for the variant being set on debug mode. npos case
149 // // should not happen since we have the std::optionalVariant.
150 //
151 // template <typename T>
152 // struct StdVariantJSONSerializer
153 // {
154 // StdVariantJSONSerializer(JSONObject::Serializer& s_p, const T& var_p)
155 // : s{s_p}
156 // , var{var_p}
157 // {
158 // }
159 // JSONObject::Serializer& s;
160 // const T& var;
161 //
162 // bool done = false;
163 //
164 // template <typename TheClass>
165 // void operator()();
166 // };
167 //
168 // template <typename T>
169 // template <typename TheClass>
170 // void StdVariantJSONSerializer<T>::operator()()
171 // {
172 // if (done)
173 // return;
174 //
175 // if (auto res = std::get_if<TheClass>(&var))
176 // {
177 // s.obj[Metadata<Json_k, TheClass>::get()] = *res;
178 // done = true;
179 // }
180 // }
181 //
182 // template <typename T>
183 // struct StdVariantJSONDeserializer
184 // {
185 // StdVariantJSONDeserializer(JSONObject::Deserializer& s_p, T& var_p)
186 // : s{s_p}
187 // , var{var_p}
188 // {
189 // }
190 // JSONObject::Deserializer& s;
191 // T& var;
192 //
193 // bool done = false;
194 // template <typename TheClass>
195 // void operator()();
196 // };
197 //
198 // template <typename T>
199 // template <typename TheClass>
200 // void StdVariantJSONDeserializer<T>::operator()()
201 // {
202 // if (done)
203 // return;
204 //
205 // if (auto it = s.obj.tryGet(Metadata<Json_k, TheClass>::get()))
206 // {
207 // JSONWriter w{*it};
208 // TheClass obj;
209 // obj <<= JsonValue{w.base};
210 // var = std::move(obj);
211 // done = true;
212 // }
213 // }
214 //
215 // template <typename... Args>
216 // struct TSerializer<JSONObject, std::variant<Args...>>
217 // {
218 // using var_t = std::variant<Args...>;
219 // static void readFrom(JSONObject::Serializer& s, const var_t& var)
220 // {
221 // s.stream.StartObject();
222 // ossia::for_each_type<Args...>(StdVariantJSONSerializer<var_t>{s, var});
223 // s.stream.EndObject();
224 // }
225 //
226 // static void writeTo(JSONObject::Deserializer& s, var_t& var)
227 // {
228 // if (s.base.MemberCount() == 0)
229 // return;
230 // ossia::for_each_type<Args...>(StdVariantJSONDeserializer<var_t>{s, var});
231 // }
232 // };
233 //