Toast Engine
Loading...
Searching...
No Matches
control_box.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <atomic>
12#include <compare>
13#include <cstdint>
14#include <functional>
15#include <toast/export.hpp>
16
17namespace toast {
18class Node;
19
20namespace _detail {
21struct WorldTestAccess;
22
33struct TOAST_API ControlBox {
34 std::atomic<unsigned int> ref_count;
35 Node* node = nullptr;
36
38 ControlBox(Node* n);
39
40 ControlBox(const ControlBox&) = delete;
41 ControlBox(ControlBox&&) = delete;
42 auto operator=(const ControlBox&) -> ControlBox& = delete;
43 auto operator=(ControlBox&&) -> ControlBox& = delete;
44
46 explicit operator bool() const noexcept;
47
49 auto operator<=>(const ControlBox& other) const noexcept -> std::strong_ordering;
50 auto operator==(const ControlBox& other) const noexcept -> bool;
51
57 [[nodiscard]]
58 auto rid() const noexcept -> std::uintptr_t;
60 void increment();
62 void decrement();
63
65 static auto get(const Node* node) -> ControlBox*;
67 static auto get(const Node& node) -> ControlBox*;
68};
69
70}
71}
72
73template<>
74struct std::hash<toast::_detail::ControlBox> {
75 auto operator()(const toast::_detail::ControlBox& a) const noexcept -> std::size_t {
76 return std::hash<std::uintptr_t> {}(a.rid());
77 }
78};
Definition node.hpp:81
Member-robber pattern used by the generated reflection code.
Definition defer.hpp:16
Reference-count control block embedded inside the Node allocation.
Definition control_box.hpp:33
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
void increment()
relaxed acquire; acquiring a Box is always preceded by an acquire elsewhere
Definition control_box.cpp:29
Node * node
null once the node is destroyed
Definition control_box.hpp:35
void decrement()
acq_rel release; ensures the last releaser sees all prior writes before decrementing
Definition control_box.cpp:33
auto rid() const noexcept -> std::uintptr_t
Stable numeric identity for this ControlBox.
Definition control_box.cpp:25
std::atomic< unsigned int > ref_count
starts at 1 (the node's self-reference); node is freed when it hits 0
Definition control_box.hpp:34
ControlBox(Node *n)
Initializes ref_count to 1 and stores the node pointer; called by nodeAllocation().
Definition control_box.cpp:7
Definition world_test_access.hpp:14