Skip to content

Commit 67c7615

Browse files
committed
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into feature/switch
2 parents 1339002 + 3734b5a commit 67c7615

File tree

17 files changed

+386
-64
lines changed

17 files changed

+386
-64
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# constrained by the version of CMake available on target systems.
3535
cmake_minimum_required(VERSION 3.25 FATAL_ERROR)
3636

37-
set(CMAKE_PROJECT_VERSION 0.4.0)
37+
set(CMAKE_PROJECT_VERSION 0.5.0)
3838

3939
# Identify the project.
4040
project(viam-cpp-sdk

src/viam/api/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
163163
${PROTO_GEN_DIR}/component/board/v1/board.grpc.pb.h
164164
${PROTO_GEN_DIR}/component/board/v1/board.pb.cc
165165
${PROTO_GEN_DIR}/component/board/v1/board.pb.h
166+
${PROTO_GEN_DIR}/component/button/v1/button.grpc.pb.cc
167+
${PROTO_GEN_DIR}/component/button/v1/button.grpc.pb.h
168+
${PROTO_GEN_DIR}/component/button/v1/button.pb.cc
169+
${PROTO_GEN_DIR}/component/button/v1/button.pb.h
166170
${PROTO_GEN_DIR}/component/camera/v1/camera.grpc.pb.cc
167171
${PROTO_GEN_DIR}/component/camera/v1/camera.grpc.pb.h
168172
${PROTO_GEN_DIR}/component/camera/v1/camera.pb.cc
@@ -317,6 +321,8 @@ target_sources(viamapi
317321
${PROTO_GEN_DIR}/component/base/v1/base.pb.cc
318322
${PROTO_GEN_DIR}/component/board/v1/board.grpc.pb.cc
319323
${PROTO_GEN_DIR}/component/board/v1/board.pb.cc
324+
${PROTO_GEN_DIR}/component/button/v1/button.grpc.pb.cc
325+
${PROTO_GEN_DIR}/component/button/v1/button.pb.cc
320326
${PROTO_GEN_DIR}/component/camera/v1/camera.grpc.pb.cc
321327
${PROTO_GEN_DIR}/component/camera/v1/camera.pb.cc
322328
${PROTO_GEN_DIR}/component/encoder/v1/encoder.grpc.pb.cc
@@ -379,6 +385,8 @@ target_sources(viamapi
379385
${PROTO_GEN_DIR}/../../viam/api/component/base/v1/base.pb.h
380386
${PROTO_GEN_DIR}/../../viam/api/component/board/v1/board.grpc.pb.h
381387
${PROTO_GEN_DIR}/../../viam/api/component/board/v1/board.pb.h
388+
${PROTO_GEN_DIR}/../../viam/api/component/button/v1/button.grpc.pb.h
389+
${PROTO_GEN_DIR}/../../viam/api/component/button/v1/button.pb.h
382390
${PROTO_GEN_DIR}/../../viam/api/component/camera/v1/camera.grpc.pb.h
383391
${PROTO_GEN_DIR}/../../viam/api/component/camera/v1/camera.pb.h
384392
${PROTO_GEN_DIR}/../../viam/api/component/encoder/v1/encoder.grpc.pb.h

src/viam/sdk/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ target_sources(viamsdk
6161
components/arm.cpp
6262
components/base.cpp
6363
components/board.cpp
64+
components/button.cpp
6465
components/camera.cpp
6566
components/component.cpp
6667
components/encoder.cpp
@@ -78,6 +79,8 @@ target_sources(viamsdk
7879
components/private/board_client.cpp
7980
components/private/board_server.cpp
8081
components/private/camera_client.cpp
82+
components/private/button_client.cpp
83+
components/private/button_server.cpp
8184
components/private/camera_server.cpp
8285
components/private/encoder.cpp
8386
components/private/encoder_client.cpp
@@ -160,6 +163,7 @@ target_sources(viamsdk
160163
../../viam/sdk/components/arm.hpp
161164
../../viam/sdk/components/base.hpp
162165
../../viam/sdk/components/board.hpp
166+
../../viam/sdk/components/button.hpp
163167
../../viam/sdk/components/camera.hpp
164168
../../viam/sdk/components/component.hpp
165169
../../viam/sdk/components/encoder.hpp

src/viam/sdk/common/proto_value.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,49 +76,37 @@ bool ProtoValue::is_null() const {
7676
}
7777

7878
template <typename T>
79-
std::enable_if_t<std::is_scalar<T>{}, T&> ProtoValue::get_unchecked() {
79+
T& ProtoValue::get_unchecked() & {
8080
assert(this->is_a<T>());
8181
return *(this->self_.template get<T>());
8282
}
8383

84-
template <typename T>
85-
std::enable_if_t<std::is_scalar<T>{}, T> ProtoValue::get_unchecked() const {
86-
assert(this->is_a<T>());
87-
return *(this->self_.template get<T>());
88-
}
89-
90-
template bool& ProtoValue::get_unchecked<bool>();
91-
template double& ProtoValue::get_unchecked<double>();
92-
93-
template bool ProtoValue::get_unchecked<bool>() const;
94-
template double ProtoValue::get_unchecked<double>() const;
84+
template bool& ProtoValue::get_unchecked<bool>() &;
85+
template double& ProtoValue::get_unchecked<double>() &;
86+
template std::string& ProtoValue::get_unchecked<std::string>() &;
87+
template ProtoList& ProtoValue::get_unchecked<ProtoList>() &;
88+
template ProtoStruct& ProtoValue::get_unchecked<ProtoStruct>() &;
9589

9690
template <typename T>
97-
std::enable_if_t<!std::is_scalar<T>{}, T&> ProtoValue::get_unchecked() & {
91+
const T& ProtoValue::get_unchecked() const& {
9892
assert(this->is_a<T>());
9993
return *(this->self_.template get<T>());
10094
}
10195

102-
template <typename T>
103-
std::enable_if_t<!std::is_scalar<T>{}, T const&> ProtoValue::get_unchecked() const& {
104-
assert(this->is_a<T>());
105-
return *(this->self_.template get<T>());
106-
}
96+
template const bool& ProtoValue::get_unchecked<bool>() const&;
97+
template const double& ProtoValue::get_unchecked<double>() const&;
98+
template std::string const& ProtoValue::get_unchecked<std::string>() const&;
99+
template ProtoList const& ProtoValue::get_unchecked<ProtoList>() const&;
100+
template ProtoStruct const& ProtoValue::get_unchecked<ProtoStruct>() const&;
107101

108102
template <typename T>
109-
std::enable_if_t<!std::is_scalar<T>{}, T&&> ProtoValue::get_unchecked() && {
103+
T&& ProtoValue::get_unchecked() && {
110104
assert(this->is_a<T>());
111105
return std::move(*(this->self_.template get<T>()));
112106
}
113107

114-
template std::string& ProtoValue::get_unchecked<std::string>() &;
115-
template ProtoList& ProtoValue::get_unchecked<ProtoList>() &;
116-
template ProtoStruct& ProtoValue::get_unchecked<ProtoStruct>() &;
117-
118-
template std::string const& ProtoValue::get_unchecked<std::string>() const&;
119-
template ProtoList const& ProtoValue::get_unchecked<ProtoList>() const&;
120-
template ProtoStruct const& ProtoValue::get_unchecked<ProtoStruct>() const&;
121-
108+
template bool&& ProtoValue::get_unchecked<bool>() &&;
109+
template double&& ProtoValue::get_unchecked<double>() &&;
122110
template std::string&& ProtoValue::get_unchecked<std::string>() &&;
123111
template ProtoList&& ProtoValue::get_unchecked<ProtoList>() &&;
124112
template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;

src/viam/sdk/common/proto_value.hpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,16 @@ class ProtoValue {
136136
T const* get() const;
137137

138138
/// @brief Return a reference to the underlying T, without checking.
139-
/// @tparam T a bool or double
140139
template <typename T>
141-
std::enable_if_t<std::is_scalar<T>{}, T&> get_unchecked();
142-
143-
/// @brief Return the underlying T by value, without checking.
144-
/// @tparam T a bool or double.
145-
template <typename T>
146-
std::enable_if_t<std::is_scalar<T>{}, T> get_unchecked() const;
147-
148-
/// @brief Return a mutable reference to the underlying T, without checking
149-
/// @tparam T a std::string, ProtoList, or ProtoStruct.
150-
template <typename T>
151-
std::enable_if_t<!std::is_scalar<T>{}, T&> get_unchecked() &;
140+
T& get_unchecked() &;
152141

153142
/// @brief Return an immutable reference to the underlying T, without checking.
154-
/// @tparam T a std::string, ProtoList, or ProtoStruct.
155143
template <typename T>
156-
std::enable_if_t<!std::is_scalar<T>{}, T const&> get_unchecked() const&;
144+
const T& get_unchecked() const&;
157145

158146
/// @brief Return an rvalue reference to the underlying T, without checking.
159-
/// @tparam T a std::string, ProtoList, or ProtoStruct.
160147
template <typename T>
161-
std::enable_if_t<!std::is_scalar<T>{}, T&&> get_unchecked() &&;
148+
T&& get_unchecked() &&;
162149

163150
///@}
164151

@@ -295,22 +282,21 @@ extern template ProtoValue::ProtoValue(ProtoList) noexcept(
295282
extern template ProtoValue::ProtoValue(ProtoStruct m) noexcept(
296283
std::is_nothrow_move_constructible<ProtoStruct>{});
297284

298-
// -- Template specialization declarations of get_unchecked: POD types -- //
299-
extern template bool& ProtoValue::get_unchecked<bool>();
300-
extern template double& ProtoValue::get_unchecked<double>();
301-
302-
extern template bool ProtoValue::get_unchecked<bool>() const;
303-
extern template double ProtoValue::get_unchecked<double>() const;
304-
305-
// -- Template specialization declarations of get_unchecked: string and recursive types -- //
285+
// -- Template specialization declarations of get_unchecked -- //
286+
extern template bool& ProtoValue::get_unchecked<bool>() &;
287+
extern template double& ProtoValue::get_unchecked<double>() &;
306288
extern template std::string& ProtoValue::get_unchecked<std::string>() &;
307289
extern template ProtoList& ProtoValue::get_unchecked<ProtoList>() &;
308290
extern template ProtoStruct& ProtoValue::get_unchecked<ProtoStruct>() &;
309291

292+
extern template bool const& ProtoValue::get_unchecked<bool>() const&;
293+
extern template double const& ProtoValue::get_unchecked<double>() const&;
310294
extern template std::string const& ProtoValue::get_unchecked<std::string>() const&;
311295
extern template ProtoList const& ProtoValue::get_unchecked<ProtoList>() const&;
312296
extern template ProtoStruct const& ProtoValue::get_unchecked<ProtoStruct>() const&;
313297

298+
extern template bool&& ProtoValue::get_unchecked<bool>() &&;
299+
extern template double&& ProtoValue::get_unchecked<double>() &&;
314300
extern template std::string&& ProtoValue::get_unchecked<std::string>() &&;
315301
extern template ProtoList&& ProtoValue::get_unchecked<ProtoList>() &&;
316302
extern template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;

src/viam/sdk/components/button.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <viam/sdk/components/button.hpp>
2+
3+
#include <viam/sdk/common/utils.hpp>
4+
5+
namespace viam {
6+
namespace sdk {
7+
8+
API Button::api() const {
9+
return API::get<Button>();
10+
}
11+
12+
API API::traits<Button>::api() {
13+
return {kRDK, kComponent, "button"};
14+
}
15+
16+
Button::Button(std::string name) : Component(std::move(name)) {}
17+
18+
} // namespace sdk
19+
} // namespace viam

src/viam/sdk/components/button.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// @file components/button.hpp
2+
///
3+
/// @brief Defines a `Button` component
4+
#pragma once
5+
6+
#include <string>
7+
8+
#include <viam/sdk/common/proto_value.hpp>
9+
#include <viam/sdk/components/component.hpp>
10+
#include <viam/sdk/resource/resource_api.hpp>
11+
12+
namespace viam {
13+
namespace sdk {
14+
15+
/// @defgroup Button Classes related to the Button component.
16+
17+
/// @class Button button.hpp "components/button.hpp"
18+
/// @brief A `Button` represents a physical or virtual button.
19+
/// @ingroup Button
20+
///
21+
/// This acts as an abstract parent class to be inherited from by any drivers representing
22+
/// specific button implementations. This class cannot be used on its own.
23+
class Button : public Component {
24+
public:
25+
/// @brief Push the button.
26+
inline void push() {
27+
return push({});
28+
}
29+
30+
/// @brief Push the button.
31+
/// @param extra Any additional arguments to the method.
32+
virtual void push(const ProtoStruct& extra) = 0;
33+
34+
/// @brief Send/receive arbitrary commands to the resource.
35+
/// @param Command the command to execute.
36+
/// @return The result of the executed command.
37+
virtual ProtoStruct do_command(const ProtoStruct& command) = 0;
38+
39+
API api() const override;
40+
41+
protected:
42+
explicit Button(std::string name);
43+
};
44+
45+
template <>
46+
struct API::traits<Button> {
47+
static API api();
48+
};
49+
50+
} // namespace sdk
51+
} // namespace viam
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <viam/sdk/components/private/button_client.hpp>
2+
3+
#include <viam/api/component/button/v1/button.grpc.pb.h>
4+
#include <viam/api/component/button/v1/button.pb.h>
5+
6+
#include <viam/sdk/common/client_helper.hpp>
7+
#include <viam/sdk/common/proto_value.hpp>
8+
9+
namespace viam {
10+
namespace sdk {
11+
namespace impl {
12+
13+
ButtonClient::ButtonClient(std::string name, std::shared_ptr<grpc::Channel> channel)
14+
: Button(std::move(name)),
15+
stub_(viam::component::button::v1::ButtonService::NewStub(channel)),
16+
channel_(std::move(channel)) {}
17+
18+
void ButtonClient::push(const ProtoStruct& extra) {
19+
return make_client_helper(this, *stub_, &StubType::Push).with(extra).invoke();
20+
}
21+
22+
ProtoStruct ButtonClient::do_command(const ProtoStruct& command) {
23+
return make_client_helper(this, *stub_, &StubType::DoCommand)
24+
.with([&](auto& request) { *request.mutable_command() = to_proto(command); })
25+
.invoke([](auto& response) { return from_proto(response.result()); });
26+
}
27+
28+
} // namespace impl
29+
} // namespace sdk
30+
} // namespace viam
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// @file components/private/button_client.hpp
2+
///
3+
/// @brief Implements a gRPC client for the `Button` component
4+
#pragma once
5+
6+
#include <memory>
7+
8+
#include <grpcpp/channel.h>
9+
10+
#include <viam/api/component/button/v1/button.grpc.pb.h>
11+
12+
#include <viam/sdk/components/button.hpp>
13+
14+
namespace viam {
15+
namespace sdk {
16+
namespace impl {
17+
18+
/// @class ButtonClient
19+
/// @brief gRPC client implementation of a `Button` component
20+
/// @ingroup Button
21+
class ButtonClient : public Button {
22+
public:
23+
using interface_type = Button;
24+
ButtonClient(std::string name, std::shared_ptr<grpc::Channel> channel);
25+
26+
void push(const ProtoStruct& extra) override;
27+
ProtoStruct do_command(const ProtoStruct& command) override;
28+
29+
using Button::push;
30+
31+
private:
32+
using StubType = viam::component::button::v1::ButtonService::StubInterface;
33+
std::unique_ptr<StubType> stub_;
34+
std::shared_ptr<grpc::Channel> channel_;
35+
};
36+
37+
} // namespace impl
38+
} // namespace sdk
39+
} // namespace viam
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <viam/sdk/components/private/button_server.hpp>
2+
3+
#include <viam/sdk/common/private/service_helper.hpp>
4+
5+
namespace viam {
6+
namespace sdk {
7+
namespace impl {
8+
9+
ButtonServer::ButtonServer(std::shared_ptr<ResourceManager> manager)
10+
: ResourceServer(std::move(manager)) {}
11+
12+
::grpc::Status ButtonServer::Push(::grpc::ServerContext*,
13+
const ::viam::component::button::v1::PushRequest* request,
14+
::viam::component::button::v1::PushResponse*) noexcept {
15+
return make_service_helper<Button>("ButtonServer::Push", this, request)(
16+
[&](auto& helper, auto& button) { button->push(helper.getExtra()); });
17+
}
18+
19+
::grpc::Status ButtonServer::DoCommand(::grpc::ServerContext*,
20+
const ::viam::common::v1::DoCommandRequest* request,
21+
::viam::common::v1::DoCommandResponse* response) noexcept {
22+
return make_service_helper<Button>(
23+
"ButtonServer::DoCommand", this, request)([&](auto&, auto& button) {
24+
const ProtoStruct result = button->do_command(from_proto(request->command()));
25+
*response->mutable_result() = to_proto(result);
26+
});
27+
}
28+
29+
} // namespace impl
30+
} // namespace sdk
31+
} // namespace viam

0 commit comments

Comments
 (0)