Skip to content

Commit 2316a02

Browse files
committed
use pimpl and fix construction of test robot
1 parent 4d2902b commit 2316a02

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/viam/sdk/rpc/dial.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,35 @@
1818
namespace viam {
1919
namespace sdk {
2020

21-
ViamChannel::RustDialData::RustDialData(const char* path_, void* runtime)
22-
: path(path_), rust_runtime(runtime) {}
21+
struct ViamChannel::impl {
22+
impl(const char* path, void* runtime) : path(path), rust_runtime(runtime) {}
2323

24-
ViamChannel::RustDialData::RustDialData(RustDialData&& other) noexcept
25-
: path(std::exchange(other.path, nullptr)),
26-
rust_runtime(std::exchange(other.rust_runtime, nullptr)) {}
24+
impl(const impl&) = delete;
2725

28-
ViamChannel::RustDialData& ViamChannel::RustDialData::operator=(RustDialData&& other) noexcept {
29-
path = std::exchange(other.path, nullptr);
30-
rust_runtime = std::exchange(other.rust_runtime, nullptr);
26+
impl(impl&& other) noexcept
27+
: path(std::exchange(other.path, nullptr)),
28+
rust_runtime(std::exchange(other.rust_runtime, nullptr)) {}
3129

32-
return *this;
33-
}
30+
impl& operator=(const impl&) = delete;
3431

35-
ViamChannel::RustDialData::~RustDialData() {
36-
free_string(path);
37-
free_rust_runtime(rust_runtime);
38-
}
32+
impl& operator=(impl&& other) noexcept {
33+
path = std::exchange(other.path, nullptr);
34+
rust_runtime = std::exchange(other.rust_runtime, nullptr);
35+
36+
return *this;
37+
}
38+
39+
~impl() {
40+
free_string(path);
41+
free_rust_runtime(rust_runtime);
42+
}
43+
44+
const char* path;
45+
void* rust_runtime;
46+
};
3947

4048
ViamChannel::ViamChannel(std::shared_ptr<grpc::Channel> channel, const char* path, void* runtime)
41-
: channel_(std::move(channel)), rust_data_(RustDialData(path, runtime)) {}
49+
: channel_(std::move(channel)), pimpl_(std::make_unique<ViamChannel::impl>(path, runtime)) {}
4250

4351
ViamChannel::ViamChannel(std::shared_ptr<grpc::Channel> channel) : channel_(std::move(channel)) {}
4452

@@ -170,7 +178,7 @@ std::shared_ptr<ViamChannel> ViamChannel::dial(const char* uri,
170178
std::string address("unix://");
171179
address += socket_path;
172180
const std::shared_ptr<grpc::Channel> channel =
173-
impl::create_viam_channel(address, grpc::InsecureChannelCredentials());
181+
sdk::impl::create_viam_channel(address, grpc::InsecureChannelCredentials());
174182
const std::unique_ptr<viam::robot::v1::RobotService::Stub> st =
175183
viam::robot::v1::RobotService::NewStub(channel);
176184
return std::make_shared<ViamChannel>(channel, socket_path, ptr);
@@ -181,7 +189,7 @@ const std::shared_ptr<grpc::Channel>& ViamChannel::channel() const {
181189
}
182190

183191
void ViamChannel::close() {
184-
rust_data_.reset();
192+
pimpl_.reset();
185193
}
186194

187195
unsigned int Options::refresh_interval() const {

src/viam/sdk/rpc/dial.hpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,11 @@ class ViamChannel {
4444
void close();
4545

4646
private:
47-
struct RustDialData {
48-
RustDialData(const char* path_, void* runtime);
49-
50-
RustDialData(const RustDialData&) = delete;
51-
RustDialData(RustDialData&&) noexcept;
52-
53-
RustDialData& operator=(const RustDialData&) = delete;
54-
RustDialData& operator=(RustDialData&&) noexcept;
55-
56-
~RustDialData();
57-
58-
const char* path;
59-
void* rust_runtime;
60-
};
47+
struct impl;
6148

6249
std::shared_ptr<GrpcChannel> channel_;
6350

64-
boost::optional<RustDialData> rust_data_;
51+
std::unique_ptr<impl> pimpl_;
6552
};
6653

6754
class Credentials {

src/viam/sdk/tests/test_robot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void robot_client_to_mocks_pipeline(F&& test_case) {
5050
// in-process gRPC channel.
5151
auto test_server = TestServer(server);
5252
auto grpc_channel = test_server.grpc_in_process_channel();
53-
auto viam_channel = std::make_shared<ViamChannel>(grpc_channel, "", nullptr);
53+
auto viam_channel = std::make_shared<ViamChannel>(grpc_channel);
5454
auto client = RobotClient::with_channel(viam_channel, Options(0, boost::none));
5555

5656
// Run the passed-in test case on the created stack and give access to the

0 commit comments

Comments
 (0)