25#include "functional.hpp"
26#include "native_handle.hpp"
34 template <
typename Callable,
typename T = function_arg_type<Callable>>
36 (std::integral<T> && std::invocable<Callable, T>)
37 || (std::is_pointer_v<T> && (std::invocable<Callable, T> || std::invocable<Callable, T*>) );
47 template <
bool Shared,
auto Deleter,
handle_stroage_type T = std::uintptr_t,
typename Mapped = T,
48 auto Validator = &is_valid_handle_value<Mapped>>
53 using value_type = std::conditional_t<Shared, std::shared_ptr<void>, std::unique_ptr<void, void (*)(
void*)>>;
56 : value_{
nullptr, &basic_managed_handle::delete_handle} {}
64 requires std::is_pointer_v<U>
66 : value_{reinterpret_cast<void*>(make_pointer_number<T>(value)), &
basic_managed_handle::delete_handle} {}
73 : value_{reinterpret_cast<void*>(static_cast<T>(value)), &
basic_managed_handle::delete_handle} {}
84 explicit operator
bool() const noexcept {
86 return Validator(make_pointer_number<Mapped>(value_.get()));
112 template <
typename U>
113 requires std::is_pointer_v<U>
115 if constexpr (Shared) {
117 reinterpret_cast<void*
>(make_pointer_number<T>(value)), &basic_managed_handle::delete_handle);
119 value_.reset(
reinterpret_cast<void*
>(make_pointer_number<T>(value)));
128 if constexpr (Shared) {
129 value_.reset(
reinterpret_cast<void*
>(
static_cast<T
>(value)), &basic_managed_handle::delete_handle);
131 value_.reset(
reinterpret_cast<void*
>(
static_cast<T
>(value)));
140 value_.swap(other.value_);
144 static void delete_handle(
void* handle) {
145 using arg_type = function_arg_type<
decltype(Deleter)>;
147 if (
auto mapped_value = make_pointer_number<Mapped>(handle); Validator(mapped_value)) {
148 if constexpr (std::integral<arg_type>) {
149 Deleter(mapped_value);
150 }
else if constexpr (std::is_pointer_v<std::remove_pointer_t<arg_type>>) {
151 Deleter(&
static_cast<std::remove_pointer_t<arg_type>&
>(handle));
153 Deleter(
static_cast<arg_type
>(handle));
161 template <
auto Deleter, handle_stroage_type T = std::uintptr_t,
typename Mapped = T,
162 auto Validator = &is_valid_handle_value<Mapped>>
163 using shared_handle = basic_managed_handle<true, Deleter, T, Mapped, Validator>;
165 template <
auto Deleter, handle_stroage_type T = std::uintptr_t,
typename Mapped = T,
166 auto Validator = &is_valid_handle_value<Mapped>>
167 using unique_handle = basic_managed_handle<false, Deleter, T, Mapped, Validator>;
Manages a platform-dependent handle.
Definition managed_handle.hpp:50
basic_managed_handle(Mapped value)
Constructs the object from a mapped number.
Definition managed_handle.hpp:72
void reset() noexcept
Deletes the handle and resets the value.
Definition managed_handle.hpp:103
void reset(U value) noexcept
Resets the object with a pointer.
Definition managed_handle.hpp:114
void swap(basic_managed_handle &other) noexcept
Swaps the handle value with another one.
Definition managed_handle.hpp:139
void reset(Mapped value) noexcept
Resets the object with a mapped number.
Definition managed_handle.hpp:127
native_type get() const noexcept
Gets the stored handle.
Definition managed_handle.hpp:96
basic_managed_handle(U value)
Constructs the object from a pointer.
Definition managed_handle.hpp:65
Stores a platform-dependent handle.
Definition native_handle.hpp:64
Definition managed_handle.hpp:35
Definition native_handle.hpp:31
Definition native_handle.hpp:34