VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
VoltMod::Event< Args > Class Template Reference

A multicast signal with a fixed handler signature: the one way to subscribe in VoltMod. More...

#include <Event.hpp>

Public Types

using Handler = std::function< void(Args...)>
 
using Lifecycle = EventLifecycle
 

Public Member Functions

 Event ()=default
 
 Event (Lifecycle lifecycle)
 
 Event (const Event &)=delete
 
Event & operator= (const Event &)=delete
 
Subscription operator+= (Handler handler)
 
bool Empty () const noexcept
 
size_t Count () const noexcept
 
void Raise (Args... args)
 

Detailed Description

template<class... Args>
class VoltMod::Event< Args >

A multicast signal with a fixed handler signature: the one way to subscribe in VoltMod.

The owner declares it as a public member and is the only caller of Raise. Everyone else adds a handler with += and keeps the returned Subscription beside the state that handler captured:

_spawnSub = runtime.Slots.Changed += [this](int slot) { _cache.Reset(slot); };
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56

A handler never runs after its Subscription drops, not even from inside a Raise already under way.

Lifetime. The Subscription points at the event, so the event has to outlive it. Declaring the subscription in the object that owns the handler state gives that for free; storing it above the service it points at does not.

Lazy install. An event whose source costs something to run - a vtable hook, an engine-wide callback - takes an EventLifecycle. Subscribing starts the source and dropping the last subscription stops it; nothing else installs it. An engine hook, alone or feeding several events, is a LazyHook.

Not copyable or movable: subscriptions point at one address for their whole life.

Definition at line 53 of file Event.hpp.

Member Typedef Documentation

◆ Handler

template<class... Args>
using VoltMod::Event< Args >::Handler = std::function<void(Args...)>

Definition at line 56 of file Event.hpp.

◆ Lifecycle

template<class... Args>
using VoltMod::Event< Args >::Lifecycle = EventLifecycle

EventLifecycle, reachable as Event<...>::Lifecycle where that reads better.

Definition at line 58 of file Event.hpp.

Constructor & Destructor Documentation

◆ Event() [1/3]

template<class... Args>
VoltMod::Event< Args >::Event ( )
default

◆ Event() [2/3]

template<class... Args>
VoltMod::Event< Args >::Event ( Lifecycle  lifecycle)
inlineexplicit

Definition at line 61 of file Event.hpp.

◆ Event() [3/3]

template<class... Args>
VoltMod::Event< Args >::Event ( const Event< Args > &  )
delete

Member Function Documentation

◆ Count()

template<class... Args>
size_t VoltMod::Event< Args >::Count ( ) const
inlinenoexcept

Definition at line 90 of file Event.hpp.

References VoltMod::CallbackRegistry< T >::Size().

◆ Empty()

template<class... Args>
bool VoltMod::Event< Args >::Empty ( ) const
inlinenoexcept

Definition at line 89 of file Event.hpp.

References VoltMod::CallbackRegistry< T >::Empty().

◆ operator+=()

template<class... Args>
Subscription VoltMod::Event< Args >::operator+= ( Handler  handler)
inline

Subscribe handler for as long as the returned Subscription lives. An empty Subscription means nothing was stored: handler was empty, or a Lifecycle refused.

Definition at line 68 of file Event.hpp.

References VoltMod::CallbackRegistry< T >::Add(), VoltMod::CallbackRegistry< T >::Empty(), VoltMod::EventLifecycle::OnFirst, VoltMod::EventLifecycle::OnLast, VoltMod::ReadFile(), and VoltMod::CallbackRegistry< T >::Remove().

◆ operator=()

template<class... Args>
Event & VoltMod::Event< Args >::operator= ( const Event< Args > &  )
delete

◆ Raise()

template<class... Args>
void VoltMod::Event< Args >::Raise ( Args...  args)
inline

Invoke every handler. The owner raises; a consumer with a += subscription does not.

Re-entrancy safe: a handler may subscribe, unsubscribe another, or drop its own Subscription while this runs. One added during the raise first fires on the next one.

Definition at line 98 of file Event.hpp.

References VoltMod::CallbackRegistry< T >::Dispatch(), and VoltMod::ReadFile().

Referenced by VoltMod::PlayerManager::OnClientFullyConnected(), VoltMod::SlotEvents::Raise(), and VoltMod::MenuStack::Step().


The documentation for this class was generated from the following file: