Base module

Signal< RT(Args...), MutexT >

Thread-safe signal and slot implementation for callback-based event dispatch.

#include <icy/signal.h>

template<typename RT, typename... Args, typename MutexT>
class Signal< RT(Args...), MutexT >

Defined in src/base/include/icy/signal.h:142

Public Methods

ReturnNameDescription
intattach const inlineConnects a lambda or std::function to the signal.
intattach const inlineConnects a pre-constructed SlotPtr to the signal.
booldetach const inlineDetaches the slot with the given ID.
booldetach const inlineDetaches all slots associated with the given instance pointer.
booldetach const inlineDetaches the slot whose delegate compares equal to other->delegate.
voiddetachAll const inlineDetaches and destroys all currently attached slots.
RTemit virtual inlineEmits the signal, invoking all live attached slots in priority order.
std::vector< SlotPtr >slots const inlineReturns a snapshot copy of the current slot list.
size_tnslots const inlineReturns the number of slots currently registered with this signal.
intoperator+= inlineAttaches a function; equivalent to attach(func).
intoperator+= inlineAttaches a pre-constructed slot; equivalent to attach(slot).
booloperator-= inlineDetaches the slot with the given ID; equivalent to detach(id).
booloperator-= inlineDetaches all slots for the given instance; equivalent to detach(instance).
booloperator-= inlineDetaches the slot matching slot's delegate; equivalent to detach(slot).
SignalDefaulted constructor.
~Signal inline
Signal inlineCopy constructor; copies the slot list and last-assigned ID from r.
Signal &operator= inlineCopy assignment operator; copies the slot list and last-assigned ID from r.

attach

const inline

inline int attach(Function const & func, void * instance = nullptr, int id = -1, int priority = -1) const

Defined in src/base/include/icy/signal.h:157

Connects a lambda or std::function to the signal.

Returns

The assigned slot ID, which can be passed to detach() to disconnect.

Parameters

ParameterTypeDescription
funcFunction const &The callable to invoke when the signal is emitted.
instancevoid *Optional owner pointer used for instance-based detach; pass nullptr if not applicable.
idintExplicit slot ID to assign; pass -1 to auto-assign.
priorityintHigher values are called first; pass -1 for default ordering.

attach

const inline

inline int attach(SlotPtr slot) const

Defined in src/base/include/icy/signal.h:171

Connects a pre-constructed SlotPtr to the signal.

Duplicate slots (matched by delegate equality) are removed before insertion. Slots are kept sorted in descending priority order after insertion.

Returns

The assigned slot ID, which can be passed to detach() to disconnect.

Parameters

ParameterTypeDescription
slotSlotPtrThe slot to attach. Must have a valid delegate.

Exceptions

ExceptionDescription
std::logic_errorif slot->id is set explicitly and already in use.

detach

const inline

inline bool detach(int id) const

Defined in src/base/include/icy/signal.h:202

Detaches the slot with the given ID.

Safe to call from within a slot's callback (the slot is marked dead before erasure).

Returns

true if a slot was found and removed; false if the ID was not found.

Parameters

ParameterTypeDescription
idintThe slot ID returned by attach().

detach

const inline

inline bool detach(const void * instance) const

Defined in src/base/include/icy/signal.h:219

Detaches all slots associated with the given instance pointer.

Useful for bulk disconnect when an object is destroyed. Matches slots by their stored instance pointer, not by delegate equality.

Returns

true if at least one slot was removed; false otherwise.

Parameters

ParameterTypeDescription
instanceconst void *The owner pointer used when the slots were attached.

detach

const inline

inline bool detach(SlotPtr other) const

Defined in src/base/include/icy/signal.h:236

Detaches the slot whose delegate compares equal to other->delegate.

Used by the slot() helper overloads and operator-= to disconnect a specific class-member or function binding by value.

Returns

true if a matching slot was found and removed; false otherwise.

Parameters

ParameterTypeDescription
otherSlotPtrA slot whose delegate is compared against attached slots.

detachAll

const inline

inline void detachAll() const

Defined in src/base/include/icy/signal.h:249

Detaches and destroys all currently attached slots.

Each slot is marked dead before removal. After this call nslots() returns 0.


emit

virtual inline

virtual inline RT emit(Args... args)

Defined in src/base/include/icy/signal.h:283

Emits the signal, invoking all live attached slots in priority order.

For Signal<bool(...)>: iterates slots and returns true as soon as any slot returns true, stopping further propagation. Returns false if no slot handled the event.

For Signal<void(...)>: calls every live slot unconditionally.

Local Signal<...> traverses the stable slot list directly without any snapshot allocation. ThreadSignal<...> snapshots raw node pointers under a shared lock, then invokes delegates without holding the lock. Dead slots are swept after the outermost emission returns, allowing attach/detach inside callbacks without copying shared_ptrs on the hot path.

Returns

For bool return type: true if any slot handled the event, false otherwise. For void return type: nothing.

Parameters

ParameterTypeDescription
argsArgs...Arguments forwarded to each connected slot.

slots

const inline

inline std::vector< SlotPtr > slots() const

Defined in src/base/include/icy/signal.h:350

Returns a snapshot copy of the current slot list.

The copy is taken under a shared lock, so it is safe to call concurrently with attach/detach operations. Only currently live slots are returned.

Returns

A vector of SlotPtr representing all currently registered slots.


nslots

const inline

inline size_t nslots() const

Defined in src/base/include/icy/signal.h:368

Returns the number of slots currently registered with this signal.

The count is taken under a shared lock and tracks only currently live slots. Dead-but-not-yet-swept nodes are excluded.

Returns

The number of entries in the internal slot list.


operator+=

inline

inline int operator+=(Function const & func)

Defined in src/base/include/icy/signal.h:375

Attaches a function; equivalent to attach(func).

Returns

Assigned slot ID.


operator+=

inline

inline int operator+=(SlotPtr slot)

Defined in src/base/include/icy/signal.h:377

Attaches a pre-constructed slot; equivalent to attach(slot).

Returns

Assigned slot ID.


operator-=

inline

inline bool operator-=(int id)

Defined in src/base/include/icy/signal.h:379

Detaches the slot with the given ID; equivalent to detach(id).

Returns

true if removed.


operator-=

inline

inline bool operator-=(const void * instance)

Defined in src/base/include/icy/signal.h:381

Detaches all slots for the given instance; equivalent to detach(instance).

Returns

true if any removed.


operator-=

inline

inline bool operator-=(SlotPtr slot)

Defined in src/base/include/icy/signal.h:383

Detaches the slot matching slot's delegate; equivalent to detach(slot).

Returns

true if removed.


Signal

Signal() = default

Defined in src/base/include/icy/signal.h:385

Defaulted constructor.


~Signal

inline

inline ~Signal()

Defined in src/base/include/icy/signal.h:387


Signal

inline

inline Signal(const Signal & r)

Defined in src/base/include/icy/signal.h:394

Copy constructor; copies the slot list and last-assigned ID from r.

Parameters

ParameterTypeDescription
rconst Signal &The signal to copy from.

operator=

inline

inline Signal & operator=(const Signal & r)

Defined in src/base/include/icy/signal.h:403

Copy assignment operator; copies the slot list and last-assigned ID from r.

Returns

Reference to this signal.

Parameters

ParameterTypeDescription
rconst Signal &The signal to copy from.

Public Types

NameDescription
Function
SlotPtr
Slot
SlotNode

Function

using Function = std::function< RT(Args...)>

Defined in src/base/include/icy/signal.h:145


SlotPtr

using SlotPtr = std::shared_ptr< internal::Slot< RT, Args... > >

Defined in src/base/include/icy/signal.h:146


Slot

using Slot = internal::Slot< RT, Args... >

Defined in src/base/include/icy/signal.h:147


SlotNode

using SlotNode = internal::SlotNode< RT, Args... >

Defined in src/base/include/icy/signal.h:148

Private Attributes

ReturnNameDescription
MutexT_mutex
SlotNode *_head
SlotNode *_tail
size_t_liveCount
int_lastId
EmitDepth_emitDepth
SweepFlag_needsSweep

_mutex

MutexT _mutex

Defined in src/base/include/icy/signal.h:625


SlotNode * _head = nullptr

Type: SlotNode *

Defined in src/base/include/icy/signal.h:626


_tail

SlotNode * _tail = nullptr

Type: SlotNode *

Defined in src/base/include/icy/signal.h:627


_liveCount

size_t _liveCount = 0

Defined in src/base/include/icy/signal.h:628


_lastId

int _lastId = 0

Defined in src/base/include/icy/signal.h:629


_emitDepth

EmitDepth _emitDepth {}

Defined in src/base/include/icy/signal.h:630


_needsSweep

SweepFlag _needsSweep {}

Defined in src/base/include/icy/signal.h:631

Private Methods

ReturnNameDescription
size_tkillMatchingLocked const inline
voidinsertSlotLocked const inline
voidappendSlotLocked const inline
voideraseNodeLocked const inline
size_temitDepthLocked const inline
voidbeginEmitLocked const inline
boolendEmitLocked const inline
voidrequestSweepLocked const inline
boolconsumeSweepRequest const inline
voidsweepLocked const inline
voidfinishEmit inline
voidclearNodesLocked const inline
voidresetEmitState inline
std::pair< std::vector< SlotPtr >, int >snapshotState const inline
voidrestoreState inline

killMatchingLocked

const inline

template<typename Matcher> inline size_t killMatchingLocked(Matcher && matcher, bool removeAll) const

Defined in src/base/include/icy/signal.h:418


insertSlotLocked

const inline

inline void insertSlotLocked(SlotPtr slot) const

Defined in src/base/include/icy/signal.h:444


appendSlotLocked

const inline

inline void appendSlotLocked(SlotPtr slot) const

Defined in src/base/include/icy/signal.h:471


eraseNodeLocked

const inline

inline void eraseNodeLocked(SlotNode * node) const

Defined in src/base/include/icy/signal.h:483


emitDepthLocked

const inline

inline size_t emitDepthLocked() const

Defined in src/base/include/icy/signal.h:498


beginEmitLocked

const inline

inline void beginEmitLocked() const

Defined in src/base/include/icy/signal.h:506


endEmitLocked

const inline

inline bool endEmitLocked() const

Defined in src/base/include/icy/signal.h:514


requestSweepLocked

const inline

inline void requestSweepLocked() const

Defined in src/base/include/icy/signal.h:522


consumeSweepRequest

const inline

inline bool consumeSweepRequest() const

Defined in src/base/include/icy/signal.h:530


sweepLocked

const inline

inline void sweepLocked() const

Defined in src/base/include/icy/signal.h:541


finishEmit

inline

inline void finishEmit()

Defined in src/base/include/icy/signal.h:551


clearNodesLocked

const inline

inline void clearNodesLocked() const

Defined in src/base/include/icy/signal.h:580


resetEmitState

inline

inline void resetEmitState()

Defined in src/base/include/icy/signal.h:593


snapshotState

const inline

inline std::pair< std::vector< SlotPtr >, int > snapshotState() const

Defined in src/base/include/icy/signal.h:604


restoreState

inline

inline void restoreState(std::vector< SlotPtr > snapshot, int lastId)

Defined in src/base/include/icy/signal.h:616

Private Static Attributes

ReturnNameDescription
constexpr boolthreadSafe static constexpr

threadSafe

static constexpr

constexpr bool threadSafe = !std::is_same_v<MutexT, NullSharedMutex>

Defined in src/base/include/icy/signal.h:413