SerializableHelpers.hpp
1 #pragma once
2 #include <score/plugins/SerializableInterface.hpp>
4 #include <score/serialization/JSONVisitor.hpp>
6 
18 template <typename FactoryList_T, typename... Args>
19 auto deserialize_interface(
20  const FactoryList_T& factories, DataStream::Deserializer& des, Args&&... args) ->
21  typename FactoryList_T::object_type*
22 {
23  QByteArray b;
24  des.stream() >> b;
26 
27  // Deserialize the interface identifier
28  try
29  {
30  SCORE_DEBUG_CHECK_DELIMITER2(sub);
31  typename FactoryList_T::factory_type::ConcreteKey k;
33  sub, k);
34 
35  SCORE_DEBUG_CHECK_DELIMITER2(sub);
36  // Get the factory
37  if(auto concrete_factory = factories.get(k))
38  {
39  // Create the object
40  auto obj = concrete_factory->load(sub.toVariant(), std::forward<Args>(args)...);
41 
42  SCORE_DEBUG_CHECK_DELIMITER2(sub);
43 
44  return obj;
45  }
46  }
47  catch(...)
48  {
49  }
50 
51  // If the object could not be loaded, we try to load a "missing" version of
52  // it.
53  return factories.loadMissing(sub.toVariant(), std::forward<Args>(args)...);
54 }
55 
56 template <typename FactoryList_T, typename... Args>
57 auto deserialize_interface(
58  const FactoryList_T& factories, DataStream::Deserializer&& des, Args&&... args) ->
59  typename FactoryList_T::object_type*
60 {
61  QByteArray b;
62  des.stream() >> b;
64 
65  // Deserialize the interface identifier
66  try
67  {
68  SCORE_DEBUG_CHECK_DELIMITER2(sub);
69  typename FactoryList_T::factory_type::ConcreteKey k;
71  sub, k);
72 
73  SCORE_DEBUG_CHECK_DELIMITER2(sub);
74  // Get the factory
75  if(auto concrete_factory = factories.get(k))
76  {
77  // Create the object
78  auto obj = concrete_factory->load(sub.toVariant(), std::forward<Args>(args)...);
79 
80  SCORE_DEBUG_CHECK_DELIMITER2(sub);
81 
82  return obj;
83  }
84  }
85  catch(...)
86  {
87  }
88 
89  // If the object could not be loaded, we try to load a "missing" version of
90  // it.
91  return factories.loadMissing(sub.toVariant(), std::forward<Args>(args)...);
92 }
93 
94 template <typename FactoryList_T, typename... Args>
95 auto deserialize_interface(
96  const FactoryList_T& factories, JSONObject::Deserializer& des, Args&&... args) ->
97  typename FactoryList_T::object_type*
98 {
99  // Deserialize the interface identifier
100  try
101  {
102  typename FactoryList_T::factory_type::ConcreteKey k;
103  JSONWriter wr{des.obj[des.strings.uuid]};
105  wr, k);
106 
107  // Get the factory
108  if(auto concrete_factory = factories.get(k))
109  {
110  // Create the object
111  return concrete_factory->load(des.toVariant(), std::forward<Args>(args)...);
112  }
113  }
114  catch(...)
115  {
116  }
117 
118  // If the object could not be loaded, we try to load a "missing" version of
119  // it.
120  return factories.loadMissing(des.toVariant(), std::forward<Args>(args)...);
121 }
122 
123 template <typename FactoryList_T, typename... Args>
124 auto deserialize_interface(
125  const FactoryList_T& factories, JSONObject::Deserializer&& des, Args&&... args) ->
126  typename FactoryList_T::object_type*
127 {
128  // Deserialize the interface identifier
129  try
130  {
131  typename FactoryList_T::factory_type::ConcreteKey k;
132  JSONWriter wr{des.obj[des.strings.uuid]};
134  wr, k);
135 
136  // Get the factory
137  if(auto concrete_factory = factories.get(k))
138  {
139  // Create the object
140  return concrete_factory->load(des.toVariant(), std::forward<Args>(args)...);
141  }
142  }
143  catch(...)
144  {
145  }
146 
147  // If the object could not be loaded, we try to load a "missing" version of
148  // it.
149  return factories.loadMissing(des.toVariant(), std::forward<Args>(args)...);
150 }
Definition: DataStreamVisitor.hpp:202
Definition: JSONVisitor.hpp:423
Definition: VisitorInterface.hpp:13