Skip to content

Commit 16c12f8

Browse files
committed
make to_proto and from_proto normal functions and remove v2 namespace
1 parent 7bd33dd commit 16c12f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+358
-368
lines changed

src/viam/sdk/common/private/repeated_ptr_convert.hpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,29 @@ namespace viam {
1010
namespace sdk {
1111
namespace impl {
1212

13-
struct to_repeated_field_ {
14-
template <typename T, typename = EquivalentApiType<T>>
15-
auto operator()(const std::vector<T>& v) const {
16-
::google::protobuf::RepeatedPtrField<EquivalentApiType<T>> result;
17-
result.Reserve(v.size());
13+
template <typename T, typename = EquivalentApiType<T>>
14+
auto to_repeated_field(const std::vector<T>& v) {
15+
::google::protobuf::RepeatedPtrField<EquivalentApiType<T>> result;
16+
result.Reserve(v.size());
1817

19-
for (const auto& elem : v) {
20-
*(result.Add()) = v2::to_proto(elem);
21-
}
22-
23-
return result;
18+
for (const auto& elem : v) {
19+
*(result.Add()) = to_proto(elem);
2420
}
25-
};
2621

27-
struct from_repeated_field_ {
28-
template <typename T, typename = EquivalentSdkType<T>>
29-
auto operator()(const ::google::protobuf::RepeatedPtrField<T>& v) const {
30-
std::vector<EquivalentSdkType<T>> result;
31-
result.reserve(v.size());
22+
return result;
23+
}
3224

33-
for (const auto& elem : v) {
34-
result.push_back(v2::from_proto(elem));
35-
}
25+
template <typename T, typename = EquivalentSdkType<T>>
26+
auto from_repeated_field(const ::google::protobuf::RepeatedPtrField<T>& v) {
27+
std::vector<EquivalentSdkType<T>> result;
28+
result.reserve(v.size());
3629

37-
return result;
30+
for (const auto& elem : v) {
31+
result.push_back(from_proto(elem));
3832
}
39-
};
40-
41-
namespace {
42-
43-
constexpr auto& to_repeated_field = proto_convert_details::static_const<to_repeated_field_>::value;
44-
45-
constexpr auto& from_repeated_field =
46-
proto_convert_details::static_const<from_repeated_field_>::value;
4733

48-
} // namespace
34+
return result;
35+
}
4936

5037
} // namespace impl
5138
} // namespace sdk

src/viam/sdk/common/private/service_helper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ServiceHelper : public ServiceHelperBase {
5151
}
5252

5353
auto getExtra() const {
54-
return request_->has_extra() ? v2::from_proto(request_->extra()) : ProtoStruct{};
54+
return request_->has_extra() ? from_proto(request_->extra()) : ProtoStruct{};
5555
}
5656

5757
private:

src/viam/sdk/common/proto_convert.hpp

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ namespace sdk {
1111

1212
namespace proto_convert_details {
1313

14-
// This is copied from range-v3 to allow the definition of callable object instances without
15-
// ODR/linkage issues. It is obviated in C++17 and onwards by constexpr inline.
16-
template <typename T>
17-
struct static_const {
18-
static constexpr const T value{};
19-
};
20-
21-
template <typename T>
22-
constexpr const T static_const<T>::value;
23-
2414
// This struct should be explicitly specialized with a
2515
// void operator()(const SdkType&, common::v1::ApiType*) const
2616
// to provide API/ABI insulated proto conversion
@@ -40,48 +30,30 @@ template <typename Callable>
4030
using ProtoArgType = std::remove_pointer_t<
4131
boost::mp11::mp_back<boost::callable_traits::args_t<Callable, boost::mp11::mp_list>>>;
4232

43-
// Implementation struct for the omni-to_proto callable defined below.
44-
struct to_proto_fn {
45-
template <typename SdkType>
46-
auto operator()(const SdkType& t) const {
47-
using ProtoReturnType = ProtoArgType<to_proto_impl<SdkType>>;
48-
49-
ProtoReturnType ret;
50-
to_proto_impl<SdkType>{}(t, &ret);
51-
52-
return ret;
53-
}
54-
};
55-
56-
// Implementation struct for the omni-from_proto callable defined below.
57-
struct from_proto_fn {
58-
template <typename ProtoType>
59-
auto operator()(const ProtoType& proto) const { // NOLINT(misc-no-recursion)
60-
return from_proto_impl<ProtoType>{}(&proto);
61-
}
62-
};
63-
6433
} // namespace proto_convert_details
6534

66-
namespace v2 {
67-
68-
namespace {
69-
70-
/// @brief Function object implementing conversion from an SDK type to an API type.
71-
/// This callable works for any type with a proto_convert_details::to_proto specialization as
72-
/// described above.
73-
constexpr auto& to_proto =
74-
proto_convert_details::static_const<proto_convert_details::to_proto_fn>::value;
75-
76-
/// @brief Function object implementing conversion from an API type to an SDK type.
77-
/// This callable works for any type with a proto_convert_details::from_proto specialization as
78-
/// described above.
79-
constexpr auto& from_proto =
80-
proto_convert_details::static_const<proto_convert_details::from_proto_fn>::value;
81-
82-
} // namespace
83-
84-
} // namespace v2
35+
/// @brief Convert an SDK type to its corresponding API type.
36+
/// @remark Only participates in overload resolution if to_proto_impl<SdkType> has been specialized.
37+
template <typename SdkType,
38+
typename = decltype(sizeof(proto_convert_details::to_proto_impl<SdkType>))>
39+
auto to_proto(const SdkType& t) {
40+
namespace pcd = proto_convert_details;
41+
using ProtoReturnType = pcd::ProtoArgType<pcd::to_proto_impl<SdkType>>;
42+
43+
ProtoReturnType ret;
44+
pcd::to_proto_impl<SdkType>{}(t, &ret);
45+
46+
return ret;
47+
}
48+
49+
/// @brief Convert an API type to its corresponding SDK type.
50+
/// @remark Only participates in overload resolution if from_proto_impl<ApiType> has been
51+
/// specialized.
52+
template <typename ApiType,
53+
typename = decltype(sizeof(proto_convert_details::from_proto_impl<ApiType>))>
54+
auto from_proto(const ApiType& proto) {
55+
return proto_convert_details::from_proto_impl<ApiType>{}(&proto);
56+
}
8557

8658
/// @brief Type alias for the API type corresponding to a given SDK type.
8759
/// This is the return type of calling to_proto on an instance of SdkType.

src/viam/sdk/common/proto_value.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ void to_value(std::string s, Value* v) {
237237
void to_value(const ProtoList& vec, Value* v) {
238238
::google::protobuf::ListValue l;
239239
for (const auto& val : vec) {
240-
*l.add_values() = v2::to_proto(val);
240+
*l.add_values() = to_proto(val);
241241
}
242242
*(v->mutable_list_value()) = std::move(l);
243243
}
244244

245245
void to_value(const ProtoStruct& m, Value* v) {
246-
*(v->mutable_struct_value()) = v2::to_proto(m);
246+
*(v->mutable_struct_value()) = to_proto(m);
247247
}
248248

249249
} // namespace proto_value_details
@@ -271,13 +271,13 @@ ProtoValue from_proto_impl<google::protobuf::Value>::operator()( // NOLINT(misc
271271
ProtoList vec;
272272
vec.reserve(v->list_value().values_size());
273273
for (const Value& list_val : v->list_value().values()) {
274-
vec.push_back(v2::from_proto(list_val));
274+
vec.push_back(from_proto(list_val));
275275
}
276276

277277
return ProtoValue(std::move(vec));
278278
}
279279
case Value::KindCase::kStructValue: {
280-
return ProtoValue(v2::from_proto(v->struct_value()));
280+
return ProtoValue(from_proto(v->struct_value()));
281281
}
282282
case Value::KindCase::KIND_NOT_SET:
283283
case Value::KindCase::kNullValue:
@@ -290,7 +290,7 @@ void to_proto_impl<ProtoStruct>::operator()(const ProtoStruct& self,
290290
google::protobuf::Struct* s) const {
291291
for (const auto& kv : self) {
292292
s->mutable_fields()->insert(
293-
google::protobuf::MapPair<std::string, Value>(kv.first, v2::to_proto(kv.second)));
293+
google::protobuf::MapPair<std::string, Value>(kv.first, to_proto(kv.second)));
294294
}
295295
}
296296

@@ -299,7 +299,7 @@ ProtoStruct from_proto_impl<google::protobuf::Struct>::operator()( // NOLINT(mi
299299
ProtoStruct result;
300300

301301
for (const auto& val : s->fields()) {
302-
result.emplace(val.first, v2::from_proto(val.second));
302+
result.emplace(val.first, from_proto(val.second));
303303
}
304304

305305
return result;

src/viam/sdk/common/utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ std::chrono::microseconds from_proto_impl<google::protobuf::Duration>::operator(
7676

7777
void to_proto_impl<response_metadata>::operator()(const response_metadata& self,
7878
common::v1::ResponseMetadata* proto) const {
79-
*(proto->mutable_captured_at()) = v2::to_proto(self.captured_at);
79+
*(proto->mutable_captured_at()) = to_proto(self.captured_at);
8080
}
8181

8282
response_metadata from_proto_impl<common::v1::ResponseMetadata>::operator()(
8383
const common::v1::ResponseMetadata* proto) const {
84-
return {v2::from_proto(proto->captured_at())};
84+
return {from_proto(proto->captured_at())};
8585
}
8686

8787
} // namespace proto_convert_details

src/viam/sdk/common/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <boost/optional/optional.hpp>
77

8+
#include <viam/sdk/common/proto_convert.hpp>
89
#include <viam/sdk/common/proto_value.hpp>
910
#include <viam/sdk/components/component.hpp>
1011
#include <viam/sdk/resource/resource_api.hpp>

src/viam/sdk/common/world_state.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ WorldState::geometries_in_frame from_proto_impl<common::v1::GeometriesInFrame>::
5050
void to_proto_impl<WorldState::transform>::operator()(const WorldState::transform& self,
5151
common::v1::Transform* proto) const {
5252
*(proto->mutable_reference_frame()) = self.reference_frame;
53-
*(proto->mutable_pose_in_observer_frame()) = v2::to_proto(self.pose_in_observer_frame);
53+
*(proto->mutable_pose_in_observer_frame()) = to_proto(self.pose_in_observer_frame);
5454
if (self.physical_object) {
55-
*(proto->mutable_physical_object()) = v2::to_proto(*self.physical_object);
55+
*(proto->mutable_physical_object()) = to_proto(*self.physical_object);
5656
}
5757
}
5858

5959
WorldState::transform from_proto_impl<common::v1::Transform>::operator()(
6060
const common::v1::Transform* proto) const {
6161
WorldState::transform result{proto->reference_frame(),
62-
v2::from_proto(proto->pose_in_observer_frame())};
62+
from_proto(proto->pose_in_observer_frame())};
6363
if (proto->has_physical_object()) {
64-
result.physical_object = v2::from_proto(proto->physical_object());
64+
result.physical_object = from_proto(proto->physical_object());
6565
}
6666

6767
return result;

src/viam/sdk/components/private/arm_client.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ ArmClient::ArmClient(std::string name, std::shared_ptr<grpc::Channel> channel)
1717
pose ArmClient::get_end_position(const ProtoStruct& extra) {
1818
return make_client_helper(this, *stub_, &StubType::GetEndPosition)
1919
.with(extra)
20-
.invoke([&](auto& response) { return v2::from_proto(response.pose()); });
20+
.invoke([&](auto& response) { return from_proto(response.pose()); });
2121
}
2222

2323
void ArmClient::move_to_position(const pose& pose, const ProtoStruct& extra) {
2424
return make_client_helper(this, *stub_, &StubType::MoveToPosition)
25-
.with(extra, [&](auto& request) { *request.mutable_to() = v2::to_proto(pose); })
25+
.with(extra, [&](auto& request) { *request.mutable_to() = to_proto(pose); })
2626
.invoke();
2727
}
2828

@@ -83,8 +83,8 @@ void ArmClient::stop(const ProtoStruct& extra) {
8383

8484
ProtoStruct ArmClient::do_command(const ProtoStruct& command) {
8585
return make_client_helper(this, *stub_, &StubType::DoCommand)
86-
.with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); })
87-
.invoke([](auto& response) { return v2::from_proto(response.result()); });
86+
.with([&](auto& request) { *request.mutable_command() = to_proto(command); })
87+
.invoke([](auto& response) { return from_proto(response.result()); });
8888
}
8989

9090
Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) {
@@ -109,7 +109,7 @@ Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) {
109109
std::vector<GeometryConfig> ArmClient::get_geometries(const ProtoStruct& extra) {
110110
return make_client_helper(this, *stub_, &StubType::GetGeometries)
111111
.with(extra)
112-
.invoke([](auto& response) { return v2::from_proto(response); });
112+
.invoke([](auto& response) { return from_proto(response); });
113113
}
114114

115115
} // namespace impl

src/viam/sdk/components/private/arm_server.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ::grpc::Status ArmServer::GetEndPosition(
1616
return make_service_helper<Arm>(
1717
"ArmServer::GetEndPosition", this, request)([&](auto& helper, auto& arm) {
1818
const pose p = arm->get_end_position(helper.getExtra());
19-
*response->mutable_pose() = v2::to_proto(p);
19+
*response->mutable_pose() = to_proto(p);
2020
});
2121
}
2222

@@ -26,7 +26,7 @@ ::grpc::Status ArmServer::MoveToPosition(
2626
::viam::component::arm::v1::MoveToPositionResponse*) noexcept {
2727
return make_service_helper<Arm>(
2828
"ArmServer::MoveToPosition", this, request)([&](auto& helper, auto& arm) {
29-
arm->move_to_position(v2::from_proto(request->to()), helper.getExtra());
29+
arm->move_to_position(from_proto(request->to()), helper.getExtra());
3030
});
3131
}
3232

@@ -98,8 +98,8 @@ ::grpc::Status ArmServer::DoCommand(::grpc::ServerContext*,
9898
const ::viam::common::v1::DoCommandRequest* request,
9999
::viam::common::v1::DoCommandResponse* response) noexcept {
100100
return make_service_helper<Arm>("ArmServer::DoCommand", this, request)([&](auto&, auto& arm) {
101-
const ProtoStruct result = arm->do_command(v2::from_proto(request->command()));
102-
*response->mutable_result() = v2::to_proto(result);
101+
const ProtoStruct result = arm->do_command(from_proto(request->command()));
102+
*response->mutable_result() = to_proto(result);
103103
});
104104
}
105105

@@ -143,7 +143,7 @@ ::grpc::Status ArmServer::GetGeometries(
143143
"ArmServer::GetGeometries", this, request)([&](auto& helper, auto& arm) {
144144
const std::vector<GeometryConfig> geometries = arm->get_geometries(helper.getExtra());
145145
for (const auto& geometry : geometries) {
146-
*response->mutable_geometries()->Add() = v2::to_proto(geometry);
146+
*response->mutable_geometries()->Add() = to_proto(geometry);
147147
}
148148
});
149149
}

src/viam/sdk/components/private/base_client.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void BaseClient::set_power(const Vector3& linear,
5353
return make_client_helper(this, *stub_, &StubType::SetPower)
5454
.with(extra,
5555
[&](auto& request) {
56-
*request.mutable_linear() = v2::to_proto(linear);
57-
*request.mutable_angular() = v2::to_proto(angular);
56+
*request.mutable_linear() = to_proto(linear);
57+
*request.mutable_angular() = to_proto(angular);
5858
})
5959
.invoke();
6060
}
@@ -65,8 +65,8 @@ void BaseClient::set_velocity(const Vector3& linear,
6565
return make_client_helper(this, *stub_, &StubType::SetVelocity)
6666
.with(extra,
6767
[&](auto& request) {
68-
*request.mutable_linear() = v2::to_proto(linear);
69-
*request.mutable_angular() = v2::to_proto(angular);
68+
*request.mutable_linear() = to_proto(linear);
69+
*request.mutable_angular() = to_proto(angular);
7070
})
7171
.invoke();
7272
}
@@ -84,7 +84,7 @@ bool BaseClient::is_moving() {
8484
std::vector<GeometryConfig> BaseClient::get_geometries(const ProtoStruct& extra) {
8585
return make_client_helper(this, *stub_, &StubType::GetGeometries)
8686
.with(extra)
87-
.invoke([](auto& response) { return v2::from_proto(response); });
87+
.invoke([](auto& response) { return from_proto(response); });
8888
}
8989

9090
Base::properties BaseClient::get_properties(const ProtoStruct& extra) {
@@ -99,8 +99,8 @@ Base::properties BaseClient::get_properties(const ProtoStruct& extra) {
9999

100100
ProtoStruct BaseClient::do_command(const ProtoStruct& command) {
101101
return make_client_helper(this, *stub_, &StubType::DoCommand)
102-
.with([&](auto& request) { *request.mutable_command() = v2::to_proto(command); })
103-
.invoke([](auto& response) { return v2::from_proto(response.result()); });
102+
.with([&](auto& request) { *request.mutable_command() = to_proto(command); })
103+
.invoke([](auto& response) { return from_proto(response.result()); });
104104
}
105105

106106
} // namespace impl

0 commit comments

Comments
 (0)