IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
core
public
ice
os
handle.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/base.hxx
>
6
#include <concepts>
7
8
namespace
ice::os
9
{
10
11
// TODO: Make it into a StringID, because it can be arbitrary...
12
enum class
HandleType
:
ice::u8
13
{
14
File
,
15
DynLib
,
16
17
// TODO: Remove
18
#if ISP_ANDROID
19
JClass,
20
JObject,
21
JString,
22
#endif
23
};
24
25
template
<HandleType HType>
26
struct
HandleDescriptor
;
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>
37
concept
ManagedHandleDescriptor
=
ValidHandleDescriptor<HType>
&&
requires
{
38
std::is_same_v<typename HandleDescriptor<HType>::HandleManagerType,
void
> ==
false
;
39
};
40
41
template
<HandleType HType,
bool
= false>
requires
(
ValidHandleDescriptor<HType>
)
42
struct
HandleInternal
43
{
44
using
NativeType
=
typename
HandleDescriptor<HType>::PlatformHandleType
;
45
using
ManagerType
=
struct
{};
46
47
NativeType
_handle
;
48
};
49
50
template
<HandleType HType>
requires
(
ManagedHandleDescriptor<HType>
)
51
struct
HandleInternal<HType, true>
52
{
53
using
NativeType
=
typename
HandleDescriptor<HType>::PlatformHandleType
;
54
using
ManagerType
=
typename
HandleDescriptor<HType>::HandleManagerType
;
55
56
NativeType
_handle
;
57
ManagerType
_manager
;
58
};
59
60
template
<HandleType HType>
requires
(
ValidHandleDescriptor<HType>
)
61
struct
Handle
;
62
63
template
<HandleType HType>
requires
(
ValidHandleDescriptor<HType>
)
64
struct
Handle
final
65
{
66
using
NativeDescriptor
=
HandleDescriptor<HType>
;
67
using
NativeInternal
=
HandleInternal<HType, ManagedHandleDescriptor<HType>
>;
68
using
NativeType
=
typename
NativeInternal::NativeType
;
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>)
104
Handle<HType>::Handle
(
NativeType
value)
noexcept
requires
(
ManagedHandleDescriptor<HType>
==
false
)
105
: _internal{ value }
106
{
107
}
108
109
template
<HandleType HType>
requires
(
ValidHandleDescriptor<HType>
)
110
Handle<HType>::Handle
(
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>
)
119
Handle<HType>::Handle
(
Handle
&& other) noexcept
120
: _internal{ ice::exchange(other._internal, { NativeDescriptor::InvalidHandle }) }
121
{
122
}
123
124
template
<HandleType HType>
requires
(ValidHandleDescriptor<HType>)
125
Handle<HType>
::~
Handle
() noexcept
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
{
146
if
constexpr
(
ManagedHandleDescriptor<HType>
)
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
base.hxx
ice::os::ManagedHandleDescriptor
Definition
handle.hxx:37
ice::os::ValidHandleDescriptor
Definition
handle.hxx:29
ice::os
Definition
handle.hxx:9
ice::os::HandleType
HandleType
Definition
handle.hxx:13
ice::os::HandleType::File
@ File
Definition
handle.hxx:14
ice::os::HandleType::DynLib
@ DynLib
Definition
handle.hxx:15
ice::u8
std::uint8_t u8
Definition
types.hxx:24
ice::os::HandleDescriptor
Definition
handle.hxx:26
ice::os::Handle
Definition
handle.hxx:65
ice::os::Handle::NativeType
typename NativeInternal::NativeType NativeType
Definition
handle.hxx:68
ice::os::Handle::close
bool close() noexcept
Definition
handle.hxx:142
ice::os::Handle::Handle
Handle() noexcept
Definition
handle.hxx:98
ice::os::Handle::NativeDescriptor
HandleDescriptor< HType > NativeDescriptor
Definition
handle.hxx:66
ice::os::Handle::NativeInternal
HandleInternal< HType, ManagedHandleDescriptor< HType > > NativeInternal
Definition
handle.hxx:67
ice::os::Handle::native
auto native() const noexcept -> NativeType
Definition
handle.hxx:90
ice::os::HandleInternal< HType, true >::NativeType
typename HandleDescriptor< HType >::PlatformHandleType NativeType
Definition
handle.hxx:53
ice::os::HandleInternal< HType, true >::ManagerType
typename HandleDescriptor< HType >::HandleManagerType ManagerType
Definition
handle.hxx:54
ice::os::HandleInternal< HType, true >::_handle
NativeType _handle
Definition
handle.hxx:56
ice::os::HandleInternal< HType, true >::_manager
ManagerType _manager
Definition
handle.hxx:57
ice::os::HandleInternal
Definition
handle.hxx:43
ice::os::HandleInternal::NativeType
typename HandleDescriptor< HType >::PlatformHandleType NativeType
Definition
handle.hxx:44
ice::os::HandleInternal::ManagerType
struct {} ManagerType
Definition
handle.hxx:45
ice::os::HandleInternal< HType, ManagedHandleDescriptor< HType > >::_handle
NativeType _handle
Definition
handle.hxx:47
Generated by
1.18.0