Skip to content

Commit d36b9ea

Browse files
committed
instead of constructor + operator, weird not-quite-sfinae approach to ptr <-> vec conversions
1 parent 3f7c6c7 commit d36b9ea

File tree

7 files changed

+94
-53
lines changed

7 files changed

+94
-53
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// @file common/proto_utils.hpp
2+
///
3+
/// @brief Utils that require generated proto includes. These should be #included
4+
/// in cpp implementation files, but not in wrapper headers consumed by third party code.
5+
#pragma once
6+
7+
#include <viam/api/common/v1/common.pb.h>
8+
9+
namespace viam {
10+
namespace sdk {
11+
12+
/// @brief Copies elements from a protobuf repeated pointer array into a std::vector. Src type
13+
/// must be implicitly convertible to Dst (probably via operator on Src).
14+
template <typename Src, typename Dst>
15+
void vecToRepeatedPtr(const std::vector<Src>& vec, google::protobuf::RepeatedPtrField<Dst>& dest) {
16+
dest.Reserve(vec.size());
17+
for (auto& x : vec) {
18+
*dest.Add() = x.to_proto();
19+
}
20+
}
21+
22+
/// @brief Non-member to_proto() version. (necessary for moving generated types out of wrapper headers).
23+
/// Template param F is a function that converts from Src to Dst.
24+
template <typename Src, typename Dst>
25+
void vecToRepeatedPtr(const std::vector<Src>& vec, google::protobuf::RepeatedPtrField<Dst>& dest, Dst& from_proto(const Src&)) {
26+
dest.Reserve(vec.size());
27+
for (auto& x : vec) {
28+
*dest.Add() = from_proto(x);
29+
}
30+
}
31+
32+
/// @brief Copies elements from a std::vector into a protobuf repeated pointer array. Src type
33+
/// must be implicitly convertible to Dst (probably via constructor on Dst).
34+
template <typename Src, typename Dst>
35+
void repeatedPtrToVec(const google::protobuf::RepeatedPtrField<Src>& src, std::vector<Dst>& vec) {
36+
vec.reserve(src.size());
37+
for (auto& x : src) {
38+
vec.push_back(Dst::from_proto(x));
39+
}
40+
}
41+
42+
/// @brief Non-member to_proto() version. (necessary for moving generated types out of wrapper headers).
43+
/// Template param F is a function that converts from Src to Dst.
44+
template <typename Src, typename Dst>
45+
void repeatedPtrToVec(const google::protobuf::RepeatedPtrField<Src>& src, std::vector<Dst>& vec, Dst& to_proto(const Src&)) {
46+
vec.reserve(src.size());
47+
for (auto& x : src) {
48+
vec.push_back(to_proto(x));
49+
}
50+
}
51+
52+
} // namespace sdk
53+
} // namespace viam

src/viam/sdk/common/utils.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,5 @@ bool from_dm_from_extra(const ProtoStruct& extra) {
184184
return false;
185185
}
186186

187-
template <typename Src, typename Dst>
188-
void vecToRepeatedPtr(const std::vector<Src>& vec, google::protobuf::RepeatedPtrField<Dst>& dest) {
189-
dest->Reserve(vec.size());
190-
for (auto& x : vec) {
191-
*dest.Add() = x;
192-
}
193-
}
194-
195-
template <typename Src, typename Dst>
196-
void repeatedPtrToVec(const google::protobuf::RepeatedPtrField<Src>& src, std::vector<Dst>& vec) {
197-
vec.reserve(src.size());
198-
for (auto& x : src) {
199-
vec.push_back(x);
200-
}
201-
}
202-
203187
} // namespace sdk
204188
} // namespace viam

src/viam/sdk/common/utils.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,9 @@ void set_logger_severity_from_args(int argc, char** argv);
9999
/// @param extra The extra ProtoStruct.
100100
bool from_dm_from_extra(const ProtoStruct& extra);
101101

102-
/// @brief Copies elements from a protobuf repeated pointer array into a std::vector. Src type
103-
/// must be implicitly convertible to Dst (probably via operator on Src).
104-
template <typename Src, typename Dst>
105-
void vecToRepeatedPtr(const std::vector<Src>& vec, google::protobuf::RepeatedPtrField<Dst>& dest);
106-
107-
/// @brief Copies elements from a std::vector into a protobuf repeated pointer array. Src type
108-
/// must be implicitly convertible to Dst (probably via constructor on Dst).
109-
template <typename Src, typename Dst>
110-
void repeatedPtrToVec(const google::protobuf::RepeatedPtrField<Src>& src, std::vector<Dst>& vec);
102+
template <typename Src, typename Dst> Dst auto_from_proto(const Src& proto);
103+
104+
template <typename Src, typename Dst> Dst auto_to_proto(const Src& wrapped);
111105

112106
} // namespace sdk
113107
} // namespace viam

src/viam/sdk/services/navigation.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include <viam/sdk/services/navigation.hpp>
22

33
#include <viam/sdk/common/utils.hpp>
4+
#include <viam/sdk/common/proto_utils.hpp>
45

56
namespace viam {
67
namespace sdk {
78

8-
Navigation::Navigation(std::string name) : Service(std::move(name)){};
9+
Navigation::Navigation(std::string name) : Service(std::move(name)) {};
910

1011
API Navigation::api() const {
1112
return API::get<Navigation>();
@@ -15,5 +16,33 @@ API API::traits<Navigation>::api() {
1516
return {kRDK, kService, "navigation"};
1617
}
1718

19+
template <>
20+
Navigation::Waypoint auto_from_proto(const viam::service::navigation::v1::Waypoint& proto) {
21+
return Navigation::Waypoint{proto.id(), geo_point::from_proto(proto.location())};
22+
}
23+
24+
template <>
25+
viam::service::navigation::v1::Waypoint auto_to_proto(const Navigation::Waypoint& wp) {
26+
viam::service::navigation::v1::Waypoint ret;
27+
*ret.mutable_id() = wp.id;
28+
*ret.mutable_location() = wp.location.to_proto();
29+
return ret;
30+
}
31+
32+
template <>
33+
Navigation::Path auto_from_proto(const viam::service::navigation::v1::Path& proto) {
34+
Navigation::Path ret{proto.destination_waypoint_id()};
35+
repeatedPtrToVec(proto.geopoints(), ret.geopoints);
36+
return ret;
37+
}
38+
39+
template <>
40+
viam::service::navigation::v1::Path auto_to_proto(const Navigation::Path& p) {
41+
viam::service::navigation::v1::Path ret;
42+
*ret.mutable_destination_waypoint_id() = p.destination_waypoint_id;
43+
vecToRepeatedPtr(p.geopoints, *ret.mutable_geopoints());
44+
return ret;
45+
}
46+
1847
} // namespace sdk
1948
} // namespace viam

src/viam/sdk/services/navigation.hpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,11 @@ class Navigation : public Service {
3737
};
3838

3939
struct Waypoint {
40-
Waypoint(const viam::service::navigation::v1::Waypoint& proto)
41-
: id(proto.id()), location(geo_point::from_proto(proto.location())) {}
42-
43-
operator viam::service::navigation::v1::Waypoint() {
44-
viam::service::navigation::v1::Waypoint ret;
45-
*ret.mutable_id() = id;
46-
*ret.mutable_location() = location.to_proto();
47-
return ret;
48-
}
49-
5040
std::string id;
5141
geo_point location;
5242
};
5343

5444
struct Path {
55-
Path(const viam::service::navigation::v1::Path& proto)
56-
: destination_waypoint_id(proto.destination_waypoint_id()) {
57-
repeatedPtrToVec(proto.geopoints(), geopoints);
58-
}
59-
60-
operator viam::service::navigation::v1::Path() {
61-
viam::service::navigation::v1::Path ret;
62-
*ret.mutable_destination_waypoint_id() = destination_waypoint_id;
63-
vecToRepeatedPtr(geopoints, *ret.mutable_geopoints());
64-
return ret;
65-
}
66-
6745
std::string destination_waypoint_id;
6846
std::vector<geo_point> geopoints;
6947
};

src/viam/sdk/services/private/navigation_client.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <viam/sdk/common/client_helper.hpp>
1111
#include <viam/sdk/common/proto_value.hpp>
1212
#include <viam/sdk/common/utils.hpp>
13+
#include <viam/sdk/common/proto_utils.hpp>
1314
#include <viam/sdk/services/navigation.hpp>
1415

1516
namespace viam {
@@ -68,7 +69,7 @@ std::unique_ptr<std::vector<Navigation::Waypoint>> NavigationClient::get_waypoin
6869
})
6970
.invoke([](auto& response) {
7071
auto ret = std::make_unique<std::vector<Navigation::Waypoint>>();
71-
repeatedPtrToVec(response.waypoints(), *ret);
72+
repeatedPtrToVec(response.waypoints(), *ret, auto_to_proto);
7273
return ret;
7374
});
7475
}
@@ -119,8 +120,9 @@ std::unique_ptr<std::vector<NavigationClient::Path>> NavigationClient::get_paths
119120
*request.mutable_extra() = map_to_struct(extra);
120121
})
121122
.invoke([](auto& response) {
122-
return std::make_unique<std::vector<Path>>(response.paths().begin(),
123-
response.paths().end());
123+
auto ret = std::make_unique<std::vector<Path>>();
124+
repeatedPtrToVec(response.paths(), *ret, auto_to_proto);
125+
return ret;
124126
});
125127
}
126128

src/viam/sdk/services/private/navigation_server.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <viam/sdk/common/proto_value.hpp>
77
#include <viam/sdk/common/service_helper.hpp>
88
#include <viam/sdk/common/utils.hpp>
9+
#include <viam/sdk/common/proto_utils.hpp>
910
#include <viam/sdk/services/motion.hpp>
1011
#include <viam/sdk/services/private/navigation_server.hpp>
1112
#include <viam/sdk/spatialmath/geometry.hpp>
@@ -51,7 +52,7 @@ ::grpc::Status NavigationServer::GetWaypoints(::grpc::ServerContext*,
5152
return make_service_helper<Navigation>(
5253
"NavigationServer::GetWaypoints", this, request)([&](auto& helper, auto& nav) {
5354
const auto& waypoints = nav->get_waypoints(request->name(), helper.getExtra());
54-
vecToRepeatedPtr(*waypoints, *response->mutable_waypoints());
55+
vecToRepeatedPtr(*waypoints, *response->mutable_waypoints(), auto_from_proto);
5556
});
5657
}
5758

@@ -90,7 +91,7 @@ ::grpc::Status NavigationServer::GetPaths(::grpc::ServerContext*,
9091
return make_service_helper<Navigation>(
9192
"NavigationServer::GetPaths", this, request)([&](auto& helper, auto& nav) {
9293
const auto& paths = nav->get_paths(request->name(), helper.getExtra());
93-
vecToRepeatedPtr(*paths, *response->mutable_paths());
94+
vecToRepeatedPtr(*paths, *response->mutable_paths(), auto_from_proto);
9495
});
9596
}
9697

0 commit comments

Comments
 (0)