From eeffe2ad97710ec7265de5ab020989c5731059c9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 24 Jul 2026 10:35:34 +0200 Subject: kapi: generalize device interface resolution --- kapi/kapi/devices/device.hpp | 18 ++++++++++++------ kapi/kapi/devices/interface_registry.hpp | 29 ++++++++++++++++++----------- 2 files changed, 30 insertions(+), 17 deletions(-) (limited to 'kapi') 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 - [[nodiscard]] constexpr auto as() -> InterfaceType * + [[nodiscard]] auto as() -> InterfaceType * { - return static_cast(query_interface(InterfaceType::id)); + return static_cast(as(InterfaceType::id)); } template - [[nodiscard]] constexpr auto as() const noexcept -> InterfaceType const * + [[nodiscard]] auto as() const noexcept -> InterfaceType const * { - return static_cast(const_cast(this)->query_interface(InterfaceType::id)); + return static_cast(as(InterfaceType::id)); } + [[nodiscard]] auto is_a(interface interface) const noexcept -> bool; + template - [[nodiscard]] constexpr auto is_a() const noexcept -> bool + [[nodiscard]] auto is_a() const noexcept -> bool { - return const_cast(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; - //! 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; + [[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; + [[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 - [[nodiscard]] auto find(std::string_view name) const -> kstd::result + [[nodiscard]] auto resolve(std::string_view name) -> Interface * { - return find(Interface::id, name); + return static_cast(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 + [[nodiscard]] auto resolve(device & device) -> Interface * + { + return static_cast(resolve(Interface::id, device)); } private: -- cgit v1.2.3