Skip to content

Commit 1ae0b1f

Browse files
committed
return vector instead of unique_ptr
1 parent 6c26a8c commit 1ae0b1f

File tree

7 files changed

+45
-49
lines changed

7 files changed

+45
-49
lines changed

src/viam/sdk/services/navigation.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Navigation : public Service {
9494
/// @brief Get the waypoints this nav service knows about.
9595
/// @param extra Any additional arguments to the method.
9696
/// @return List of waypoints.
97-
virtual std::unique_ptr<std::vector<Waypoint>> get_waypoints(const ProtoStruct& extra) = 0;
97+
virtual std::vector<Waypoint> get_waypoints(const ProtoStruct& extra) = 0;
9898

9999
/// @brief Add a waypoint.
100100
/// @param location Coordinate of the new waypoint.
@@ -108,12 +108,12 @@ class Navigation : public Service {
108108
/// @brief Get the obstacles this nav service knows about.
109109
/// @param extra Any additional arguments to the method.
110110
/// @return List of shapes.
111-
virtual std::unique_ptr<std::vector<geo_geometry>> get_obstacles(const ProtoStruct& extra) = 0;
111+
virtual std::vector<geo_geometry> get_obstacles(const ProtoStruct& extra) = 0;
112112

113113
/// @brief Get the paths this nav service knows about.
114114
/// @param extra Any additional arguments to the method.
115115
/// @return List of paths.
116-
virtual std::unique_ptr<std::vector<Path>> get_paths(const ProtoStruct& extra) = 0;
116+
virtual std::vector<Path> get_paths(const ProtoStruct& extra) = 0;
117117

118118
/// @brief Get this nav service's properties.
119119
/// @return Properties.
@@ -138,7 +138,7 @@ class Navigation : public Service {
138138
return get_location({});
139139
}
140140

141-
inline std::unique_ptr<std::vector<Waypoint>> get_waypoints() {
141+
inline std::vector<Waypoint> get_waypoints() {
142142
return get_waypoints({});
143143
}
144144

@@ -150,11 +150,11 @@ class Navigation : public Service {
150150
remove_waypoint(id, {});
151151
}
152152

153-
inline std::unique_ptr<std::vector<geo_geometry>> get_obstacles() {
153+
inline std::vector<geo_geometry> get_obstacles() {
154154
return get_obstacles({});
155155
}
156156

157-
inline std::unique_ptr<std::vector<Path>> get_paths() {
157+
inline std::vector<Path> get_paths() {
158158
return get_paths({});
159159
}
160160

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ Navigation::LocationResponse NavigationClient::get_location(const ProtoStruct& e
6060
});
6161
}
6262

63-
std::unique_ptr<std::vector<Navigation::Waypoint>> NavigationClient::get_waypoints(
64-
const ProtoStruct& extra) {
63+
std::vector<Navigation::Waypoint> NavigationClient::get_waypoints(const ProtoStruct& extra) {
6564
return make_client_helper(this, *stub_, &StubType::GetWaypoints)
6665
.with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); })
6766
.invoke([](auto& response) {
68-
auto ret = std::make_unique<std::vector<Navigation::Waypoint>>();
69-
repeatedPtrToVec(response.waypoints(), *ret, from_proto);
67+
std::vector<Navigation::Waypoint> ret;
68+
repeatedPtrToVec(response.waypoints(), ret, from_proto);
7069
return ret;
7170
});
7271
}
@@ -89,24 +88,22 @@ void NavigationClient::remove_waypoint(const std::string id, const ProtoStruct&
8988
.invoke([](auto& response) {});
9089
}
9190

92-
std::unique_ptr<std::vector<geo_geometry>> NavigationClient::get_obstacles(
93-
const ProtoStruct& extra) {
91+
std::vector<geo_geometry> NavigationClient::get_obstacles(const ProtoStruct& extra) {
9492
return make_client_helper(this, *stub_, &StubType::GetObstacles)
9593
.with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); })
9694
.invoke([](auto& response) {
97-
auto ret = std::make_unique<std::vector<geo_geometry>>();
98-
repeatedPtrToVec(response.obstacles(), *ret);
95+
std::vector<geo_geometry> ret;
96+
repeatedPtrToVec(response.obstacles(), ret);
9997
return ret;
10098
});
10199
}
102100

103-
std::unique_ptr<std::vector<NavigationClient::Path>> NavigationClient::get_paths(
104-
const ProtoStruct& extra) {
101+
std::vector<NavigationClient::Path> NavigationClient::get_paths(const ProtoStruct& extra) {
105102
return make_client_helper(this, *stub_, &StubType::GetPaths)
106103
.with([&](auto& request) { *request.mutable_extra() = map_to_struct(extra); })
107104
.invoke([](auto& response) {
108-
auto ret = std::make_unique<std::vector<Path>>();
109-
repeatedPtrToVec(response.paths(), *ret, from_proto);
105+
std::vector<Path> ret;
106+
repeatedPtrToVec(response.paths(), ret, from_proto);
110107
return ret;
111108
});
112109
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class NavigationClient : public Navigation {
2424
Mode get_mode(const ProtoStruct& extra) override;
2525
void set_mode(const Mode mode, const ProtoStruct& extra) override;
2626
LocationResponse get_location(const ProtoStruct& extra) override;
27-
std::unique_ptr<std::vector<Waypoint>> get_waypoints(const ProtoStruct& extra) override;
27+
std::vector<Waypoint> get_waypoints(const ProtoStruct& extra) override;
2828
void add_waypoint(const geo_point& location, const ProtoStruct& extra) override;
2929
void remove_waypoint(const std::string id, const ProtoStruct& extra) override;
30-
std::unique_ptr<std::vector<geo_geometry>> get_obstacles(const ProtoStruct& extra) override;
31-
std::unique_ptr<std::vector<Path>> get_paths(const ProtoStruct& extra) override;
30+
std::vector<geo_geometry> get_obstacles(const ProtoStruct& extra) override;
31+
std::vector<Path> get_paths(const ProtoStruct& extra) override;
3232
Properties get_properties() override;
3333
ProtoStruct do_command(const ProtoStruct& command) override;
3434

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ ::grpc::Status NavigationServer::GetWaypoints(::grpc::ServerContext*,
6565
GetWaypointsResponse* response) noexcept {
6666
return make_service_helper<Navigation>(
6767
"NavigationServer::GetWaypoints", this, request)([&](auto& helper, auto& nav) {
68-
const auto& waypoints = nav->get_waypoints(helper.getExtra());
69-
vecToRepeatedPtr(*waypoints, *response->mutable_waypoints(), to_proto);
68+
const auto waypoints = nav->get_waypoints(helper.getExtra());
69+
vecToRepeatedPtr(waypoints, *response->mutable_waypoints(), to_proto);
7070
});
7171
}
7272

@@ -91,8 +91,8 @@ ::grpc::Status NavigationServer::GetObstacles(::grpc::ServerContext*,
9191
GetObstaclesResponse* response) noexcept {
9292
return make_service_helper<Navigation>(
9393
"NavigationServer::GetObstacles", this, request)([&](auto& helper, auto& nav) {
94-
const auto& obstacles = nav->get_obstacles(helper.getExtra());
95-
vecToRepeatedPtr(*obstacles, *response->mutable_obstacles());
94+
const auto obstacles = nav->get_obstacles(helper.getExtra());
95+
vecToRepeatedPtr(obstacles, *response->mutable_obstacles());
9696
});
9797
}
9898

@@ -101,8 +101,8 @@ ::grpc::Status NavigationServer::GetPaths(::grpc::ServerContext*,
101101
GetPathsResponse* response) noexcept {
102102
return make_service_helper<Navigation>(
103103
"NavigationServer::GetPaths", this, request)([&](auto& helper, auto& nav) {
104-
const auto& paths = nav->get_paths(helper.getExtra());
105-
vecToRepeatedPtr(*paths, *response->mutable_paths(), to_proto);
104+
const auto paths = nav->get_paths(helper.getExtra());
105+
vecToRepeatedPtr(paths, *response->mutable_paths(), to_proto);
106106
});
107107
}
108108

src/viam/sdk/tests/mocks/mock_navigation.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ Navigation::LocationResponse MockNav::get_location(const ProtoStruct& extra) {
2929
return loc;
3030
}
3131

32-
std::unique_ptr<std::vector<Navigation::Waypoint>> MockNav::get_waypoints(
33-
const ProtoStruct& extra) {
34-
return std::make_unique<std::vector<Waypoint>>(waypoints);
32+
std::vector<Navigation::Waypoint> MockNav::get_waypoints(const ProtoStruct& extra) {
33+
return waypoints;
3534
}
3635

3736
void MockNav::add_waypoint(const geo_point& location, const ProtoStruct& extra) {
@@ -47,12 +46,12 @@ void MockNav::remove_waypoint(const std::string id, const ProtoStruct& extra) {
4746
}
4847
}
4948

50-
std::unique_ptr<std::vector<geo_geometry>> MockNav::get_obstacles(const ProtoStruct& extra) {
51-
return std::make_unique<std::vector<geo_geometry>>(obstacles);
49+
std::vector<geo_geometry> MockNav::get_obstacles(const ProtoStruct& extra) {
50+
return obstacles;
5251
}
5352

54-
std::unique_ptr<std::vector<Navigation::Path>> MockNav::get_paths(const ProtoStruct& extra) {
55-
return std::make_unique<std::vector<Path>>(paths);
53+
std::vector<Navigation::Path> MockNav::get_paths(const ProtoStruct& extra) {
54+
return paths;
5655
}
5756

5857
Navigation::Properties MockNav::get_properties() {

src/viam/sdk/tests/mocks/mock_navigation.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class MockNav : public sdk::Navigation {
1414
Mode get_mode(const ProtoStruct& extra) override;
1515
void set_mode(const Mode mode, const ProtoStruct& extra) override;
1616
LocationResponse get_location(const ProtoStruct& extra) override;
17-
std::unique_ptr<std::vector<Waypoint>> get_waypoints(const ProtoStruct& extra) override;
17+
std::vector<Waypoint> get_waypoints(const ProtoStruct& extra) override;
1818
void add_waypoint(const geo_point& location, const ProtoStruct& extra) override;
1919
void remove_waypoint(const std::string id, const ProtoStruct& extra) override;
20-
std::unique_ptr<std::vector<geo_geometry>> get_obstacles(const ProtoStruct& extra) override;
21-
std::unique_ptr<std::vector<Path>> get_paths(const ProtoStruct& extra) override;
20+
std::vector<geo_geometry> get_obstacles(const ProtoStruct& extra) override;
21+
std::vector<Path> get_paths(const ProtoStruct& extra) override;
2222
Properties get_properties() override;
2323
ProtoStruct do_command(const ProtoStruct& command) override;
2424

src/viam/sdk/tests/test_navigation.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ BOOST_AUTO_TEST_CASE(nav_waypoints) {
5858
client_to_mock_pipeline<Navigation>(mock, [&](Navigation& client) {
5959
// confirm empty
6060
auto waypoints = client.get_waypoints();
61-
BOOST_CHECK_EQUAL(waypoints->size(), 0);
61+
BOOST_CHECK_EQUAL(waypoints.size(), 0);
6262

6363
// add 3 and confirm size
6464
for (int i = 0; i < 3; i++) {
6565
client.add_waypoint(geo_point{0, double(i)});
6666
}
6767
waypoints = client.get_waypoints();
68-
BOOST_CHECK_EQUAL(waypoints->size(), 3);
68+
BOOST_CHECK_EQUAL(waypoints.size(), 3);
6969

7070
// remove 1, check size and IDs
71-
client.remove_waypoint((*waypoints)[1].id);
72-
const std::vector<std::string> expected_ids = {(*waypoints)[0].id, (*waypoints)[2].id};
71+
client.remove_waypoint(waypoints[1].id);
72+
const std::vector<std::string> expected_ids = {waypoints[0].id, waypoints[2].id};
7373
waypoints = client.get_waypoints();
74-
BOOST_CHECK_EQUAL(waypoints->size(), 2);
74+
BOOST_CHECK_EQUAL(waypoints.size(), 2);
7575
const auto actual_ids =
76-
mapOver(*waypoints, [](const Navigation::Waypoint& wp) { return wp.id; });
76+
mapOver(waypoints, [](const Navigation::Waypoint& wp) { return wp.id; });
7777
BOOST_CHECK_EQUAL(expected_ids, actual_ids);
7878
});
7979
}
@@ -83,18 +83,18 @@ BOOST_AUTO_TEST_CASE(nav_obstacles) {
8383
client_to_mock_pipeline<Navigation>(mock, [&](Navigation& client) {
8484
// empty case
8585
auto obstacles = client.get_obstacles();
86-
BOOST_CHECK_EQUAL(obstacles->size(), 0);
86+
BOOST_CHECK_EQUAL(obstacles.size(), 0);
8787

8888
// one element
8989
mock->obstacles.push_back({});
9090
obstacles = client.get_obstacles();
91-
BOOST_CHECK_EQUAL(obstacles->size(), 1);
91+
BOOST_CHECK_EQUAL(obstacles.size(), 1);
9292

9393
// one element with one sub-element
9494
mock->obstacles.back().geometries.push_back({});
9595
obstacles = client.get_obstacles();
96-
BOOST_CHECK_EQUAL(obstacles->size(), 1);
97-
BOOST_CHECK_EQUAL(obstacles->back().geometries.size(), 1);
96+
BOOST_CHECK_EQUAL(obstacles.size(), 1);
97+
BOOST_CHECK_EQUAL(obstacles.back().geometries.size(), 1);
9898
});
9999
}
100100

@@ -103,13 +103,13 @@ BOOST_AUTO_TEST_CASE(nav_paths) {
103103
client_to_mock_pipeline<Navigation>(mock, [&](Navigation& client) {
104104
// empty
105105
auto paths = client.get_paths();
106-
BOOST_CHECK_EQUAL(paths->size(), 0);
106+
BOOST_CHECK_EQUAL(paths.size(), 0);
107107

108108
// 1 element with 1 sub-element
109109
mock->paths.push_back({"2", {{}}});
110110
paths = client.get_paths();
111111
std::vector<Navigation::Path> expected = {Navigation::Path{"2", {{}}}};
112-
BOOST_CHECK_EQUAL(*paths, expected);
112+
BOOST_CHECK_EQUAL(paths, expected);
113113
});
114114
}
115115

0 commit comments

Comments
 (0)