Skip to content

Commit 9dc2143

Browse files
lia-viamstuqdog
andauthored
RSDK-9298: Instance class (viamrobotics#374)
Co-authored-by: Ethan <[email protected]>
1 parent 4ec0ad8 commit 9dc2143

28 files changed

+285
-96
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ set(VIAMCPPSDK_XTL_VERSION_MINIMUM 0.7.2)
324324
set(VIAMCPPSDK_XTENSOR_VERSION_MINIMUM 0.24.3)
325325

326326
# Time to find `BOOST`.
327-
find_package(Boost ${VIAMCPPSDK_BOOST_VERSION_MINIMUM} REQUIRED COMPONENTS headers log program_options)
327+
if (VIAMCPPSDK_BUILD_TESTS)
328+
set(VIAMCPPSDK_BOOST_TEST "unit_test_framework")
329+
endif()
330+
331+
find_package(Boost ${VIAMCPPSDK_BOOST_VERSION_MINIMUM} REQUIRED COMPONENTS headers log program_options ${VIAMCPPSDK_BOOST_TEST})
328332

329333
# Time to find `protobuf` and `gRPC[++]`. Normally this would just be
330334
# something like `find_package(gRPC <version> CONFIG REQUIRED)`, and

src/viam/examples/camera/example_camera.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <unistd.h>
44
#include <vector>
55

6+
#include <viam/sdk/common/instance.hpp>
67
#include <viam/sdk/components/camera.hpp>
78
#include <viam/sdk/robot/client.hpp>
89
#include <viam/sdk/rpc/dial.hpp>
@@ -13,6 +14,11 @@ int main() {
1314
using std::endl;
1415
namespace vs = ::viam::sdk;
1516
try {
17+
// Every Viam C++ SDK program must have one and only one Instance object which is created
18+
// before
19+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
20+
vs::Instance inst;
21+
1622
// If you want to connect to a remote robot, this should be the url of the robot
1723
// Ex: xxx.xxx.viam.cloud
1824
std::string robot_address("localhost:8080");

src/viam/examples/dial/example_dial.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99

1010
#include <boost/optional.hpp>
1111

12+
#include <viam/sdk/common/instance.hpp>
1213
#include <viam/sdk/components/generic.hpp>
1314
#include <viam/sdk/robot/client.hpp>
1415
#include <viam/sdk/rpc/dial.hpp>
1516

1617
using namespace viam::sdk;
1718

1819
int main() {
20+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
21+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
22+
Instance inst;
23+
1924
const char* uri = "<your robot URI here>";
2025
DialOptions dial_options;
2126
std::string type = "<your authentication type>";

src/viam/examples/dial_api_key/example_dial_api_key.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <boost/optional.hpp>
1111
#include <boost/program_options.hpp>
1212

13+
#include <viam/sdk/common/instance.hpp>
1314
#include <viam/sdk/robot/client.hpp>
1415
#include <viam/sdk/rpc/dial.hpp>
1516

@@ -18,6 +19,10 @@ using namespace viam::sdk;
1819
namespace po = boost::program_options;
1920

2021
int main(int argc, char* argv[]) {
22+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
23+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
24+
Instance inst;
25+
2126
po::options_description desc("Allowed options");
2227
desc.add_options()("help", "List options and exit")(
2328
"uri", po::value<std::string>(), "URI of robot")(

src/viam/examples/mlmodel/example_audio_classification_client.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <boost/program_options.hpp>
3030
#include <boost/variant/get.hpp>
3131

32+
#include <viam/sdk/common/instance.hpp>
3233
#include <viam/sdk/robot/client.hpp>
3334
#include <viam/sdk/services/mlmodel.hpp>
3435

@@ -78,6 +79,10 @@ constexpr char kRobotConfigTemplate[] = R"(
7879
} // namespace
7980

8081
int main(int argc, char* argv[]) try {
82+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
83+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
84+
viam::sdk::Instance inst;
85+
8186
// Build up our command line options. The example operates in two
8287
// modes. In the "--generate" mode, it takes command line
8388
// parameters needed to satisfy the interpolation points in the

src/viam/examples/modules/complex/client.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <grpcpp/grpcpp.h>
1212
#include <grpcpp/support/status.h>
1313

14+
#include <viam/sdk/common/instance.hpp>
1415
#include <viam/sdk/components/motor.hpp>
1516
#include <viam/sdk/robot/client.hpp>
1617
#include <viam/sdk/rpc/dial.hpp>
@@ -21,6 +22,10 @@
2122
using namespace viam::sdk;
2223

2324
int main() {
25+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
26+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
27+
Instance inst;
28+
2429
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
2530
DialOptions dial_options;
2631
dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely
@@ -37,8 +42,8 @@ int main() {
3742

3843
// Register custom gizmo and summation clients so robot client can access resources
3944
// of that type from the server.
40-
Registry::register_resource_client<GizmoClient>();
41-
Registry::register_resource_client<SummationClient>();
45+
Registry::get().register_resource_client<GizmoClient>();
46+
Registry::get().register_resource_client<SummationClient>();
4247

4348
// Connect to robot.
4449
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);

src/viam/examples/modules/complex/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <grpcpp/grpcpp.h>
66
#include <grpcpp/server_context.h>
77

8+
#include <viam/sdk/common/instance.hpp>
89
#include <viam/sdk/components/base.hpp>
910
#include <viam/sdk/components/component.hpp>
1011
#include <viam/sdk/config/resource.hpp>
@@ -24,11 +25,15 @@
2425
using namespace viam::sdk;
2526

2627
int main(int argc, char** argv) {
28+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
29+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
30+
Instance inst;
31+
2732
Model mybase_model("viam", "base", "mybase");
2833

2934
// Make sure to explicity register resources with custom APIs.
30-
Registry::register_resource_server<GizmoServer>();
31-
Registry::register_resource_server<SummationServer>();
35+
Registry::get().register_resource_server<GizmoServer>();
36+
Registry::get().register_resource_server<SummationServer>();
3237

3338
std::shared_ptr<ModelRegistration> mybase_mr = std::make_shared<ModelRegistration>(
3439
API::get<Base>(),

src/viam/examples/modules/complex/test_complex_module.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ using namespace viam::sdktests;
2424

2525
struct RegisterGizmoAndSummationFixture {
2626
RegisterGizmoAndSummationFixture() {
27-
Registry::register_resource<GizmoClient, GizmoServer>();
28-
Registry::register_resource<SummationClient, SummationServer>();
27+
auto& registry = Registry::get();
28+
registry.register_resource<GizmoClient, GizmoServer>();
29+
registry.register_resource<SummationClient, SummationServer>();
2930
}
3031

3132
// Test teardown is a noop;

src/viam/examples/modules/simple/client.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <memory>
33
#include <string>
44

5+
#include <viam/sdk/common/instance.hpp>
56
#include <viam/sdk/common/proto_value.hpp>
67
#include <viam/sdk/components/sensor.hpp>
78
#include <viam/sdk/robot/client.hpp>
@@ -10,6 +11,10 @@
1011
using namespace viam::sdk;
1112

1213
int main() {
14+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
15+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
16+
Instance inst;
17+
1318
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
1419
DialOptions dial_options;
1520
dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely

src/viam/examples/modules/simple/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <sstream>
44

55
#include <viam/sdk/common/exception.hpp>
6+
#include <viam/sdk/common/instance.hpp>
67
#include <viam/sdk/common/proto_value.hpp>
78
#include <viam/sdk/components/sensor.hpp>
89
#include <viam/sdk/config/resource.hpp>
@@ -75,6 +76,10 @@ ProtoStruct MySensor::get_readings(const ProtoStruct&) {
7576
}
7677

7778
int main(int argc, char** argv) try {
79+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
80+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
81+
Instance inst;
82+
7883
Model mysensor_model("viam", "sensor", "mysensor");
7984

8085
std::shared_ptr<ModelRegistration> mr = std::make_shared<ModelRegistration>(

0 commit comments

Comments
 (0)