EntitySerialization.hpp
1 #pragma once
2 #include <score/model/EntityBase.hpp>
4 #include <score/serialization/JSONVisitor.hpp>
5 
6 template <typename T>
8 {
10 }
11 
12 template <typename T>
14 {
16 }
17 
18 template <typename T>
19 struct TSerializer<DataStream, score::Entity<T>>
20 {
21  static void readFrom(DataStream::Serializer& s, const score::Entity<T>& obj)
22  {
24  s.readFrom(obj.metadata());
25 
26 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
27  // Save components
28  score::DataStreamComponents vec;
29  for(auto& comp : obj.components())
30  {
31  if(auto c = dynamic_cast<score::SerializableComponent*>(&comp))
32  {
33  vec[c->concreteKey()] = s.marshall(*c);
34  }
35  }
36 
37  s.readFrom(std::move(vec));
38 #endif
39  SCORE_DEBUG_INSERT_DELIMITER2(s);
40  }
41 
42  static void writeTo(DataStream::Deserializer& s, score::Entity<T>& obj)
43  {
44  s.writeTo(obj.metadata());
45 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
46 
47  // Reload components
48  score::DataStreamComponents vec;
49  s.writeTo(vec);
50  if(!vec.empty())
51  {
52  // TODO we use id -1, there should be a better way... for now it will
53  // work since id's begin at 1.
54  auto comp = new score::DataStreamSerializedComponents{
55  Id<score::Component>{-1}, std::move(vec), &obj};
56  obj.components().add(comp);
57  }
58 #endif
59  SCORE_DEBUG_CHECK_DELIMITER2(s);
60  }
61 };
62 
63 template <typename T>
64 struct TSerializer<JSONObject, score::Entity<T>>
65 {
66  static void readFrom(JSONObject::Serializer& s, const score::Entity<T>& obj)
67  {
69  s.obj[s.strings.Metadata] = obj.metadata();
70 
71 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
72  // Save components
73  QJsonArray json_components;
74  for(auto& comp : obj.components())
75  {
76  if(auto c = dynamic_cast<score::SerializableComponent*>(&comp))
77  {
78  json_components.append(s.marshall(*c));
79  }
80  }
81 
82  s.obj[s.strings.Components] = std::move(json_components);
83 #endif
84  }
85 
86  static void writeTo(JSONObject::Deserializer& s, score::Entity<T>& obj)
87  {
88  {
89  JSONWriter writer{s.obj[s.strings.Metadata]};
90  writer.writeTo(obj.metadata());
91  }
92 
93 #if defined(SCORE_SERIALIZABLE_COMPONENTS)
94  const QJsonArray json_components = s.obj[s.strings.Components].toArray();
95  if(!json_components.empty())
96  {
97  score::JSONComponents vec;
98  for(const auto& comp : json_components)
99  {
100  // Since the component is a SerializableInterface, it has an uuid
101  // attribute.
102  auto obj = comp.toObject();
103 
105  = fromJsonValue<score::uuid_t>(obj[s.strings.uuid]);
106  vec.emplace(k, std::move(obj));
107  }
108  // TODO we use id -1, there should be a better way... for now it will
109  // work since id's begin at 1.
110  auto comp = new score::JSONSerializedComponents{
111  Id<score::Component>{-1}, std::move(vec), &obj};
112  obj.components().add(comp);
113  }
114 #endif
115  }
116 };
117 
119 {
121  template <typename T>
122  static void readFrom(DataStream::Serializer& s, const T& vec)
123  {
124  s.m_stream << (int32_t)vec.size();
125  for(auto* v : vec)
126  s.readFrom(*v);
127  }
128 
129  template <typename List, typename OnSucces, typename OnFailure>
130  static void writeTo(
131  DataStream::Deserializer& s, const List& lst, QObject* parent,
132  const OnSucces& success, const OnFailure& fail)
133  {
134  int32_t count;
135  s.m_stream >> count;
136  for(; count-- > 0;)
137  {
138  auto proc = deserialize_interface(lst, s, parent);
139  if(proc)
140  {
141  success(proc);
142  }
143  else
144  {
145  fail();
146  }
147  }
148  }
149 
150  template <typename T>
151  static void readFrom(JSONObject::Serializer& s, const T& vec)
152  {
153  s.stream.StartArray();
154  for(const auto* elt : vec)
155  s.readFrom(*elt);
156  s.stream.EndArray();
157  }
158 
159  template <typename List, typename OnSucces, typename OnFailure>
160  static void writeTo(
161  const JSONObject::Deserializer& s, const List& lst, QObject* parent,
162  const OnSucces& success, const OnFailure& fail)
163  {
164  for(const auto& json_vref : s.base.GetArray())
165  {
166  auto proc
167  = deserialize_interface(lst, JSONObject::Deserializer{json_vref}, parent);
168  if(proc)
169  {
170  success(proc);
171  }
172  else
173  {
174  fail(json_vref);
175  }
176  }
177  }
178 };
179 
180 template <typename T, typename Alloc>
181 struct TSerializer<DataStream, std::vector<T*, Alloc>> : ArrayEntitySerializer
182 {
183 };
184 template <typename T, typename Alloc>
185 struct TSerializer<JSONObject, std::vector<T*, Alloc>> : ArrayEntitySerializer
186 {
187 };
188 
189 template <typename T, std::size_t N>
190 struct TSerializer<DataStream, boost::container::small_vector<T*, N>>
192 {
193 };
194 template <typename T, std::size_t N>
195 struct TSerializer<JSONObject, boost::container::small_vector<T*, N>>
197 {
198 };
Definition: VisitorInterface.hpp:53
Definition: DataStreamVisitor.hpp:27
Definition: DataStreamVisitor.hpp:202
Definition: VisitorInterface.hpp:61
Definition: JSONVisitor.hpp:52
void readFrom(const score::Entity< T > &obj)
Definition: EntitySerialization.hpp:7
Definition: JSONVisitor.hpp:423
Definition: UuidKey.hpp:343
The id_base_t class.
Definition: Identifier.hpp:57
Base for complex model objects.
Definition: EntityBase.hpp:24
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: EntitySerialization.hpp:119
static void readFrom(DataStream::Serializer &s, const T &vec)
Arrays of pointed-to objects.
Definition: EntitySerialization.hpp:122
Definition: VisitorInterface.hpp:13