Lockable renamed to Lock (#219)
Also changed usage from unique_ptr to class value.
This commit is contained in:
committed by
GitHub
parent
2e86d4774b
commit
55bfb9fe3b
@@ -19,56 +19,55 @@ auto toVector(RangeType&& range) {
|
||||
}
|
||||
|
||||
void registerDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
if (scoped_mutex->lock()) {
|
||||
if (findDevice(device->getId()) == nullptr) {
|
||||
devices.push_back(device);
|
||||
TT_LOG_I(TAG, "Registered %s with id %lu", device->getName().c_str(), device->getId());
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Device %s with id %lu was already registered", device->getName().c_str(), device->getId());
|
||||
}
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
if (findDevice(device->getId()) == nullptr) {
|
||||
devices.push_back(device);
|
||||
TT_LOG_I(TAG, "Registered %s with id %lu", device->getName().c_str(), device->getId());
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Device %s with id %lu was already registered", device->getName().c_str(), device->getId());
|
||||
}
|
||||
}
|
||||
|
||||
void deregisterDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto id_to_remove = device->getId();
|
||||
if (scoped_mutex->lock()) {
|
||||
auto remove_iterator = std::remove_if(devices.begin(), devices.end(), [id_to_remove](const auto& device) {
|
||||
return device->getId() == id_to_remove;
|
||||
});
|
||||
if (remove_iterator != devices.end()) {
|
||||
TT_LOG_I(TAG, "Deregistering %s with id %lu", device->getName().c_str(), device->getId());
|
||||
devices.erase(remove_iterator);
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Deregistering %s with id %lu failed: not found", device->getName().c_str(), device->getId());
|
||||
}
|
||||
auto remove_iterator = std::remove_if(devices.begin(), devices.end(), [id_to_remove](const auto& device) {
|
||||
return device->getId() == id_to_remove;
|
||||
});
|
||||
if (remove_iterator != devices.end()) {
|
||||
TT_LOG_I(TAG, "Deregistering %s with id %lu", device->getName().c_str(), device->getId());
|
||||
devices.erase(remove_iterator);
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Deregistering %s with id %lu failed: not found", device->getName().c_str(), device->getId());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> findDevices(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
if (scoped_mutex->lock()) {
|
||||
auto devices_view = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
return toVector(devices_view);
|
||||
}
|
||||
return {};
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto devices_view = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
return toVector(devices_view);
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
if (scoped_mutex->lock()) {
|
||||
auto result_set = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
if (!result_set.empty()) {
|
||||
return result_set.front();
|
||||
}
|
||||
}
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
return nullptr;
|
||||
auto result_set = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
if (!result_set.empty()) {
|
||||
return result_set.front();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(std::string name) {
|
||||
|
||||
Reference in New Issue
Block a user