Skip to content

Commit

Permalink
Convert bmi_model members from shared_ptr to unique_ptr
Browse files Browse the repository at this point in the history
This avoids giving the impression that there should ever actually be
shared ownership - a model instance is always exclusively held by the
single adapter instance that created it.

Leave the C++ adapter alone for now due to the more complicated
lifetime logic that it implements.
  • Loading branch information
PhilMiller committed Nov 8, 2023
1 parent 6ac2685 commit e1d2bef
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/bmi/Bmi_C_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ namespace models {
inline void construct_and_init_backing_model_for_type() {
if (model_initialized)
return;
bmi_model = std::make_shared<C_Bmi>(C_Bmi());
bmi_model = std::make_unique<C_Bmi>(C_Bmi());
execModuleRegistration();
int init_result = bmi_model->initialize(bmi_model.get(), bmi_init_config.c_str());
if (init_result != BMI_SUCCESS) {
Expand Down Expand Up @@ -643,7 +643,7 @@ namespace models {
friend class ::Bmi_C_Adapter_Test;

/** Pointer to backing BMI model instance. */
std::shared_ptr<C_Bmi> bmi_model = nullptr;
std::unique_ptr<C_Bmi> bmi_model = nullptr;

};

Expand Down
4 changes: 2 additions & 2 deletions include/bmi/Bmi_Fortran_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ namespace models {
inline void construct_and_init_backing_model_for_fortran() {
if (model_initialized)
return;
bmi_model = std::make_shared<Bmi_Fortran_Handle_Wrapper>(Bmi_Fortran_Handle_Wrapper());
bmi_model = std::make_unique<Bmi_Fortran_Handle_Wrapper>(Bmi_Fortran_Handle_Wrapper());
dynamic_library_load();
execModuleRegistration();
int init_result = initialize(&bmi_model->handle, bmi_init_config.c_str());
Expand Down Expand Up @@ -805,7 +805,7 @@ namespace models {

private:
/** Pointer to backing BMI model instance. */
std::shared_ptr<Bmi_Fortran_Handle_Wrapper> bmi_model = nullptr;
std::unique_ptr<Bmi_Fortran_Handle_Wrapper> bmi_model = nullptr;

};
}
Expand Down
4 changes: 2 additions & 2 deletions include/bmi/Bmi_Py_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ namespace models {
// This is a class object for the BMI module Python class
py::object bmi_py_class = utils::ngenPy::InterpreterUtil::getPyModule(moduleComponents);
// This is the actual backing model object
bmi_model = std::make_shared<py::object>(bmi_py_class());
bmi_model = std::make_unique<py::object>(bmi_py_class());
bmi_model->attr("initialize")(bmi_init_config);
}
catch (std::runtime_error& e){ //Catch specific exception types so the type/message don't get erased
Expand Down Expand Up @@ -773,7 +773,7 @@ namespace models {

protected:
/** Pointer to backing BMI model instance. */
std::shared_ptr<py::object> bmi_model = nullptr;
std::unique_ptr<py::object> bmi_model = nullptr;

};

Expand Down

0 comments on commit e1d2bef

Please sign in to comment.