Make senseApiLibs a vector<sh_ptr>; getters return sh_ptr

Proper reference and object lifetime management.
This commit is contained in:
2025-08-29 13:19:33 -04:00
parent e024ccdf95
commit b9ca38bff1
2 changed files with 14 additions and 14 deletions
+3 -3
View File
@@ -23,9 +23,9 @@ public:
} }
SenseApiLib& loadSenseApiLib(const std::string& libraryPath); SenseApiLib& loadSenseApiLib(const std::string& libraryPath);
std::optional<std::reference_wrapper<SenseApiLib>> getSenseApiLib( std::optional<std::shared_ptr<SenseApiLib>> getSenseApiLib(
const std::string& libraryPath); const std::string& libraryPath);
std::optional<std::reference_wrapper<SenseApiLib>> getSenseApiLibByApiName( std::optional<std::shared_ptr<SenseApiLib>> getSenseApiLibByApiName(
const std::string& apiName); const std::string& apiName);
void unloadSenseApiLib(const std::string& libraryPath); void unloadSenseApiLib(const std::string& libraryPath);
@@ -53,7 +53,7 @@ private:
SenseApiManager(const SenseApiManager&) = delete; SenseApiManager(const SenseApiManager&) = delete;
SenseApiManager& operator=(const SenseApiManager&) = delete; SenseApiManager& operator=(const SenseApiManager&) = delete;
std::vector<std::unique_ptr<SenseApiLib>> senseApiLibs; std::vector<std::shared_ptr<SenseApiLib>> senseApiLibs;
public: public:
static std::optional<std::string> searchForLibInSmoSearchPaths( static std::optional<std::string> searchForLibInSmoSearchPaths(
+11 -11
View File
@@ -122,43 +122,43 @@ SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
} }
const SenseApiDesc &libApiDesc = func(salmanoffCallbacks); const SenseApiDesc &libApiDesc = func(salmanoffCallbacks);
auto lib = std::make_unique<SenseApiLib>( auto lib = std::make_shared<SenseApiLib>(
libraryPath, dlopen_handle.release(), func); libraryPath, dlopen_handle.release(), func);
lib->setSenseApiDesc(libApiDesc); lib->setSenseApiDesc(libApiDesc);
senseApiLibs.push_back(std::move(lib)); senseApiLibs.push_back(lib);
return *senseApiLibs.back(); return *senseApiLibs.back();
} }
std::optional<std::reference_wrapper<SenseApiLib>> std::optional<std::shared_ptr<SenseApiLib>>
SenseApiManager::getSenseApiLib(const std::string& libraryPath) SenseApiManager::getSenseApiLib(const std::string& libraryPath)
{ {
auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(), auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(),
[&libPath = libraryPath](const std::unique_ptr<SenseApiLib>& lib) { [&libPath = libraryPath](const std::shared_ptr<SenseApiLib>& lib) {
return lib->libraryPath == libPath; return lib->libraryPath == libPath;
} }
); );
if (it != senseApiLibs.end()) { return **it; } if (it != senseApiLibs.end()) { return *it; }
return std::nullopt; return std::nullopt;
} }
std::optional<std::reference_wrapper<SenseApiLib>> std::optional<std::shared_ptr<SenseApiLib>>
SenseApiManager::getSenseApiLibByApiName(const std::string& apiName) SenseApiManager::getSenseApiLibByApiName(const std::string& apiName)
{ {
auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(), auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(),
[&apiName](const std::unique_ptr<SenseApiLib>& lib) { [&apiName](const std::shared_ptr<SenseApiLib>& lib) {
return lib->senseApiDesc.name == apiName; return lib->senseApiDesc.name == apiName;
} }
); );
if (it != senseApiLibs.end()) { return **it; } if (it != senseApiLibs.end()) { return *it; }
return std::nullopt; return std::nullopt;
} }
void SenseApiManager::unloadSenseApiLib(const std::string& libraryPath) void SenseApiManager::unloadSenseApiLib(const std::string& libraryPath)
{ {
auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(), auto it = std::find_if(senseApiLibs.begin(), senseApiLibs.end(),
[&lpath = libraryPath](const std::unique_ptr<SenseApiLib>& lib) { [&lpath = libraryPath](const std::shared_ptr<SenseApiLib>& lib) {
return lib->libraryPath == lpath; return lib->libraryPath == lpath;
} }
); );
@@ -242,7 +242,7 @@ void SenseApiManager::attachSenseDevice(
std::string(__func__) + ": No library found for API '" std::string(__func__) + ": No library found for API '"
+ spec->api + "'"); + spec->api + "'");
} }
auto& lib = libOpt.value().get(); auto& lib = *libOpt.value();
if (!lib.senseApiDesc.sal_mgmt_libOps.attachDeviceReq) if (!lib.senseApiDesc.sal_mgmt_libOps.attachDeviceReq)
{ {
throw std::runtime_error( throw std::runtime_error(
@@ -263,7 +263,7 @@ void SenseApiManager::detachSenseDevice(
std::string(__func__) + ": No library found for API '" std::string(__func__) + ": No library found for API '"
+ spec->api + "'"); + spec->api + "'");
} }
auto& lib = libOpt.value().get(); auto& lib = *libOpt.value();
if (!lib.senseApiDesc.sal_mgmt_libOps.detachDeviceReq) if (!lib.senseApiDesc.sal_mgmt_libOps.detachDeviceReq)
{ {
throw std::runtime_error( throw std::runtime_error(