forked from viamrobotics/viam-cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_value.cpp
299 lines (238 loc) · 9.78 KB
/
proto_value.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <viam/sdk/common/proto_value.hpp>
#include <google/protobuf/struct.pb.h>
namespace viam {
namespace sdk {
using google::protobuf::Struct;
using google::protobuf::Value;
ProtoValue::ProtoValue() noexcept : ProtoValue(nullptr, nullptr) {}
ProtoValue::ProtoValue(std::nullptr_t) noexcept : ProtoValue() {}
ProtoValue::ProtoValue(bool b) noexcept : ProtoValue(b, nullptr) {}
ProtoValue::ProtoValue(int i) noexcept : ProtoValue(static_cast<double>(i)) {}
ProtoValue::ProtoValue(double d) noexcept : ProtoValue(d, nullptr) {}
ProtoValue::ProtoValue(std::string s) noexcept : ProtoValue(std::move(s), nullptr) {}
template <typename Val, typename>
ProtoValue::ProtoValue(std::vector<Val> v) noexcept(
std::is_nothrow_move_constructible<std::vector<Val>>{})
: ProtoValue(std::move(v), 0) {}
template <typename Val, typename>
ProtoValue::ProtoValue(std::unordered_map<std::string, Val> s) noexcept(
std::is_nothrow_move_constructible<std::unordered_map<std::string, Val>>{})
: ProtoValue(std::move(s), 0) {}
template <typename T>
ProtoValue::ProtoValue(T t, std::nullptr_t) noexcept(std::is_nothrow_move_constructible<T>{})
: vtable_{model<T>::vtable_}, self_{std::move(t)} {}
// -- explicit instantiations of by-value constructors -- //
template ProtoValue::ProtoValue(ProtoList) noexcept(
std::is_nothrow_move_constructible<ProtoList>{});
template ProtoValue::ProtoValue(ProtoStruct m) noexcept(
std::is_nothrow_move_constructible<ProtoStruct>{});
ProtoValue::ProtoValue(const char* str) : ProtoValue(std::string(str)) {}
ProtoValue::ProtoValue(ProtoValue&& other) noexcept(proto_value_details::all_moves_noexcept{})
: vtable_(std::move(other.vtable_)), self_(std::move(other.self_), vtable_) {}
ProtoValue::ProtoValue(const ProtoValue& other)
: vtable_(other.vtable_), self_(other.self_, other.vtable_) {}
ProtoValue& ProtoValue::operator=(ProtoValue&& other) noexcept(
proto_value_details::all_moves_noexcept{}) {
ProtoValue(std::move(other)).swap(*this);
return *this;
}
ProtoValue& ProtoValue::operator=(const ProtoValue& other) {
ProtoValue(other).swap(*this);
return *this;
}
ProtoValue::~ProtoValue() {
self_.destruct(vtable_);
}
bool operator==(const ProtoValue& lhs, const ProtoValue& rhs) {
return lhs.vtable_.equal_to(lhs.self_.get(), rhs.self_.get(), rhs.vtable_);
}
void ProtoValue::swap(ProtoValue& other) noexcept(proto_value_details::all_moves_noexcept{}) {
self_.swap(vtable_, other.self_, other.vtable_);
std::swap(vtable_, other.vtable_);
}
ProtoValue::Kind ProtoValue::kind() const {
return vtable_.kind();
}
bool ProtoValue::is_null() const {
return kind() == Kind::k_null;
}
template <typename T>
T& ProtoValue::get_unchecked() & {
assert(this->is_a<T>());
return *(this->self_.template get<T>());
}
template bool& ProtoValue::get_unchecked<bool>() &;
template double& ProtoValue::get_unchecked<double>() &;
template std::string& ProtoValue::get_unchecked<std::string>() &;
template ProtoList& ProtoValue::get_unchecked<ProtoList>() &;
template ProtoStruct& ProtoValue::get_unchecked<ProtoStruct>() &;
template <typename T>
const T& ProtoValue::get_unchecked() const& {
assert(this->is_a<T>());
return *(this->self_.template get<T>());
}
template const bool& ProtoValue::get_unchecked<bool>() const&;
template const double& ProtoValue::get_unchecked<double>() const&;
template std::string const& ProtoValue::get_unchecked<std::string>() const&;
template ProtoList const& ProtoValue::get_unchecked<ProtoList>() const&;
template ProtoStruct const& ProtoValue::get_unchecked<ProtoStruct>() const&;
template <typename T>
T&& ProtoValue::get_unchecked() && {
assert(this->is_a<T>());
return std::move(*(this->self_.template get<T>()));
}
template bool&& ProtoValue::get_unchecked<bool>() &&;
template double&& ProtoValue::get_unchecked<double>() &&;
template std::string&& ProtoValue::get_unchecked<std::string>() &&;
template ProtoList&& ProtoValue::get_unchecked<ProtoList>() &&;
template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;
// --- ProtoT::model<T> definitions --- //
template <typename T>
ProtoValue::model<T>::model(T t) noexcept(std::is_nothrow_move_constructible<T>{})
: data(std::move(t)) {}
template <typename T>
void ProtoValue::model<T>::dtor(void* self) noexcept {
static_cast<model*>(self)->~model();
}
template <typename T>
void ProtoValue::model<T>::copy(void const* self, void* dest) {
new (dest) model(*static_cast<model const*>(self));
}
template <typename T>
void ProtoValue::model<T>::move(void* self, void* dest) {
new (dest) model(std::move(*static_cast<model*>(self)));
}
template <typename T>
void ProtoValue::model<T>::to_value(void const* self, google::protobuf::Value* v) {
viam::sdk::proto_value_details::to_value(static_cast<model const*>(self)->data, v);
}
template <typename T>
ProtoValue::Kind ProtoValue::model<T>::kind() noexcept {
return proto_value_details::kind<T>::type::value;
}
template <typename T>
bool ProtoValue::model<T>::equal_to(void const* self,
void const* other,
const ProtoValue::vtable& other_vtable) {
if (model::kind() != other_vtable.kind()) {
return false;
}
return *static_cast<T const*>(self) == *static_cast<T const*>(other);
}
// --- ProtoT::storage definitions --- //
template <typename T>
ProtoValue::storage::storage(T t) noexcept(std::is_nothrow_move_constructible<T>{}) {
static_assert(sizeof(T) <= local_storage_size, "Type too large to fit in local storage");
static_assert(alignof(T) <= local_storage_alignment,
"Type alignment too strict for local storage");
static_assert(sizeof(std::vector<void*>) == sizeof(ProtoList),
"vector<ProtoValue> stand-in size mismatch");
static_assert(alignof(std::vector<void*>) == alignof(ProtoList),
"vector<ProtoValue> stand-in alignment mismatch");
static_assert(sizeof(std::unordered_map<std::string, void*>) == sizeof(ProtoStruct),
"ProtoStruct stand-in size mismatch");
static_assert(alignof(std::unordered_map<std::string, void*>) == alignof(ProtoStruct),
"ProtoStruct stand-in alignment mismatch");
new (&buf_) model<T>(std::move(t));
}
ProtoValue::storage::storage(const ProtoValue::storage& other, const ProtoValue::vtable& vtab) {
vtab.copy(other.get(), this->get());
}
ProtoValue::storage::storage(ProtoValue::storage&& other, const ProtoValue::vtable& vtab) noexcept(
proto_value_details::all_moves_noexcept{}) {
vtab.move(other.get(), this->get());
}
void ProtoValue::storage::swap(
const ProtoValue::vtable& this_vtable,
ProtoValue::storage& other,
const ProtoValue::vtable& other_vtable) noexcept(proto_value_details::all_moves_noexcept{}) {
if (this == &other) {
return;
}
BufType tmp;
other_vtable.move(other.get(), &tmp);
other_vtable.dtor(other.get());
this_vtable.move(this->get(), other.get());
this_vtable.dtor(this->get());
other_vtable.move(&tmp, this->get());
other_vtable.dtor(&tmp);
}
void ProtoValue::storage::destruct(const ProtoValue::vtable& vtab) noexcept {
vtab.dtor(this->get());
}
namespace proto_value_details {
void to_value(std::nullptr_t, Value* v) {
v->set_null_value(::google::protobuf::NULL_VALUE);
}
void to_value(bool b, Value* v) {
v->set_bool_value(b);
}
void to_value(double d, Value* v) {
v->set_number_value(d);
}
void to_value(std::string s, Value* v) {
v->set_string_value(std::move(s));
}
void to_value(const ProtoList& vec, Value* v) {
::google::protobuf::ListValue l;
for (const auto& val : vec) {
*l.add_values() = to_proto(val);
}
*(v->mutable_list_value()) = std::move(l);
}
void to_value(const ProtoStruct& m, Value* v) {
*(v->mutable_struct_value()) = to_proto(m);
}
} // namespace proto_value_details
namespace proto_convert_details {
void to_proto_impl<ProtoValue>::operator()(const ProtoValue& self,
google::protobuf::Value* v) const {
self.vtable_.to_value(self.self_.get(), v);
}
ProtoValue from_proto_impl<google::protobuf::Value>::operator()( // NOLINT(misc-no-recursion)
const google::protobuf::Value* v) const {
switch (v->kind_case()) {
case Value::KindCase::kBoolValue: {
return ProtoValue(v->bool_value());
}
case Value::KindCase::kStringValue: {
return ProtoValue(v->string_value());
}
case Value::KindCase::kNumberValue: {
return ProtoValue(v->number_value());
}
case Value::KindCase::kListValue: {
ProtoList vec;
vec.reserve(v->list_value().values_size());
for (const Value& list_val : v->list_value().values()) {
vec.push_back(from_proto(list_val));
}
return ProtoValue(std::move(vec));
}
case Value::KindCase::kStructValue: {
return ProtoValue(from_proto(v->struct_value()));
}
case Value::KindCase::KIND_NOT_SET:
case Value::KindCase::kNullValue:
default:
return ProtoValue(nullptr);
}
}
void to_proto_impl<ProtoStruct>::operator()(const ProtoStruct& self,
google::protobuf::Struct* s) const {
for (const auto& kv : self) {
s->mutable_fields()->insert(
google::protobuf::MapPair<std::string, Value>(kv.first, to_proto(kv.second)));
}
}
ProtoStruct from_proto_impl<google::protobuf::Struct>::operator()( // NOLINT(misc-no-recursion)
const google::protobuf::Struct* s) const {
ProtoStruct result;
for (const auto& val : s->fields()) {
result.emplace(val.first, from_proto(val.second));
}
return result;
}
} // namespace proto_convert_details
} // namespace sdk
} // namespace viam