@@ -43,37 +43,6 @@ ProtoValue::ProtoValue(ProtoValue&& other) noexcept(proto_value_details::all_mov
4343ProtoValue::ProtoValue (const ProtoValue& other)
4444 : vtable_(other.vtable_), self_(other.self_, other.vtable_) {}
4545
46- ProtoValue::ProtoValue (const Value* value) // NOLINT(misc-no-recursion)
47- : ProtoValue([](const Value& v) { // NOLINT(misc-no-recursion)
48- switch (v.kind_case ()) {
49- case Value::KindCase::kBoolValue : {
50- return ProtoValue (v.bool_value ());
51- }
52- case Value::KindCase::kStringValue : {
53- return ProtoValue (v.string_value ());
54- }
55- case Value::KindCase::kNumberValue : {
56- return ProtoValue (v.number_value ());
57- }
58- case Value::KindCase::kListValue : {
59- ProtoList vec;
60- vec.reserve (v.list_value ().values_size ());
61- for (const Value& list_val : v.list_value ().values ()) {
62- vec.push_back (ProtoValue::from_proto (list_val));
63- }
64-
65- return ProtoValue (std::move (vec));
66- }
67- case Value::KindCase::kStructValue : {
68- return ProtoValue (struct_to_map (v.struct_value ()));
69- }
70- case Value::KindCase::KIND_NOT_SET:
71- case Value::KindCase::kNullValue :
72- default :
73- return ProtoValue (nullptr );
74- }
75- }(*value)) {}
76-
7746ProtoValue& ProtoValue::operator =(ProtoValue&& other) noexcept (
7847 proto_value_details::all_moves_noexcept{}) {
7948 ProtoValue (std::move (other)).swap (*this );
@@ -98,13 +67,6 @@ void ProtoValue::swap(ProtoValue& other) noexcept(proto_value_details::all_moves
9867 std::swap (vtable_, other.vtable_ );
9968}
10069
101- template <typename Val>
102- ProtoValue ProtoValue::from_proto (const Val& v) { // NOLINT(misc-no-recursion)
103- return ProtoValue (&v);
104- }
105-
106- template ProtoValue ProtoValue::from_proto (const Value&);
107-
10870ProtoValue::Kind ProtoValue::kind () const {
10971 return vtable_.kind ();
11072}
@@ -182,8 +144,8 @@ void ProtoValue::model<T>::move(void* self, void* dest) {
182144}
183145
184146template <typename T>
185- void ProtoValue::model<T>::to_proto (void const * self, google::protobuf::Value* v) {
186- viam::sdk::to_proto (static_cast <model const *>(self)->data , v);
147+ void ProtoValue::model<T>::to_value (void const * self, google::protobuf::Value* v) {
148+ viam::sdk::proto_value_details::to_value (static_cast <model const *>(self)->data , v);
187149}
188150
189151template <typename T>
@@ -254,53 +216,94 @@ void ProtoValue::storage::destruct(const ProtoValue::vtable& vtab) noexcept {
254216 vtab.dtor (this ->get ());
255217}
256218
257- void to_proto (std::nullptr_t , Value* v) {
219+ namespace proto_value_details {
220+
221+ void to_value (std::nullptr_t , Value* v) {
258222 v->set_null_value (::google::protobuf::NULL_VALUE);
259223}
260224
261- void to_proto (bool b, Value* v) {
225+ void to_value (bool b, Value* v) {
262226 v->set_bool_value (b);
263227}
264228
265- void to_proto (double d, Value* v) {
229+ void to_value (double d, Value* v) {
266230 v->set_number_value (d);
267231}
268232
269- void to_proto (std::string s, Value* v) {
233+ void to_value (std::string s, Value* v) {
270234 v->set_string_value (std::move (s));
271235}
272236
273- void to_proto (const ProtoList& vec, Value* v) {
237+ void to_value (const ProtoList& vec, Value* v) {
274238 ::google::protobuf::ListValue l;
275239 for (const auto & val : vec) {
276- *l.add_values () = to_proto (val);
240+ *l.add_values () = v2:: to_proto (val);
277241 }
278242 *(v->mutable_list_value ()) = std::move (l);
279243}
280244
281- void to_proto (const ProtoStruct& m, Value* v) {
282- Struct s;
283- map_to_struct (m, &s);
284-
285- *(v->mutable_struct_value ()) = std::move (s);
245+ void to_value (const ProtoStruct& m, Value* v) {
246+ *(v->mutable_struct_value ()) = v2::to_proto (m);
247+ }
248+
249+ } // namespace proto_value_details
250+
251+ namespace proto_convert_details {
252+
253+ void to_proto<ProtoValue>::operator ()(const ProtoValue& self, google::protobuf::Value* v) const {
254+ self.vtable_ .to_value (self.self_ .get (), v);
255+ }
256+
257+ ProtoValue from_proto<google::protobuf::Value>::operator ()(
258+ const google::protobuf::Value* v) const { // NOLINT(misc-no-recursion)
259+ switch (v->kind_case ()) {
260+ case Value::KindCase::kBoolValue : {
261+ return ProtoValue (v->bool_value ());
262+ }
263+ case Value::KindCase::kStringValue : {
264+ return ProtoValue (v->string_value ());
265+ }
266+ case Value::KindCase::kNumberValue : {
267+ return ProtoValue (v->number_value ());
268+ }
269+ case Value::KindCase::kListValue : {
270+ ProtoList vec;
271+ vec.reserve (v->list_value ().values_size ());
272+ for (const Value& list_val : v->list_value ().values ()) {
273+ vec.push_back (v2::from_proto (list_val));
274+ }
275+
276+ return ProtoValue (std::move (vec));
277+ }
278+ case Value::KindCase::kStructValue : {
279+ return ProtoValue (v2::from_proto (v->struct_value ()));
280+ }
281+ case Value::KindCase::KIND_NOT_SET:
282+ case Value::KindCase::kNullValue :
283+ default :
284+ return ProtoValue (nullptr );
285+ }
286286}
287287
288- void to_proto (const ProtoValue& t, Value* v) {
289- t.vtable_ .to_proto (t.self_ .get (), v);
288+ void to_proto<ProtoStruct>::operator ()(const ProtoStruct& self, google::protobuf::Struct* s) const {
289+ for (const auto & kv : self) {
290+ s->mutable_fields ()->insert (
291+ google::protobuf::MapPair<std::string, Value>(kv.first , v2::to_proto (kv.second )));
292+ }
290293}
291294
292- void struct_to_map (Struct const * s, ProtoStruct& map) { // NOLINT(misc-no-recursion)
295+ ProtoStruct from_proto<google::protobuf::Struct>::operator ()(
296+ const google::protobuf::Struct* s) const { // NOLINT(misc-no-recursion)
297+ ProtoStruct result;
298+
293299 for (const auto & val : s->fields ()) {
294- map .emplace (val.first , ProtoValue ::from_proto (val.second ));
300+ result .emplace (val.first , v2 ::from_proto (val.second ));
295301 }
296- }
297302
298- void map_to_struct (const ProtoStruct& m, Struct* s) {
299- for (const auto & kv : m) {
300- s->mutable_fields ()->insert (
301- google::protobuf::MapPair<std::string, Value>(kv.first , to_proto (kv.second )));
302- }
303+ return result;
303304}
304305
306+ } // namespace proto_convert_details
307+
305308} // namespace sdk
306309} // namespace viam
0 commit comments