aboutsummaryrefslogtreecommitdiff
path: root/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-07-24 10:35:34 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-07-24 10:35:34 +0200
commiteeffe2ad97710ec7265de5ab020989c5731059c9 (patch)
treefa173631ea4fbcc017b92c29edd9b349577f54ce /kapi
parentef124ea26ebe98f16e0e8fb204ec1f419bf656ee (diff)
downloadkernel-eeffe2ad97710ec7265de5ab020989c5731059c9.tar.xz
kernel-eeffe2ad97710ec7265de5ab020989c5731059c9.zip
kapi: generalize device interface resolution
Diffstat (limited to 'kapi')
-rw-r--r--kapi/kapi/devices/device.hpp18
-rw-r--r--kapi/kapi/devices/interface_registry.hpp29
2 files changed, 30 insertions, 17 deletions
diff --git a/kapi/kapi/devices/device.hpp b/kapi/kapi/devices/device.hpp
index c26a4701..2c799d5d 100644
--- a/kapi/kapi/devices/device.hpp
+++ b/kapi/kapi/devices/device.hpp
@@ -40,22 +40,28 @@ namespace kapi::devices
*/
virtual ~device() = default;
+ [[nodiscard]] auto as(interface interface) noexcept -> void *;
+
+ [[nodiscard]] auto as(interface interface) const noexcept -> void const *;
+
template<typename InterfaceType>
- [[nodiscard]] constexpr auto as() -> InterfaceType *
+ [[nodiscard]] auto as() -> InterfaceType *
{
- return static_cast<InterfaceType *>(query_interface(InterfaceType::id));
+ return static_cast<InterfaceType *>(as(InterfaceType::id));
}
template<typename InterfaceType>
- [[nodiscard]] constexpr auto as() const noexcept -> InterfaceType const *
+ [[nodiscard]] auto as() const noexcept -> InterfaceType const *
{
- return static_cast<InterfaceType const *>(const_cast<device *>(this)->query_interface(InterfaceType::id));
+ return static_cast<InterfaceType const *>(as(InterfaceType::id));
}
+ [[nodiscard]] auto is_a(interface interface) const noexcept -> bool;
+
template<typename InterfaceType>
- [[nodiscard]] constexpr auto is_a() const noexcept -> bool
+ [[nodiscard]] auto is_a() const noexcept -> bool
{
- return const_cast<device *>(this)->query_interface(InterfaceType::id) != nullptr;
+ return is_a(InterfaceType::id);
}
/**
diff --git a/kapi/kapi/devices/interface_registry.hpp b/kapi/kapi/devices/interface_registry.hpp
index b1dfa08e..155a23cd 100644
--- a/kapi/kapi/devices/interface_registry.hpp
+++ b/kapi/kapi/devices/interface_registry.hpp
@@ -114,29 +114,36 @@ namespace kapi::devices
//! @param interface The interface for which to get the implementing devices.
[[nodiscard]] auto all(interface interface) const -> kstd::vector<entry>;
- //! Find an entry for a given interface on a device with the given name.
+ //! Attempt to resolve a given device, by name, to a given interface implementation
//!
//! @param interface The interface to look for.
//! @param name The stable name of the device to look for.
- //! @return An entry representing the published interface if it exists, an error otherwise.
- [[nodiscard]] auto find(interface interface, std::string_view name) const -> kstd::result<entry>;
+ [[nodiscard]] auto resolve(interface interface, std::string_view name) -> void *;
- //! Find an entry for a given interface on a specific device.
+ //! Attempt to resolve a given device to a given interface implementation
//!
//! @param interface The interface to look for.
//! @param device The device to look for.
- //! @return An entry representing the published interface if it exists, an error otherwise.
- [[nodiscard]] auto find(interface interface, device const & device) const -> kstd::result<entry>;
+ [[nodiscard]] auto resolve(interface interface, device & device) -> void *;
- //! Find an entry for a given interface on a device with the given name.
+ //! Attempt to resolve a given device, by name, to a given interface implementation
//!
- //! @tparam Interface The interface type to look for.
+ //! @tparam Interface The interface to look for.
//! @param name The stable name of the device to look for.
- //! @return An entry representing the published interface if it exists, an error otherwise.
template<typename Interface>
- [[nodiscard]] auto find(std::string_view name) const -> kstd::result<entry>
+ [[nodiscard]] auto resolve(std::string_view name) -> Interface *
{
- return find(Interface::id, name);
+ return static_cast<Interface *>(resolve(Interface::id, name));
+ }
+
+ //! Attempt to resolve a given device to a given interface implementation
+ //!
+ //! @tparam Interface The interface to look for.
+ //! @param device The device to look for.
+ template<typename Interface>
+ [[nodiscard]] auto resolve(device & device) -> Interface *
+ {
+ return static_cast<Interface *>(resolve(Interface::id, device));
}
private: