AnySerialization.hpp
Go to the documentation of this file.
1 #pragma once
3 #include <score/serialization/JSONVisitor.hpp>
4 #include <score/tools/std/String.hpp>
5 
6 #include <ossia/detail/any_map.hpp>
7 
20 namespace score
21 {
22 using any_map = ossia::any_map;
23 
24 struct SCORE_LIB_BASE_EXPORT any_serializer
25 {
26  virtual ~any_serializer();
27  virtual void apply(DataStream::Serializer&, const ossia::any&) = 0;
28  virtual void apply(DataStream::Deserializer&, ossia::any&) = 0;
29  virtual void apply(JSONObject::Serializer&, const std::string& key, const ossia::any&)
30  = 0;
31  virtual void apply(JSONObject::Deserializer&, const std::string& key, ossia::any&) = 0;
32  void cast_error(const char*);
33 };
34 
35 template <typename T>
36 struct any_serializer_t final : public any_serializer
37 {
38  void apply(DataStream::Serializer& s, const ossia::any& val) override
39  {
40  if(const T* ptr = std::any_cast<T>(&val))
41  s.stream() << *ptr;
42  else
43  cast_error("");
44  }
45  void apply(DataStream::Deserializer& s, ossia::any& val) override
46  {
47  T t;
48  s.stream() >> t;
49  val = std::move(t);
50  }
51 
52  void apply(
53  JSONObject::Serializer& s, const std::string& key, const ossia::any& val) override
54  {
55  if(const T* ptr = std::any_cast<T>(&val))
56  s.obj[key] = *ptr;
57  else
58  cast_error("");
59  }
60  void
61  apply(JSONObject::Deserializer& s, const std::string& key, ossia::any& val) override
62  {
63  T t;
64  t <<= s.obj[key];
65  val = std::move(t);
66  }
67 };
68 
69 using any_serializer_map = score::hash_map<std::string, std::unique_ptr<any_serializer>>;
70 
72 SCORE_LIB_BASE_EXPORT any_serializer_map& anySerializers();
73 }
74 SCORE_LIB_BASE_EXPORT
75 void apply(DataStreamReader& s, const std::string& key, const ossia::any& v);
76 
77 SCORE_LIB_BASE_EXPORT
78 void apply(DataStreamWriter& s, const std::string& key, ossia::any& v);
79 
80 SCORE_LIB_BASE_EXPORT
81 void apply(JSONReader& s, const std::string& key, const ossia::any& v);
82 
83 SCORE_LIB_BASE_EXPORT
84 void apply(JSONWriter& s, const std::string& key, ossia::any& v);
85 
86 template <>
87 struct SCORE_LIB_BASE_EXPORT TSerializer<DataStream, score::any_map>
88 {
89  static void readFrom(DataStream::Serializer& s, const score::any_map& obj)
90  {
91  auto& st = s.stream();
92 
93  st << (int32_t)obj.size();
94  for(const auto& e : obj)
95  {
96  st << e.first;
97  apply(s, e.first, e.second);
98  }
99  }
100 
101  static void writeTo(DataStream::Deserializer& s, score::any_map& obj)
102  {
103  auto& st = s.stream();
104  int32_t n;
105  st >> n;
106  for(int i = 0; i < n; i++)
107  {
108  std::string key;
109  ossia::any value;
110  st >> key;
111  apply(s, key, value);
112  obj.emplace(std::move(key), std::move(value));
113  }
114  }
115 };
116 
117 template <>
118 struct SCORE_LIB_BASE_EXPORT TSerializer<JSONObject, score::any_map>
119 {
120  static void readFrom(JSONObject::Serializer& s, const score::any_map& obj)
121  {
122  s.stream.StartObject();
123  for(const auto& e : obj)
124  {
125  apply(s, e.first, e.second);
126  }
127  s.stream.EndObject();
128  }
129 
130  static void writeTo(JSONObject::Deserializer& s, score::any_map& obj)
131  {
132  for(const auto& m : s.base.GetObject())
133  {
134  const std::string str(m.name.GetString(), m.name.GetStringLength());
135  apply(s, str, obj[str]);
136  }
137  }
138 };
139 
140 template <>
141 struct is_template<score::any_map> : std::true_type
142 {
143 };
Definition: VisitorInterface.hpp:53
Definition: DataStreamVisitor.hpp:27
Definition: DataStreamVisitor.hpp:202
Definition: VisitorInterface.hpp:61
Definition: JSONVisitor.hpp:52
Definition: JSONVisitor.hpp:423
Base toolkit upon which the software is built.
Definition: Application.cpp:90
score::hash_map< std::string, std::unique_ptr< any_serializer > > & anySerializers()
The serializers for types that go in ossia::any should fit in here.
Definition: AnySerialization.cpp:7
Definition: VisitorInterface.hpp:13
Definition: AnySerialization.hpp:37
Definition: AnySerialization.hpp:25