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
+11 -11
View File
@@ -122,43 +122,43 @@ SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
}
const SenseApiDesc &libApiDesc = func(salmanoffCallbacks);
auto lib = std::make_unique<SenseApiLib>(
auto lib = std::make_shared<SenseApiLib>(
libraryPath, dlopen_handle.release(), func);
lib->setSenseApiDesc(libApiDesc);
senseApiLibs.push_back(std::move(lib));
senseApiLibs.push_back(lib);
return *senseApiLibs.back();
}
std::optional<std::reference_wrapper<SenseApiLib>>
std::optional<std::shared_ptr<SenseApiLib>>
SenseApiManager::getSenseApiLib(const std::string& libraryPath)
{
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;
}
);
if (it != senseApiLibs.end()) { return **it; }
if (it != senseApiLibs.end()) { return *it; }
return std::nullopt;
}
std::optional<std::reference_wrapper<SenseApiLib>>
std::optional<std::shared_ptr<SenseApiLib>>
SenseApiManager::getSenseApiLibByApiName(const std::string& apiName)
{
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;
}
);
if (it != senseApiLibs.end()) { return **it; }
if (it != senseApiLibs.end()) { return *it; }
return std::nullopt;
}
void SenseApiManager::unloadSenseApiLib(const std::string& libraryPath)
{
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;
}
);
@@ -242,7 +242,7 @@ void SenseApiManager::attachSenseDevice(
std::string(__func__) + ": No library found for API '"
+ spec->api + "'");
}
auto& lib = libOpt.value().get();
auto& lib = *libOpt.value();
if (!lib.senseApiDesc.sal_mgmt_libOps.attachDeviceReq)
{
throw std::runtime_error(
@@ -263,7 +263,7 @@ void SenseApiManager::detachSenseDevice(
std::string(__func__) + ": No library found for API '"
+ spec->api + "'");
}
auto& lib = libOpt.value().get();
auto& lib = *libOpt.value();
if (!lib.senseApiDesc.sal_mgmt_libOps.detachDeviceReq)
{
throw std::runtime_error(