Topaz 5.0
Topaz Game Engine
Loading...
Searching...
No Matches
handle.hpp
1#ifndef TZ_DATA_HANDLE_HPP
2#define TZ_DATA_HANDLE_HPP
3#include <cstdint>
4#include <utility>
5#include <limits>
6
7namespace tz
8{
9 enum class hanval : std::uint64_t{};
10
11 struct nullhand_t
12 {
13 bool operator==(std::integral auto num) const
14 {
15 return std::cmp_equal(num, std::numeric_limits<std::underlying_type_t<hanval>>::max());
16 }
17 };
22 constexpr nullhand_t nullhand;
23
30 template<typename T>
31 class handle
32 {
33 public:
34 handle(hanval v):
35 value(v){}
36
37 handle([[maybe_unused]] nullhand_t nh = {}):
38 value(static_cast<hanval>(std::numeric_limits<std::underlying_type_t<hanval>>::max())){}
39
40 explicit operator hanval() const
41 {
42 return this->value;
43 }
44
46 std::underlying_type_t<hanval> peek() const
47 {
48 return static_cast<std::underlying_type_t<hanval>>(this->value);
49 }
50
52 handle& operator=(hanval value)
53 {
54 this->value = value;
55 return *this;
56 }
57
59 bool operator==(nullhand_t) const
60 {
61 return handle<T>{nullhand}.value == this->value;
62 }
64 bool operator!=(nullhand_t) const
65 {
66 return handle<T>{nullhand}.value != this->value;
67 }
68
69 bool operator==(const handle<T>& rhs) const = default;
70 bool operator!=(const handle<T>& rhs) const = default;
71 private:
72 hanval value;
73 };
74}
75
76#endif // TZ_DATA_HANDLE_HPP
Represents a generic opaque handle.
Definition handle.hpp:32
handle & operator=(hanval value)
Assign a handle to the null handle, meaning it is now invalid.
Definition handle.hpp:52
std::underlying_type_t< hanval > peek() const
Disregard the concept of "opaqueness" of an opaque handle by peeking at the underlying value....
Definition handle.hpp:46
bool operator!=(nullhand_t) const
Compare a handle to the null handle. This comparison will return true if the handle is valid.
Definition handle.hpp:64
bool operator==(nullhand_t) const
Compare a handle to the null handle. This comparison will return false if the handle is valid.
Definition handle.hpp:59
constexpr nullhand_t nullhand
Definition handle.hpp:22