VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
PendingCommit.cpp
Go to the documentation of this file.
3#include <utility>
4
5namespace VoltMod
6{
7
8// Resetting an entry drops its timer subscription, so the commit never runs.
10
11void PendingCommit::Hold(int slot, int index, std::function<void()> commit)
12{
13 if (!IsValidSlot(slot) || !commit)
14 {
15 return;
16 }
17
18 // Commit the previous row before holding a different one. Holding the same row again only
19 // restarts its delay.
20 if (_entries[slot].Index != index)
21 {
22 Apply(slot);
23 }
24
25 Entry& entry = _entries[slot];
26 entry.Index = index;
27 entry.Commit = std::move(commit);
28 entry.Timer = _timer ? _timer(DelayMs, [this, slot] { Apply(slot); }) : Subscription{};
29}
30
32{
33 return IsValidSlot(slot) ? _entries[slot].Index : -1;
34}
35
36bool PendingCommit::IsPending(int slot, int index) const
37{
38 return index >= 0 && Index(slot) == index;
39}
40
42{
43 if (!IsValidSlot(slot))
44 {
45 return;
46 }
47
48 // Remove the entry before running it. The callback may replace it or change the menu.
49 Entry taken = std::move(_entries[slot]);
50 _entries[slot] = Entry{};
51
52 if (taken.Commit)
53 {
54 taken.Commit();
55 }
56}
57
59{
60 if (IsValidSlot(slot))
61 {
62 _entries[slot] = Entry{};
63 }
64}
65
66} // namespace VoltMod
bool IsPending(int slot, int index) const
PendingCommit(Timer timer, SlotEvents &slots)
static constexpr int64_t DelayMs
void Hold(int slot, int index, std::function< void()> commit)
int Index(int slot) const
std::function< Subscription(int64_t delayMs, std::function< void()> callback)> Timer
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Owns one registration and releases it on destruction.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13