VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CallbackList.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6#include <vector>
7
8namespace VoltMod
9{
10
11/**
12 * @internal
13 * @brief One host event's callbacks, safe to edit from inside a dispatch.
14 *
15 * Unlike Core's CallbackRegistry: plain function pointers across the module boundary, ordered by
16 * plugin load position then subscription order, and a pass stops at the first that returns true.
17 * During a pass a removal only marks and an addition waits aside; both land when the outermost pass ends.
18 */
19template <class Fn>
21{
22public:
23 void Add(uint64_t token, uint64_t order, Fn callback, void* context)
24 {
25 const Entry entry{.Token = token, .Order = order, .Call = callback, .Context = context};
26 if (_depth > 0)
27 {
28 _pending.push_back(entry);
29 }
30 else
31 {
32 Insert(entry);
33 }
34 }
35
36 /** Whether nothing is subscribed, counting additions still waiting aside. */
37 bool Empty() const { return _pending.empty() && std::ranges::all_of(_entries, &Entry::Removed); }
38
39 /** Whether @p token was taken on this event. */
40 bool Remove(uint64_t token)
41 {
42 for (Entry& entry : _entries)
43 {
44 if (entry.Token != token || entry.Removed)
45 {
46 continue;
47 }
48
49 entry.Removed = true;
50 if (_depth == 0)
51 {
52 ApplyPending();
53 }
54 return true;
55 }
56
57 const auto waiting = std::ranges::find(_pending, token, &Entry::Token);
58 if (waiting == _pending.end())
59 {
60 return false;
61 }
62
63 _pending.erase(waiting);
64 return true;
65 }
66
67 /** Invoke @p visit(callback, context) until one returns true; returns whether one did. */
68 template <class Visit>
69 bool Dispatch(Visit&& visit)
70 {
71 return Walk([&](const Entry& entry) { return visit(entry.Call, entry.Context); });
72 }
73
74 /** Invoke @p visit(callback, context) over one plugin's callbacks: those added with @p order. */
75 template <class Visit>
76 void DispatchTo(uint64_t order, Visit&& visit)
77 {
78 Walk([&](const Entry& entry) {
79 if (entry.Order == order)
80 {
81 visit(entry.Call, entry.Context);
82 }
83 return false;
84 });
85 }
86
87private:
88 struct Entry
89 {
90 uint64_t Token = 0;
91 uint64_t Order = 0; ///< the owning plugin's load position
92 Fn Call = nullptr;
93 void* Context = nullptr;
94 bool Removed = false;
95 };
96
97 template <class Visit>
98 bool Walk(Visit&& visit)
99 {
100 ++_depth;
101 bool stopped = false;
102 // Nothing is inserted or erased while the depth is up, so the count and the references hold.
103 const size_t count = _entries.size();
104 for (size_t i = 0; i < count && !stopped; ++i)
105 {
106 const Entry& entry = _entries[i];
107 if (!entry.Removed)
108 {
109 stopped = visit(entry);
110 }
111 }
112 if (--_depth == 0)
113 {
114 ApplyPending();
115 }
116 return stopped;
117 }
118
119 void Insert(const Entry& entry)
120 {
121 const auto at = std::ranges::find_if(_entries, [&](const Entry& held) { return held.Order > entry.Order; });
122 _entries.insert(at, entry);
123 }
124
125 void ApplyPending()
126 {
127 std::erase_if(_entries, [](const Entry& entry) { return entry.Removed; });
128 for (const Entry& entry : _pending)
129 {
130 Insert(entry);
131 }
132 _pending.clear();
133 }
134
135 std::vector<Entry> _entries;
136 std::vector<Entry> _pending;
137 int _depth = 0;
138};
139
140} // namespace VoltMod
bool Remove(uint64_t token)
void DispatchTo(uint64_t order, Visit &&visit)
bool Dispatch(Visit &&visit)
void Add(uint64_t token, uint64_t order, Fn callback, void *context)