-
Notifications
You must be signed in to change notification settings - Fork 182
/
packed_variant.hpp
219 lines (183 loc) · 6.17 KB
/
packed_variant.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (C) 2020 The ESPResSo project
*
* This file is part of ESPResSo.
*
* ESPResSo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ESPResSo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SCRIPT_INTERFACE_PACKED_VARIANT_HPP
#define SCRIPT_INTERFACE_PACKED_VARIANT_HPP
#include "Variant.hpp"
#include <cstddef>
#include <functional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
namespace ScriptInterface {
using ObjectId = std::size_t;
/**
* @brief Id for object.
*
* This assigns every ObjectHandle a unique id.
*/
inline ObjectId object_id(const ObjectHandle *p) {
// NOLINTNEXTLINE(bugprone-sizeof-expression)
static_assert(sizeof(const ObjectHandle *) <= sizeof(ObjectId), "");
// Use the pointer value as the unique identifier.
// This function is only called on the head node.
return reinterpret_cast<ObjectId>(p);
}
/**
* @brief Packed version of @ref Variant.
*
* When packing variants by @ref PackVisitor, objects of type
* @ref ObjectRef are packed as @ref ObjectId. Other than that,
* all other types allowed in @ref Variant must appear here.
*/
using PackedVariant = boost::make_recursive_variant<
None, bool, int, double, std::string, std::vector<int>, std::vector<double>,
ObjectId, std::vector<boost::recursive_variant_>, Utils::Vector2d,
Utils::Vector3d, Utils::Vector4d,
std::unordered_map<int, boost::recursive_variant_>>::type;
using PackedMap = std::vector<std::pair<std::string, PackedVariant>>;
/**
* @brief Visitor that converts a Variant to a PackedVariant.
*
* While packing, keeps track of all the ObjectRef values that
* were encountered and stores them. This also keeps the
* referees alive if there are no other owners.
*/
struct PackVisitor : boost::static_visitor<PackedVariant> {
private:
mutable std::unordered_map<ObjectId, ObjectRef> m_objects;
public:
/** @brief Map of objects whose references were replaced by ids. */
auto const &objects() const { return m_objects; }
/* For the vector, we recurse into each element. */
auto operator()(const std::vector<Variant> &vec) const {
std::vector<PackedVariant> ret(vec.size());
boost::transform(vec, ret.begin(), [this](const Variant &v) {
return boost::apply_visitor(*this, v);
});
return ret;
}
/* For the map, we recurse into each element. */
auto operator()(const std::unordered_map<int, Variant> &map) const {
std::unordered_map<int, PackedVariant> ret{};
for (auto const &it : map) {
ret.insert({it.first, boost::apply_visitor(*this, it.second)});
}
return ret;
}
/* For object references we store the object reference, and
* replace it by just an id. */
PackedVariant operator()(const ObjectRef &so_ptr) const {
auto const oid = object_id(so_ptr.get());
m_objects[oid] = so_ptr;
return oid;
}
/* Regular value are just verbatim copied into the result. */
template <class T> PackedVariant operator()(T &&val) const {
return std::forward<T>(val);
}
};
/**
* @brief Visitor that converts a PackedVariant to a Variant.
*
* ObjectId are replaced according to the provided object map.
*/
struct UnpackVisitor : boost::static_visitor<Variant> {
std::unordered_map<ObjectId, ObjectRef> const &objects;
explicit UnpackVisitor(std::unordered_map<ObjectId, ObjectRef> const &objects)
: objects(objects) {}
/* For the vector, we recurse into each element. */
auto operator()(const std::vector<PackedVariant> &vec) const {
std::vector<Variant> ret(vec.size());
boost::transform(vec, ret.begin(), [this](const PackedVariant &v) {
return boost::apply_visitor(*this, v);
});
return ret;
}
/* For the map, we recurse into each element. */
auto operator()(const std::unordered_map<int, PackedVariant> &map) const {
std::unordered_map<int, Variant> ret{};
for (auto const &it : map) {
ret.insert({it.first, boost::apply_visitor(*this, it.second)});
}
return ret;
}
/* Regular value are just verbatim copied into the result. */
template <class T> Variant operator()(T &&val) const {
return std::forward<T>(val);
}
/* For object id's they are replaced by references according to the map. */
Variant operator()(const ObjectId &id) const { return objects.at(id); }
};
/**
* @brief Transform a Variant to a PackedVariant
*
* Applies @ref PackVisitor to a @ref Variant.
*
* @param v Input Variant
* @return Packed variant.
*/
inline PackedVariant pack(const Variant &v) {
return boost::apply_visitor(PackVisitor{}, v);
}
/**
* @brief Unpack a PackedVariant.
*
* Applies @ref UnpackVisitor to a @ref Variant.
*
* @param v Packed Variant.
* @param objects Map of ids to reference.
* @return Transformed variant.
*/
inline Variant unpack(const PackedVariant &v,
std::unordered_map<ObjectId, ObjectRef> const &objects) {
return boost::apply_visitor(UnpackVisitor{objects}, v);
}
/**
* @brief Pack a VariantMap.
*
* Applies @ref pack to every value in the
* input map.
*/
inline PackedMap pack(const VariantMap &v) {
PackedMap ret(v.size());
boost::transform(v, ret.begin(), [](auto const &kv) {
return std::pair<std::string, PackedVariant>{kv.first, pack(kv.second)};
});
return ret;
}
/**
* @brief Unpack a PackedMap.
*
* Applies @ref unpack to every value in the
* input map.
*/
inline VariantMap
unpack(const PackedMap &v,
std::unordered_map<ObjectId, ObjectRef> const &objects) {
VariantMap ret;
boost::transform(
v, std::inserter(ret, ret.end()),
[&objects](auto const &kv) -> std::pair<std::string, Variant> {
return {kv.first, unpack(kv.second, objects)};
});
return ret;
}
} // namespace ScriptInterface
#endif