diff options
Diffstat (limited to 'kapi')
| -rw-r--r-- | kapi/kapi/devices/device.hpp | 18 | ||||
| -rw-r--r-- | kapi/kapi/devices/interface_registry.hpp | 29 |
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: |
