IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
handle.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/base.hxx>
6#include <concepts>
7
8namespace ice::os
9{
10
11 // TODO: Make it into a StringID, because it can be arbitrary...
12 enum class HandleType : ice::u8
13 {
16
17 // TODO: Remove
18#if ISP_ANDROID
19 JClass,
20 JObject,
21 JString,
22#endif
23 };
24
25 template<HandleType HType>
27
28 template<HandleType HType>
29 concept ValidHandleDescriptor = requires {
30 std::is_same_v<typename HandleDescriptor<HType>::PlatformHandleType, void> == false;
31 { HandleDescriptor<HType>::InvalidHandle } -> std::convertible_to<typename HandleDescriptor<HType>::PlatformHandleType>;
32 { HandleDescriptor<HType>::is_valid(HandleDescriptor<HType>::InvalidHandle, nullptr) } -> std::convertible_to<bool>;
33 { HandleDescriptor<HType>::close(HandleDescriptor<HType>::InvalidHandle, nullptr) } -> std::convertible_to<bool>;
34 };
35
36 template<HandleType HType>
38 std::is_same_v<typename HandleDescriptor<HType>::HandleManagerType, void> == false;
39 };
40
41 template<HandleType HType, bool = false> requires (ValidHandleDescriptor<HType>)
49
50 template<HandleType HType> requires (ManagedHandleDescriptor<HType>)
59
60 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
61 struct Handle;
62
63 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
64 struct Handle final
65 {
69
70 Handle() noexcept;
71 explicit Handle(NativeType value) noexcept requires(ManagedHandleDescriptor<HType> == false);
72 explicit Handle(typename NativeInternal::ManagerType manager, NativeType value) noexcept requires(ManagedHandleDescriptor<HType> == true);
73 Handle(Handle&& other) noexcept;
74 Handle(Handle const& other) noexcept = delete;
75 ~Handle() noexcept;
76
77 auto operator=(Handle&& other) noexcept -> Handle&;
78 auto operator=(Handle const& other) noexcept -> Handle& = delete;
79
80 operator bool() const noexcept requires(ManagedHandleDescriptor<HType> == false)
81 {
82 return NativeDescriptor::is_valid(_internal._handle, nullptr);
83 }
84
85 operator bool() const noexcept requires(ManagedHandleDescriptor<HType> == true)
86 {
87 return NativeDescriptor::is_valid(_internal._handle, _internal._manager);
88 }
89
90 auto native() const noexcept -> NativeType { return _internal._handle; }
91 bool close() noexcept;
92
93 private:
94 NativeInternal _internal;
95 };
96
97 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
98 Handle<HType>::Handle() noexcept
99 : _internal{ NativeDescriptor::InvalidHandle }
100 {
101 }
102
103 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
105 : _internal{ value }
106 {
107 }
108
109 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
111 typename NativeInternal::ManagerType manager,
112 NativeType value
113 ) noexcept requires(ManagedHandleDescriptor<HType> == true)
114 : _internal{ value, manager }
115 {
116 }
117
118 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
120 : _internal{ ice::exchange(other._internal, { NativeDescriptor::InvalidHandle }) }
121 {
122 }
123
124 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
126 {
127 close();
128 }
129
130 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
131 auto Handle<HType>::operator=(Handle&& other) noexcept -> Handle&
132 {
133 if (this != &other)
134 {
135 close();
136 _internal = ice::exchange(other._internal, { NativeDescriptor::InvalidHandle });
137 }
138 return *this;
139 }
140
141 template<HandleType HType> requires (ValidHandleDescriptor<HType>)
142 bool Handle<HType>::close() noexcept
143 {
144 if (*this)
145 {
147 {
148 auto const temp = ice::exchange(_internal, { NativeDescriptor::InvalidHandle });
149 return NativeDescriptor::close(temp._handle, temp._manager);
150 }
151 else
152 {
153 return NativeDescriptor::close(ice::exchange(_internal._handle, NativeDescriptor::InvalidHandle), nullptr);
154 }
155 }
156 return false;
157 }
158
159} // namespace ice::os
Definition handle.hxx:37
Definition handle.hxx:29
Definition handle.hxx:9
HandleType
Definition handle.hxx:13
@ File
Definition handle.hxx:14
@ DynLib
Definition handle.hxx:15
std::uint8_t u8
Definition types.hxx:24
Definition handle.hxx:26
Definition handle.hxx:65
typename NativeInternal::NativeType NativeType
Definition handle.hxx:68
bool close() noexcept
Definition handle.hxx:142
Handle() noexcept
Definition handle.hxx:98
HandleDescriptor< HType > NativeDescriptor
Definition handle.hxx:66
HandleInternal< HType, ManagedHandleDescriptor< HType > > NativeInternal
Definition handle.hxx:67
auto native() const noexcept -> NativeType
Definition handle.hxx:90
typename HandleDescriptor< HType >::PlatformHandleType NativeType
Definition handle.hxx:53
typename HandleDescriptor< HType >::HandleManagerType ManagerType
Definition handle.hxx:54
NativeType _handle
Definition handle.hxx:56
ManagerType _manager
Definition handle.hxx:57
Definition handle.hxx:43
typename HandleDescriptor< HType >::PlatformHandleType NativeType
Definition handle.hxx:44
struct {} ManagerType
Definition handle.hxx:45