VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Subscriptions.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <utility>
5#include <vector>
6
7namespace VoltMod
8{
9
10/**
11 * @brief Holds the subscriptions an object makes and releases them together, newest first.
12 *
13 * @code
14 * _subs.Add(runtime.Slots.Changed += [this](int slot) { Forget(slot); });
15 * _subs.Add(runtime.GameEvents.On<PlayerSpawn>([this](const PlayerSpawn& e) { Spawn(e.Slot); }));
16 * @endcode
17 *
18 * Declare it after whatever its handlers touch, the same as a single Subscription. A handler that
19 * has to be dropped on its own still wants its own Subscription member.
20 */
22{
23public:
24 Subscriptions() = default;
26
28 /** Releases what this held before taking @p other's. */
30 {
31 if (this != &other)
32 {
33 Clear();
34 _items = std::move(other._items);
35 }
36 return *this;
37 }
38
39 Subscriptions(const Subscriptions&) = delete;
41
42 /** Keep @p subscription for as long as this lives. */
43 void Add(Subscription subscription) { _items.push_back(std::move(subscription)); }
44
45 /** Release everything now, newest first. */
46 void Clear()
47 {
48 while (!_items.empty())
49 {
50 _items.pop_back();
51 }
52 }
53
54 /** True while this holds nothing - also how a caller asks "have I subscribed yet?" for a
55 * registration deferred to first use. */
56 bool Empty() const noexcept { return _items.empty(); }
57
58private:
59 std::vector<Subscription> _items;
60};
61
62} // namespace VoltMod
Owns one registration and releases it on destruction.
Holds the subscriptions an object makes and releases them together, newest first.
void Add(Subscription subscription)
Subscriptions(Subscriptions &&) noexcept=default
Subscriptions & operator=(const Subscriptions &)=delete
bool Empty() const noexcept
Subscriptions(const Subscriptions &)=delete
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56