From da5d678f2a33e692b68b9f950221e9248ca65ea9 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 24 Feb 2022 20:03:26 +0200 Subject: [PATCH] Kernel: Add DeviceManagement::try_for_each() for fallible iteration This API will allow users to short circuit iteration and properly propagate errors. --- Kernel/Devices/DeviceManagement.cpp | 9 +++++++++ Kernel/Devices/DeviceManagement.h | 1 + 2 files changed, 10 insertions(+) diff --git a/Kernel/Devices/DeviceManagement.cpp b/Kernel/Devices/DeviceManagement.cpp index af824674545..134f67c8479 100644 --- a/Kernel/Devices/DeviceManagement.cpp +++ b/Kernel/Devices/DeviceManagement.cpp @@ -109,6 +109,15 @@ void DeviceManagement::for_each(Function callback) }); } +ErrorOr DeviceManagement::try_for_each(Function(Device&)> callback) +{ + return m_devices.with([&](auto& map) -> ErrorOr { + 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 e23f86ce923..0b4f57ee7bc 100644 --- a/Kernel/Devices/DeviceManagement.h +++ b/Kernel/Devices/DeviceManagement.h @@ -44,6 +44,7 @@ public: void before_device_removal(Badge, Device&); void for_each(Function); + ErrorOr try_for_each(Function(Device&)>); Device* get_device(MajorNumber major, MinorNumber minor); NullDevice const& null_device() const;