MapSerialization.hpp
1 #pragma once
3 #include <score/serialization/JSONValueVisitor.hpp>
4 #include <score/serialization/JSONVisitor.hpp>
5 
6 #include <ossia/detail/flat_map.hpp>
7 #include <ossia/detail/hash_map.hpp>
8 
9 #include <boost/container/flat_map.hpp>
10 
11 #include <unordered_map>
12 
14 {
15  template <typename Map_T>
16  static void readFrom(DataStream::Serializer& s, const Map_T& obj)
17  {
18  auto& st = s.stream();
19  st << (int32_t)obj.size();
20  for(const auto& e : obj)
21  {
22  st << e.first << e.second;
23  }
24  }
25 
26  template <typename Map_T>
27  static void writeTo(DataStream::Deserializer& s, Map_T& obj)
28  {
29  obj.clear();
30 
31  auto& st = s.stream();
32  int32_t n;
33  st >> n;
34  for(int32_t i = 0; i < n; i++)
35  {
36  typename Map_T::key_type key;
37  typename Map_T::mapped_type value;
38  st >> key >> value;
39  obj.emplace(std::move(key), std::move(value));
40  }
41  }
42 
43  template <typename Map_T>
44  static void readFrom(JSONObject::Serializer& s, const Map_T& obj)
45  {
46  s.stream.StartArray();
47  for(const auto& pair : obj)
48  {
49  s.stream.StartArray();
50  s.readFrom(pair.first);
51  s.readFrom(pair.second);
52  s.stream.EndArray();
53  }
54  s.stream.EndArray();
55  }
56 
57  template <typename Map_T>
58  static void writeTo(JSONObject::Deserializer& s, Map_T& obj)
59  {
60  obj.clear();
61 
62  const auto& arr = s.base.GetArray();
63  for(const auto& elt : arr)
64  {
65  const auto& pair = elt.GetArray();
66  typename Map_T::key_type key;
67  typename Map_T::mapped_type value;
68  key <<= JsonValue{pair[0]};
69  value <<= JsonValue{pair[1]};
70  obj.emplace(std::move(key), std::move(value));
71  }
72  }
73 };
74 
75 template <typename T, typename U, typename H, typename E, typename A>
76 struct TSerializer<DataStream, std::unordered_map<T, U, H, E, A>> : MapSerializer
77 {
78 };
79 template <typename T, typename U, typename H, typename E, typename A>
80 struct TSerializer<JSONObject, std::unordered_map<T, U, H, E, A>> : MapSerializer
81 {
82 };
83 
84 #if !defined(OSSIA_NO_FAST_CONTAINERS)
85 template <
86  class Key, class T, class Hash, class KeyEqual, class AllocatorOrContainer,
87  class Bucket, bool IsSegmented>
88 struct TSerializer<
89  DataStream, ankerl::unordered_dense::detail::table<
90  Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, IsSegmented>>
92 {
93 };
94 
95 template <
96  class Key, class T, class Hash, class KeyEqual, class AllocatorOrContainer,
97  class Bucket, bool IsSegmented>
98 struct TSerializer<
99  JSONObject, ankerl::unordered_dense::detail::table<
100  Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, IsSegmented>>
101  : MapSerializer
102 {
103 };
104 #endif
105 
106 template <typename T, typename U>
107 struct TSerializer<DataStream, boost::container::dtl::pair<T, U>>
108 {
109  using type = boost::container::dtl::pair<T, U>;
110  static void readFrom(DataStream::Serializer& s, const type& obj)
111  {
112  s.m_stream << obj.first << obj.second;
113  }
114 
115  static void writeTo(DataStream::Deserializer& s, type& obj)
116  {
117  s.m_stream >> obj.first >> obj.second;
118  }
119 };
120 
121 template <typename T, typename U>
122 struct TSerializer<JSONObject, boost::container::dtl::pair<T, U>>
123 {
124  using type = boost::container::dtl::pair<T, U>;
125  static void readFrom(JSONObject::Serializer& s, const type& obj)
126  {
127  s.stream.StartArray();
128  s.readFrom(obj.first);
129  s.readFrom(obj.second);
130  s.stream.EndArray();
131  }
132 
133  static void writeTo(JSONObject::Deserializer& s, type& obj)
134  {
135  const auto& arr = s.base.GetArray();
136  obj.first <<= JsonValue{arr[0]};
137  obj.second <<= JsonValue{arr[1]};
138  }
139 };
140 template <typename T, typename U>
141 struct TSerializer<DataStream, ossia::flat_map<T, U>>
142 {
143  using type = ossia::flat_map<T, U>;
144  using pair_type = typename type::value_type;
145  static void readFrom(DataStream::Serializer& s, const type& obj)
146  {
147  s.m_stream << obj.tree().get_sequence_cref();
148  }
149 
150  static void writeTo(DataStream::Deserializer& s, type& obj)
151  {
152  s.m_stream >> obj.tree().get_sequence_ref();
153  }
154 };
155 
156 template <typename T, typename U>
157 struct TSerializer<JSONObject, ossia::flat_map<T, U>>
158 {
159  using type = ossia::flat_map<T, U>;
160  static void readFrom(JSONObject::Serializer& s, const type& obj)
161  {
162  ArraySerializer::readFrom(s, obj.tree().get_sequence_cref());
163  }
164 
165  static void writeTo(JSONObject::Deserializer& s, type& obj)
166  {
167  ArraySerializer::writeTo(s, obj.tree().get_sequence_ref());
168  }
169 };
170 
171 template <typename T, typename U>
172 struct TSerializer<DataStream, boost::container::flat_map<T, U>> : MapSerializer
173 {
174 };
175 
176 template <typename T, typename U>
177 struct TSerializer<JSONObject, boost::container::flat_map<T, U>> : MapSerializer
178 {
179 };
Definition: VisitorInterface.hpp:53
Definition: DataStreamVisitor.hpp:27
Definition: DataStreamVisitor.hpp:202
Definition: VisitorInterface.hpp:61
Definition: JSONVisitor.hpp:52
Definition: JSONVisitor.hpp:423
Definition: JSONVisitor.hpp:372
Definition: MapSerialization.hpp:14
Definition: VisitorInterface.hpp:13