Skip to content

Engine → Core → event_bus.h

event_bus

Syntax

class CONJURE_API event_bus

Constructors

AccessSignatureDescription
publicevent_bus();

Important

Copy constructor and assignment has been deleted. Move constructors have been deleted. Move assignment has been deleted.

Destructors

AccessSignatureDescription
public~event_bus();

Variables

AccessNameTypeDefaultDescription
privatemChannelsstd::unordered_map<std::type_index, std::unique_ptr<channel_base>>{}
privatemQueuestd::vector<deferred_publish>{}
privatemQueueMutexmutable std::mutex{}

Functions

Public

clear_queue

void clear_queue();

Drops every queued event without publishing it. Thread-safe.

dispatch_queued

usize dispatch_queued();

Publishes every event enqueued before the call, in enqueue order, and returns how many were published (events whose type has no subscriber count too). Events enqueued while dispatching (by handlers or other threads) wait for the next call; a handler may call this re-entrantly to drain them early. Main thread only.

enqueue

template <typename Event> void enqueue(
    Event eventValue);

Stores a copy of the event for the next dispatch_queued(). Thread-safe.

publish

template <typename Event> void publish(
    const Event &eventValue);

Delivers the event to every subscriber of its type now, on the calling thread. Publishing a type nobody listens to does nothing.

queued_count

[[nodiscard]] usize queued_count() const;

Number of events waiting for dispatch_queued(). Thread-safe.

subscribe

template <typename Event> [[nodiscard]] event_subscription subscribe(
    std::function<void(const Event &)> handler);

Registers a handler for one event type. The subscription follows event semantics: RAII, move-only, inert once the bus is destroyed.

subscriber_count

template <typename Event> [[nodiscard]] usize subscriber_count() const noexcept;

Number of live subscriptions for one event type.

Private

channel_for

template <typename Event> [[nodiscard]] channel<Event> &channel_for();

find_channel

template <typename Event> [[nodiscard]] const channel<Event> *find_channel() const noexcept;