Skip to content

Commit 185121a

Browse files
committed
registry get cannot create instance by default
1 parent e80bf80 commit 185121a

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

src/viam/sdk/common/instance.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff 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();

src/viam/sdk/common/instance.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@ namespace sdk {
1212
/// objects in the same program is an error.
1313
class 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;

src/viam/sdk/registry/registry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const Model& ModelRegistration::model() const {
9292
};
9393

9494
Registry& 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
}

src/viam/sdk/tests/test_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <viam/sdk/spatialmath/orientation_types.hpp>
1111

1212
namespace viam {
13-
namespace sdktests {
1413

14+
namespace sdktests {
1515
using namespace viam::sdk;
1616

1717
ProtoStruct fake_map() {

src/viam/sdk/tests/test_utils.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
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>
@@ -10,6 +13,15 @@
1013
namespace viam {
1114
namespace 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+
1325
using namespace viam::sdk;
1426

1527
ProtoStruct fake_map();

0 commit comments

Comments
 (0)