Toast Engine
Loading...
Searching...
No Matches
box.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "control_box.hpp"
14
15#include <format>
16
17namespace toast {
18class Node;
19
34template<typename T>
35class Box {
36 template<typename U>
37 friend class Box;
38 friend struct _detail::ControlBox;
39 _detail::ControlBox* control = nullptr;
40
41public:
43 Box() noexcept = default;
44 ~Box() noexcept;
45
47 Box(std::nullptr_t) noexcept { }
48
50 Box(_detail::ControlBox* control_box) noexcept;
51
53 Box(const T* node) noexcept;
54
56 Box(const T& node) noexcept;
57
59 Box(const Box& other) noexcept;
61 auto operator=(const Box& other) noexcept -> Box&;
62
64 Box(Box&& other) noexcept;
66 auto operator=(Box&& other) noexcept -> Box&;
67
69 template<typename U>
70 requires std::convertible_to<U*, T*>
71 Box(const Box<U>& other) noexcept;
72
74 template<typename U>
75 requires std::convertible_to<U*, T*>
76 auto operator=(const Box<U>& other) noexcept -> Box&;
77
79 template<typename U>
80 requires std::convertible_to<U*, T*>
81 Box(Box<U>&& other) noexcept;
82
84 template<typename U>
85 requires std::convertible_to<U*, T*>
86 auto operator=(Box<U>&& other) noexcept -> Box&;
87
93 [[nodiscard]]
94 auto exists() const noexcept -> bool;
95
96 auto operator<=>(const Box& other) const noexcept -> std::strong_ordering;
97 auto operator==(const Box& other) const noexcept -> bool;
98 explicit operator bool() const noexcept;
99
100 operator T&() noexcept;
101 auto operator->() noexcept -> T*;
102 auto operator*() noexcept -> T&;
103
104 operator const T&() const noexcept;
105 auto operator->() const noexcept -> const T*;
106 auto operator*() const noexcept -> const T&;
107
109 template<typename U>
110 requires std::derived_from<U, T>
111 [[nodiscard]]
112 auto as() const noexcept -> Box<U>;
113
115 [[nodiscard]]
116 auto rid() const noexcept -> size_t;
117};
118
119}
120
121template<typename T>
122struct std::hash<toast::Box<T>> {
123 auto operator()(const toast::Box<T>& a) const noexcept -> std::size_t { return std::hash<std::uintptr_t> {}(a.rid()); }
124};
125
127template<typename T>
128struct std::formatter<toast::Box<T>> : std::formatter<std::string_view> {
129 auto format(const toast::Box<T>& p, std::format_context& ctx) const {
130 if (not p.exists()) {
131 return std::format_to(ctx.out(), "(empty node)");
132 }
133 return std::format_to(ctx.out(), "{} ({})", p->name(), p->uid());
134 }
135};
136
137#include "box.inl"
Intrusive reference-counted handle to a Node.
Definition box.hpp:35
Box() noexcept=default
Default-constructs an empty (null) box; does not allocate anything.
auto operator=(const Box &other) noexcept -> Box &
Shares ownership with other; increments ref count.
Definition box.inl:50
auto as() const noexcept -> Box< U >
Downcasts to a derived type; a null Box returns a null Box rather than throwing.
Definition box.inl:196
auto rid() const noexcept -> size_t
Raw identity of the underlying ControlBox as a pointer value; stable for the node's lifetime; used as...
Definition box.inl:206
auto exists() const noexcept -> bool
Whether this box holds a live node.
Definition box.inl:135
Definition node.hpp:81
Control block for the pseudo shared pointer Box<Node>.
Reference-count control block embedded inside the Node allocation.
Definition control_box.hpp:33