IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
interfaces.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/expected.hxx>
6#include <ice/stringid.hxx>
7#include <concepts>
8
9namespace ice
10{
11
12 namespace concepts
13 {
14
15 template<typename T>
16 concept InterfaceType = requires(T t) {
17 { ice::clear_type_t<T>::InterfaceID } -> std::convertible_to<ice::StringID const>;
18 };
19
20 } // namespace concepts
21
22 namespace detail
23 {
24
25 template<ice::concepts::InterfaceType... Interfaces>
27 {
28 using InterfacePointers = std::tuple<Interfaces*...>;
29
30 if constexpr (sizeof...(Interfaces) > 0)
31 {
32 ice::StringID const identifiers[]{
33 Interfaces::InterfaceID...
34 };
35
36 void* pointers[]{
37 static_cast<Interfaces*>(ptr)...
38 };
39
40 ice::u32 idx = 0;
41 for (ice::StringID_Arg iface_id : identifiers)
42 {
43 if (iface_id == id)
44 {
45 return pointers[idx];
46 }
47 idx += 1;
48 }
49 }
50
51 return ice::E_Fail;
52 }
53
54 } // namespace detail
55
57 {
58 virtual ~InterfaceSelector() noexcept = default;
59
60 virtual auto query_interface(ice::StringID_Arg id) noexcept -> ice::Expected<void*>
61 {
62 return ice::E_Fail;
63 }
64
65 template<typename T>
66 auto query_interface(T*& out_interface) noexcept -> ice::Result
67 {
68 ice::Expected<void*> const result = this->query_interface(T::InterfaceID);
69 if (result.succeeded())
70 {
71 out_interface = reinterpret_cast<T*>(result.value());
72 return S_Ok;
73 }
74 return result.error();
75 }
76 };
77
78 template<typename Derived, ice::concepts::InterfaceType... Interfaces>
80 {
82 {
83 return ice::detail::interface_select_helper<Interfaces...>(static_cast<Derived*>(this), id);
84 }
85 };
86
87 template<ice::concepts::InterfaceType... Interfaces>
88 struct Implements : public InterfaceSelector, public Interfaces...
89 {
91 {
92 return ice::detail::interface_select_helper<Interfaces...>(this, id);
93 }
94 };
95
96} // namespace ice
Definition expected.hxx:16
auto error() const noexcept -> ErrorType
Definition expected.hxx:122
bool succeeded() const noexcept
Definition expected.hxx:112
auto value(this Self &&self) noexcept -> auto &&
Definition expected.hxx:116
Definition interfaces.hxx:16
Definition container_concepts.hxx:12
Definition hashmap_details.hxx:13
auto interface_select_helper(auto *ptr, ice::StringID_Arg id) noexcept -> ice::Expected< void * >
Definition interfaces.hxx:26
SPDX-License-Identifier: MIT.
Definition array.hxx:12
static constexpr ice::ErrorCodeError E_Fail
Definition error_codes.hxx:13
std::remove_pointer_t< std::remove_reference_t< std::remove_cv_t< T > > > clear_type_t
Definition base.hxx:64
std::conditional_t< ice::build::Constant_StringID_DebugInfoEnabled, StringID const &, StringID > StringID_Arg
Argument type used to pass ice::StringID values to functions.
Definition stringid.hxx:23
BaseStringID< ice::build::Constant_StringID_DebugInfoEnabled > StringID
\copy ice::BaseStringID.
Definition stringid.hxx:15
std::uint32_t u32
Definition types.hxx:26
static constexpr ice::ErrorCodeSuccess S_Ok
Definition error_codes.hxx:10
ice::Expected< ice::ErrorCode > Result
Definition expected.hxx:197
Definition interfaces.hxx:89
auto query_interface(ice::StringID_Arg id) noexcept -> ice::Expected< void * > override
Definition interfaces.hxx:90
Definition interfaces.hxx:57
virtual ~InterfaceSelector() noexcept=default
virtual auto query_interface(ice::StringID_Arg id) noexcept -> ice::Expected< void * >
Definition interfaces.hxx:60
auto query_interface(T *&out_interface) noexcept -> ice::Result
Definition interfaces.hxx:66
Definition interfaces.hxx:80
auto query_interface(ice::StringID_Arg id) noexcept -> ice::Expected< void * > override
Definition interfaces.hxx:81