C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
native_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 <compare>
26#include <concepts>
27#include <cstdint>
28
29namespace essence {
30 template <typename T>
31 concept handle_stroage_type = std::integral<T> && sizeof(T) >= sizeof(std::uintptr_t);
32
33 template <auto Callable, typename T>
34 concept handle_validator_type = requires {
35 requires std::integral<T>;
36 { Callable } -> std::predicate<T>;
37 };
38
39 template <std::integral T>
40 T make_pointer_number(const void* pointer) noexcept {
41 return static_cast<T>(reinterpret_cast<std::uintptr_t>(pointer));
42 }
43
50 template <std::integral T>
51 constexpr bool is_valid_handle_value(T value) noexcept {
52 return value != 0 && value != -1;
53 }
54
61 template <handle_stroage_type T = std::uintptr_t, typename Mapped = T,
62 auto Validator = &is_valid_handle_value<Mapped>>
63 requires handle_validator_type<Validator, Mapped>
65 public:
66 constexpr basic_native_handle(std::nullptr_t = nullptr) noexcept : value_{} {} // NOLINT(*-explicit-constructor)
67
73 template <typename U>
74 requires std::is_pointer_v<U>
75 basic_native_handle(U value) noexcept : value_{make_pointer_number<T>(value)} {} // NOLINT(*-explicit-constructor)
76
81 constexpr basic_native_handle(Mapped value) noexcept : value_{static_cast<T>(value)} {} // NOLINT(*-explicit-constructor)
82
83 constexpr bool operator==(const basic_native_handle&) const noexcept = default;
84 constexpr auto operator<=>(const basic_native_handle&) const noexcept = default;
85
90 explicit constexpr operator bool() const noexcept {
91 return Validator(static_cast<Mapped>(value_));
92 }
93
98 template <typename U>
99 requires std::is_pointer_v<U>
100 operator U() const noexcept { // NOLINT(*-explicit-constructor)
101 return reinterpret_cast<U>(value_);
102 }
103
107 constexpr operator Mapped() const noexcept { // NOLINT(*-explicit-constructor)
108 return static_cast<Mapped>(value_);
109 }
110
111 private:
112 T value_;
113 };
114
115 using native_handle = basic_native_handle<>;
116} // namespace essence
Stores a platform-dependent handle.
Definition native_handle.hpp:64
basic_native_handle(U value) noexcept
Constructs the object from a pointer.
Definition native_handle.hpp:75
constexpr basic_native_handle(Mapped value) noexcept
Constructs the object from a mapped number.
Definition native_handle.hpp:81
Definition native_handle.hpp:31
Definition native_handle.hpp:34