Skip to content

Commit 18d26c9

Browse files
committed
revert and adapt examples
1 parent 4771390 commit 18d26c9

File tree

11 files changed

+55
-6
lines changed

11 files changed

+55
-6
lines changed

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: 5 additions & 0 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

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::get().register_resource_server<GizmoServer>();
31-
Registry::get().register_resource_server<SummationServer>();
35+
inst.registry()->register_resource_server<GizmoServer>();
36+
inst.registry()->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::get().register_resource<GizmoClient, GizmoServer>();
28-
Registry::get().register_resource<SummationClient, SummationServer>();
27+
auto* registry = Instance::current().registry();
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: 3 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,8 @@
1011
using namespace viam::sdk;
1112

1213
int main() {
14+
Instance inst;
15+
1316
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
1417
DialOptions dial_options;
1518
dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely

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

Lines changed: 3 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,8 @@ ProtoStruct MySensor::get_readings(const ProtoStruct&) {
7576
}
7677

7778
int main(int argc, char** argv) try {
79+
Instance inst;
80+
7881
Model mysensor_model("viam", "sensor", "mysensor");
7982

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

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <tensorflow/lite/c/c_api.h>
2525

26+
#include <viam/sdk/common/instance.hpp>
2627
#include <viam/sdk/common/proto_value.hpp>
2728
#include <viam/sdk/components/component.hpp>
2829
#include <viam/sdk/config/resource.hpp>
@@ -723,6 +724,10 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
723724
};
724725

725726
int serve(const std::string& socket_path) try {
727+
// Every Viam C++ SDK program must have one and only one Instance object which is created before
728+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
729+
vsdk::Instance inst;
730+
726731
// Create a new model registration for the service.
727732
auto module_registration = std::make_shared<vsdk::ModelRegistration>(
728733
// Identify that this resource offers the MLModelService API
@@ -737,10 +742,10 @@ int serve(const std::string& socket_path) try {
737742
});
738743

739744
// Register the newly created registration with the Registry.
740-
vsdk::Registry::register_model(module_registration);
745+
inst.registry()->register_model(module_registration);
741746

742747
// Construct the module service and tell it where to place the socket path.
743-
auto module_service = std::make_shared<vsdk::ModuleService>(socket_path);
748+
auto module_service = std::make_shared<vsdk::ModuleService>(socket_path, inst.registry());
744749

745750
// Add the server as providing the API and model declared in the
746751
// registration.

0 commit comments

Comments
 (0)