ValueSerializationImpl.hpp
1 #pragma once
2 #include <State/OSSIASerializationImpl.hpp>
3 
4 #include <ossia/detail/size.hpp>
5 #include <ossia/detail/typelist.hpp>
6 
7 template <>
8 struct TSerializer<JSONObject, ossia::value_variant_type>
9 {
10  using var_t = ossia::value_variant_type;
11 
12  using value_type_list = ossia::tl<
13  float, int, ossia::vec2f, ossia::vec3f, ossia::vec4f, ossia::impulse, bool,
14  std::string, std::vector<ossia::value>, ossia::value_map_type>;
15 
16  static void readFrom(JSONObject::Serializer& s, const var_t& var)
17  {
18  s.stream.StartObject();
19  if((quint64)var.which() != (quint64)var.npos)
20  {
21  ossia::for_each_type(value_type_list{}, VariantJSONSerializer<var_t>{s, var});
22  }
23  s.stream.EndObject();
24  }
25 
26  static void writeTo(JSONObject::Deserializer& s, var_t& var)
27  {
28  if(!s.base.IsObject() || s.base.MemberCount() == 0)
29  return;
30  ossia::for_each_type(value_type_list{}, VariantJSONDeserializer<var_t>{s, var});
31  }
32 };
33 
35 template <typename Functor>
36 void apply_typeonly(
37  Functor&& functor, ossia::value_variant_type::Type type,
38  ossia::value_variant_type& var)
39 {
40  using namespace ossia;
41  switch(type)
42  {
43  case value_variant_type::Type::Type0:
44  return functor(typeholder<float>{}, var);
45  case value_variant_type::Type::Type1:
46  return functor(typeholder<int>{}, var);
47  case value_variant_type::Type::Type2:
48  return functor(typeholder<ossia::vec2f>{}, var);
49  case value_variant_type::Type::Type3:
50  return functor(typeholder<ossia::vec3f>{}, var);
51  case value_variant_type::Type::Type4:
52  return functor(typeholder<ossia::vec4f>{}, var);
53  case value_variant_type::Type::Type5:
54  return functor(typeholder<ossia::impulse>{}, var);
55  case value_variant_type::Type::Type6:
56  return functor(typeholder<bool>{}, var);
57  case value_variant_type::Type::Type7:
58  return functor(typeholder<std::string>{}, var);
59  case value_variant_type::Type::Type8:
60  return functor(typeholder<std::vector<ossia::value>>{}, var);
61  case value_variant_type::Type::Type9:
62  return functor(typeholder<value_map_type>{}, var);
63  default:
64  throw;
65  }
66 }
67 
69 {
71  template <typename T>
72  void operator()(const T& value)
73  {
74  s.stream() << value;
75  }
76 };
77 
78 template <>
79 struct TSerializer<DataStream, ossia::value_variant_type>
80 {
81  using var_t = ossia::value_variant_type;
82  static void readFrom(DataStream::Serializer& s, const var_t& var)
83  {
84  s.stream() << (quint64)var.which();
85 
86  if(var)
87  {
88  ossia::apply_nonnull(ValueVariantDatastreamSerializer{s}, var);
89  }
90 
91  s.insertDelimiter();
92  }
93 
94  static void writeTo(DataStream::Deserializer& s, var_t& var)
95  {
96  quint64 which;
97  s.stream() >> which;
98 
99  if(which != (quint64)var.npos)
100  {
101  apply_typeonly(
102  [&](auto type, var_t& var) {
103  typename decltype(type)::type value;
104  s.stream() >> value;
105  var = std::move(value);
106  },
107  (var_t::Type)which, var);
108  }
109  s.checkDelimiter();
110  }
111 };
112 
114 
115 template <>
116 void DataStreamReader::read(const ossia::impulse& value)
117 {
118 }
119 
120 template <>
121 void DataStreamWriter::write(ossia::impulse& value)
122 {
123 }
124 
125 template <>
126 void JSONReader::read(const ossia::impulse& value)
127 {
128  stream.Null();
129 }
130 
131 template <>
132 void JSONWriter::write(ossia::impulse& value)
133 {
134 }
135 
136 template <>
137 SCORE_LIB_STATE_EXPORT void DataStreamReader::read(const ossia::value& n)
138 {
139  readFrom((const ossia::value_variant_type&)n.v);
140 }
141 
142 template <>
143 SCORE_LIB_STATE_EXPORT void DataStreamWriter::write(ossia::value& n)
144 {
145  writeTo((ossia::value_variant_type&)n.v);
146 }
147 
148 template <>
149 SCORE_LIB_STATE_EXPORT void JSONReader::read(const ossia::value& n)
150 {
152 }
153 
154 template <>
155 SCORE_LIB_STATE_EXPORT void JSONWriter::write(ossia::value& n)
156 {
158 }
Definition: VisitorInterface.hpp:53
Definition: DataStreamVisitor.hpp:27
void readFrom(const score::Entity< T > &obj)
Called by code that wants to serialize.
Definition: DataStreamVisitor.hpp:53
void read(const T &)
Definition: DataStreamVisitor.hpp:202
Definition: VisitorInterface.hpp:61
Definition: JSONVisitor.hpp:52
Definition: JSONVisitor.hpp:423
Definition: VisitorInterface.hpp:13
Definition: ValueSerializationImpl.hpp:69
Definition: VariantSerialization.hpp:183
Definition: VariantSerialization.hpp:152
Definition: OSSIASerializationImpl.hpp:81