summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-02-24 20:03:26 +0200
committerAndreas Kling <kling@serenityos.org>2022-02-27 20:37:57 +0100
commitda5d678f2a33e692b68b9f950221e9248ca65ea9 (patch)
treecc56ed7335e16084e77884916c031f07dcd628ef
parent064b93c2ad51bbbf6dee3284ef37b7134d955d5f (diff)
downloadserenity-da5d678f2a33e692b68b9f950221e9248ca65ea9.zip
Kernel: Add DeviceManagement::try_for_each() for fallible iteration
This API will allow users to short circuit iteration and properly propagate errors.
-rw-r--r--Kernel/Devices/DeviceManagement.cpp9
-rw-r--r--Kernel/Devices/DeviceManagement.h1
2 files changed, 10 insertions, 0 deletions
diff --git a/Kernel/Devices/DeviceManagement.cpp b/Kernel/Devices/DeviceManagement.cpp
index af82467454..134f67c847 100644
--- a/Kernel/Devices/DeviceManagement.cpp
+++ b/Kernel/Devices/DeviceManagement.cpp
@@ -109,6 +109,15 @@ void DeviceManagement::for_each(Function<void(Device&)> callback)
});
}
+ErrorOr<void> DeviceManagement::try_for_each(Function<ErrorOr<void>(Device&)> callback)
+{
+ return m_devices.with([&](auto& map) -> ErrorOr<void> {
+ for (auto& entry : map)
+ TRY(callback(*entry.value));
+ return {};
+ });
+}
+
NullDevice& DeviceManagement::null_device()
{
return *m_null_device;
diff --git a/Kernel/Devices/DeviceManagement.h b/Kernel/Devices/DeviceManagement.h
index e23f86ce92..0b4f57ee7b 100644
--- a/Kernel/Devices/DeviceManagement.h
+++ b/Kernel/Devices/DeviceManagement.h
@@ -44,6 +44,7 @@ public:
void before_device_removal(Badge<Device>, Device&);
void for_each(Function<void(Device&)>);
+ ErrorOr<void> try_for_each(Function<ErrorOr<void>(Device&)>);
Device* get_device(MajorNumber major, MinorNumber minor);
NullDevice const& null_device() const;