File tree Expand file tree Collapse file tree 5 files changed +31
-6
lines changed Expand file tree Collapse file tree 5 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -34,10 +34,14 @@ Instance::~Instance() {
3434 impl_.reset ();
3535}
3636
37- Instance& Instance::current () {
37+ Instance& Instance::current (Instance::Creation creation ) {
3838 if (!current_instance.load ()) {
39- // This variable declaration calls the default ctor, storing a current instance.
40- static Instance inst; // NOLINT (misc-const-correctness)
39+ if (creation == Creation::if_needed) {
40+ // This variable declaration calls the default ctor, storing a current instance.
41+ static Instance inst; // NOLINT (misc-const-correctness)
42+ } else {
43+ throw Exception (" Instance has not yet been created" );
44+ }
4145 }
4246
4347 Instance* current = current_instance.load ();
Original file line number Diff line number Diff line change @@ -12,10 +12,19 @@ namespace sdk {
1212// / objects in the same program is an error.
1313class Instance {
1414 public:
15+ // / @brief Enumeration for creation behavior of @ref current
16+ enum class Creation {
17+ open_existing, // /< Instance must already exist
18+ if_needed // /< Use existing instance if present, else create one.
19+ };
20+
1521 Instance ();
1622 ~Instance ();
1723
18- static Instance& current ();
24+ // / @brief Get the current Instance according to the Creation behavior.
25+ // / Calling current(Creation::open_existing) when an instance has not yet been constructed is an
26+ // / error.
27+ static Instance& current (Creation);
1928
2029 private:
2130 friend class Registry ;
Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ const Model& ModelRegistration::model() const {
9292};
9393
9494Registry& Registry::get () {
95- static Registry& result = Instance::current ().impl_ ->registry ;
95+ static Registry& result = Instance::current (Instance::Creation::open_existing ).impl_ ->registry ;
9696
9797 return result;
9898}
Original file line number Diff line number Diff line change 1010#include < viam/sdk/spatialmath/orientation_types.hpp>
1111
1212namespace viam {
13- namespace sdktests {
1413
14+ namespace sdktests {
1515using namespace viam ::sdk;
1616
1717ProtoStruct fake_map () {
Original file line number Diff line number Diff line change 22
33#include < grpcpp/grpcpp.h>
44
5+ #include < boost/test/unit_test.hpp>
6+
7+ #include < viam/sdk/common/instance.hpp>
58#include < viam/sdk/config/resource.hpp>
69#include < viam/sdk/registry/registry.hpp>
710#include < viam/sdk/resource/resource.hpp>
1013namespace viam {
1114namespace sdktests {
1215
16+ struct GlobalFixture {
17+ GlobalFixture () {
18+ BOOST_TEST_MESSAGE (" Creating instance" );
19+ (void )sdk::Instance::current (sdk::Instance::Creation::if_needed);
20+ }
21+ };
22+
23+ BOOST_TEST_GLOBAL_FIXTURE (GlobalFixture);
24+
1325using namespace viam ::sdk;
1426
1527ProtoStruct fake_map ();
You can’t perform that action at this time.
0 commit comments