C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
managed_handle.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "functional.hpp"
26#include "native_handle.hpp"
27
28#include <concepts>
29#include <cstdint>
30#include <memory>
31#include <type_traits>
32
33namespace essence {
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*>) );
38
47 template <bool Shared, auto Deleter, handle_stroage_type T = std::uintptr_t, typename Mapped = T,
48 auto Validator = &is_valid_handle_value<Mapped>>
51 public:
53 using value_type = std::conditional_t<Shared, std::shared_ptr<void>, std::unique_ptr<void, void (*)(void*)>>;
54
55 basic_managed_handle(std::nullptr_t = nullptr) noexcept // NOLINT(*-explicit-constructor)
56 : value_{nullptr, &basic_managed_handle::delete_handle} {}
57
63 template <typename U>
64 requires std::is_pointer_v<U>
65 explicit basic_managed_handle(U value)
66 : value_{reinterpret_cast<void*>(make_pointer_number<T>(value)), &basic_managed_handle::delete_handle} {}
67
72 explicit basic_managed_handle(Mapped value)
73 : value_{reinterpret_cast<void*>(static_cast<T>(value)), &basic_managed_handle::delete_handle} {}
74
76 basic_managed_handle(basic_managed_handle&&) noexcept = default;
77 basic_managed_handle& operator=(const basic_managed_handle&) = default;
78 basic_managed_handle& operator=(basic_managed_handle&&) noexcept = default;
79
84 explicit operator bool() const noexcept {
85 if (value_) {
86 return Validator(make_pointer_number<Mapped>(value_.get()));
87 }
88
89 return false;
90 }
91
96 native_type get() const noexcept {
97 return value_.get();
98 }
99
103 void reset() noexcept {
104 value_.reset();
105 }
106
112 template <typename U>
113 requires std::is_pointer_v<U>
114 void reset(U value) noexcept {
115 if constexpr (Shared) {
116 value_.reset(
117 reinterpret_cast<void*>(make_pointer_number<T>(value)), &basic_managed_handle::delete_handle);
118 } else {
119 value_.reset(reinterpret_cast<void*>(make_pointer_number<T>(value)));
120 }
121 }
122
127 void reset(Mapped value) noexcept {
128 if constexpr (Shared) {
129 value_.reset(reinterpret_cast<void*>(static_cast<T>(value)), &basic_managed_handle::delete_handle);
130 } else {
131 value_.reset(reinterpret_cast<void*>(static_cast<T>(value)));
132 }
133 }
134
139 void swap(basic_managed_handle& other) noexcept {
140 value_.swap(other.value_);
141 }
142
143 private:
144 static void delete_handle(void* handle) {
145 using arg_type = function_arg_type<decltype(Deleter)>;
146
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));
152 } else {
153 Deleter(static_cast<arg_type>(handle));
154 }
155 }
156 }
157
158 value_type value_;
159 };
160
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>;
164
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>;
168} // namespace essence
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