Loading...
Searching...
No Matches
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 if(!s.base.IsArray())
63 return;
64
65 const auto& arr = s.base.GetArray();
66 for(const auto& elt : arr)
67 {
68 if(!elt.IsArray())
69 continue;
70 const auto& pair = elt.GetArray();
71 if(pair.Size() < 2)
72 continue;
73 typename Map_T::key_type key;
74 typename Map_T::mapped_type value;
75 key <<= JsonValue{pair[0]};
76 value <<= JsonValue{pair[1]};
77 obj.emplace(std::move(key), std::move(value));
78 }
79 }
80};
81
82template <typename T, typename U, typename H, typename E, typename A>
83struct TSerializer<DataStream, std::unordered_map<T, U, H, E, A>> : MapSerializer
84{
85};
86template <typename T, typename U, typename H, typename E, typename A>
87struct TSerializer<JSONObject, std::unordered_map<T, U, H, E, A>> : MapSerializer
88{
89};
90
91#if !defined(OSSIA_NO_FAST_CONTAINERS)
92template <
93 class Key, class T, class Hash, class KeyEqual, class AllocatorOrContainer,
94 class Bucket, class BC, bool IsSegmented>
96 DataStream, ankerl::unordered_dense::detail::table<
97 Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BC, IsSegmented>>
99{
100};
101
102template <
103 class Key, class T, class Hash, class KeyEqual, class AllocatorOrContainer,
104 class Bucket, class BC, bool IsSegmented>
106 JSONObject, ankerl::unordered_dense::detail::table<
107 Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, BC, IsSegmented>>
109{
110};
111#endif
112
113template <typename T, typename U>
114struct TSerializer<DataStream, boost::container::dtl::pair<T, U>>
115{
116 using type = boost::container::dtl::pair<T, U>;
117 static void readFrom(DataStream::Serializer& s, const type& obj)
118 {
119 s.m_stream << obj.first << obj.second;
120 }
121
122 static void writeTo(DataStream::Deserializer& s, type& obj)
123 {
124 s.m_stream >> obj.first >> obj.second;
125 }
126};
127
128template <typename T, typename U>
129struct TSerializer<JSONObject, boost::container::dtl::pair<T, U>>
130{
131 using type = boost::container::dtl::pair<T, U>;
132 static void readFrom(JSONObject::Serializer& s, const type& obj)
133 {
134 s.stream.StartArray();
135 s.readFrom(obj.first);
136 s.readFrom(obj.second);
137 s.stream.EndArray();
138 }
139
140 static void writeTo(JSONObject::Deserializer& s, type& obj)
141 {
142 const auto& arr = s.base.GetArray();
143 obj.first <<= JsonValue{arr[0]};
144 obj.second <<= JsonValue{arr[1]};
145 }
146};
147template <typename T, typename U>
148struct TSerializer<DataStream, ossia::flat_map<T, U>>
149{
150 using type = ossia::flat_map<T, U>;
151 using pair_type = typename type::value_type;
152 static void readFrom(DataStream::Serializer& s, const type& obj)
153 {
154 s.m_stream << obj.tree().get_sequence_cref();
155 }
156
157 static void writeTo(DataStream::Deserializer& s, type& obj)
158 {
159 s.m_stream >> obj.tree().get_sequence_ref();
160 }
161};
162
163template <typename T, typename U>
164struct TSerializer<JSONObject, ossia::flat_map<T, U>>
165{
166 using type = ossia::flat_map<T, U>;
167 static void readFrom(JSONObject::Serializer& s, const type& obj)
168 {
169 ArraySerializer::readFrom(s, obj.tree().get_sequence_cref());
170 }
171
172 static void writeTo(JSONObject::Deserializer& s, type& obj)
173 {
174 ArraySerializer::writeTo(s, obj.tree().get_sequence_ref());
175 }
176};
177
178template <typename T, typename U>
179struct TSerializer<DataStream, boost::container::flat_map<T, U>> : MapSerializer
180{
181};
182
183template <typename T, typename U>
184struct TSerializer<JSONObject, boost::container::flat_map<T, U>> : MapSerializer
185{
186};
Definition VisitorInterface.hpp:53
Definition DataStreamVisitor.hpp:27
Definition DataStreamVisitor.hpp:202
Definition VisitorInterface.hpp:61
Definition JSONVisitor.hpp:52
Definition JSONVisitor.hpp:423
STL namespace.
Definition JSONVisitor.hpp:372
Definition MapSerialization.hpp:14
Definition VisitorInterface.hpp:13