Toast Engine
Toggle main menu visibility
Loading...
Searching...
No Matches
box.inl
1
#pragma once
2
#include "
box.hpp
"
3
#include "
toast/log.hpp
"
4
5
namespace
toast {
6
7
template
<
typename
T>
8
Box<T>::~Box() noexcept {
9
if
(control) {
10
control->decrement();
11
}
12
}
13
14
template
<
typename
T>
15
Box<T>::Box
(
_detail::ControlBox
* control_box)
noexcept
{
16
if
(control_box) {
17
this->control = control_box;
18
control->increment();
19
}
20
}
21
22
template
<
typename
T>
23
Box<T>::Box
(
const
T* node)
noexcept
{
24
_detail::ControlBox
* control_box =
_detail::ControlBox::get
(node);
25
if
(control_box) {
26
control = control_box;
27
control->
increment
();
28
}
29
}
30
31
template
<
typename
T>
32
Box<T>::Box
(
const
T& node)
noexcept
{
33
_detail::ControlBox
* control_box =
_detail::ControlBox::get
(node);
34
if
(control_box) {
35
control = control_box;
36
control->
increment
();
37
}
38
}
39
40
template
<
typename
T>
41
Box<T>::Box
(
const
Box& other)
noexcept
{
42
_detail::ControlBox
* control_box = other.control;
43
if
(control_box) {
44
control = control_box;
45
control->
increment
();
46
}
47
}
48
49
template
<
typename
T>
50
auto
Box<T>::operator=
(
const
Box& other)
noexcept
-> Box& {
51
if
(this->control == other.control) {
52
return
*
this
;
53
}
54
if
(control) {
55
control->decrement();
56
}
57
control = other.control;
58
if
(control) {
59
control->increment();
60
}
61
return
*
this
;
62
}
63
64
template
<
typename
T>
65
Box<T>::Box
(Box&& other)
noexcept
{
66
control = other.control;
67
other.control =
nullptr
;
68
}
69
70
template
<
typename
T>
71
auto
Box<T>::operator=
(Box&& other)
noexcept
-> Box& {
72
if
(this->control == other.control) {
73
return
*
this
;
74
}
75
if
(control) {
76
control->decrement();
77
}
78
control = other.control;
79
other.control =
nullptr
;
80
return
*
this
;
81
}
82
83
template
<
typename
T>
84
template
<
typename
U>
85
requires
std::convertible_to<U*, T*>
86
Box<T>::Box
(
const
Box<U>& other)
noexcept
{
87
_detail::ControlBox
* control_box = other.control;
88
if
(control_box) {
89
control = control_box;
90
control->
increment
();
91
}
92
}
93
94
template
<
typename
T>
95
template
<
typename
U>
96
requires
std::convertible_to<U*, T*>
97
auto
Box<T>::operator=
(
const
Box<U>& other)
noexcept
-> Box& {
98
if
(this->control == other.control) {
99
return
*
this
;
100
}
101
if
(control) {
102
control->decrement();
103
}
104
control = other.control;
105
if
(control) {
106
control->increment();
107
}
108
return
*
this
;
109
}
110
111
template
<
typename
T>
112
template
<
typename
U>
113
requires
std::convertible_to<U*, T*>
114
Box<T>::Box
(Box<U>&& other)
noexcept
{
115
control = other.control;
116
other.control =
nullptr
;
117
}
118
119
template
<
typename
T>
120
template
<
typename
U>
121
requires
std::convertible_to<U*, T*>
122
auto
Box<T>::operator=
(Box<U>&& other)
noexcept
-> Box& {
123
if
(this->control == other.control) {
124
return
*
this
;
125
}
126
if
(control) {
127
control->decrement();
128
}
129
control = other.control;
130
other.control =
nullptr
;
131
return
*
this
;
132
}
133
134
template
<
typename
T>
135
auto
Box<T>::exists
() const noexcept ->
bool
{
136
return
(control !=
nullptr
) && (control->node !=
nullptr
);
137
}
138
139
template
<
typename
T>
140
auto
Box<T>::operator<=>(
const
Box
& other)
const
noexcept
-> std::strong_ordering {
141
if
(this->control && other.control) {
142
return
*this->control <=> *other.control;
143
}
144
return
this->control <=> other.control;
145
}
146
147
template
<
typename
T>
148
auto
Box<T>::operator==(
const
Box
& other)
const
noexcept
->
bool
{
149
return
(*this <=> other) == std::strong_ordering::equal;
150
}
151
152
template
<
typename
T>
153
Box<T>::operator
bool() const noexcept {
154
return
this->exists();
155
}
156
157
template
<
typename
T>
158
Box<T>::operator
T&()
noexcept
{
159
TOAST_ASSERT(control && control->node,
"Box"
,
"Box is in a Invalid State: ControlBox or Node is nullptr"
);
160
return
static_cast<
T&
>
(*control->node);
161
}
162
163
template
<
typename
T>
164
auto
Box<T>::operator->() noexcept -> T* {
165
TOAST_ASSERT(control && control->node,
"Box"
,
"Box is in a Invalid State: ControlBox or Node is nullptr"
);
166
return
static_cast<
T*
>
(control->node);
167
}
168
169
template
<
typename
T>
170
auto
Box<T>::operator*() noexcept -> T& {
171
TOAST_ASSERT(control && control->node,
"Box"
,
"Box is in a Invalid State: ControlBox or Node is nullptr"
);
172
return
static_cast<
T&
>
(*control->node);
173
}
174
175
template
<
typename
T>
176
Box<T>::operator
const
T&()
const
noexcept
{
177
TOAST_ASSERT(control && control->node,
"Box"
,
"Box is in a Invalid State: ControlBox or Node is nullptr"
);
178
return
static_cast<
T&
>
(*control->node);
179
}
180
181
template
<
typename
T>
182
auto
Box<T>::operator->() const noexcept -> const T* {
183
TOAST_ASSERT(control && control->node,
"Box"
,
"Box is in a Invalid State: ControlBox or Node is nullptr"
);
184
return
static_cast<
T*
>
(control->node);
185
}
186
187
template
<
typename
T>
188
auto
Box<T>::operator*() const noexcept -> const T& {
189
TOAST_ASSERT(control && control->node,
"Box"
,
"Box is in a Invalid State: ControlBox or Node is nullptr"
);
190
return
static_cast<
T&
>
(*control->node);
191
}
192
193
template
<
typename
T>
194
template
<
typename
U>
195
requires
std::derived_from<U, T>
196
auto
Box<T>::as
() const noexcept -> Box<U> {
197
if
(control && control->node) {
198
if
(
dynamic_cast<
const
U*
>
(control->node)) {
199
return
Box<U>(control);
200
}
201
}
202
return
Box<U>();
203
}
204
205
template
<
typename
T>
206
auto
Box<T>::rid
() const noexcept ->
size_t
{
207
return
control->rid();
208
}
209
}
box.hpp
pseudo shared pointer for Nodes
toast::Box
Intrusive reference-counted handle to a Node.
Definition
box.hpp:35
toast::Box::Box
Box() noexcept=default
Default-constructs an empty (null) box; does not allocate anything.
toast::Box::operator=
auto operator=(const Box &other) noexcept -> Box &
Shares ownership with other; increments ref count.
Definition
box.inl:50
toast::Box::as
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
toast::Box::rid
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
toast::Box::exists
auto exists() const noexcept -> bool
Whether this box holds a live node.
Definition
box.inl:135
log.hpp
Public logging interface for the engine.
toast::_detail::ControlBox
Reference-count control block embedded inside the Node allocation.
Definition
control_box.hpp:33
toast::_detail::ControlBox::get
static auto get(const Node *node) -> ControlBox *
Recovers the ControlBox from a raw node pointer; the ControlBox sits immediately before the Node in m...
Definition
control_box.cpp:37
toast::_detail::ControlBox::increment
void increment()
relaxed acquire; acquiring a Box is always preceded by an acquire elsewhere
Definition
control_box.cpp:29
engine
src
toast
world
box.inl
Generated by
1.17.0