Event System

[中文]

API Reference

Public header: #include "brookesia/service_manager/event/definition.hpp"

Header File

Event Registry

Public header: #include "brookesia/service_manager/event/registry.hpp"

Header File

Classes

class EventRegistry

Registry of event schemas and local subscriptions for one service.

Public Types

using Signal = esp_brookesia::lib_utils::signal<void(const std::string &event_name, const EventItemMap &event_items)>

Signal type used for local in-process event listeners.

using SignalConnection = esp_brookesia::lib_utils::scoped_connection

RAII connection handle returned by Signal::connect().

using SignalSlot = Signal::slot_type

Slot type accepted by subscribe_event().

Public Functions

bool add(EventSchema event_schema)

Register an event schema.

Parameters

event_schema -- [in] Schema to add.

Returns

true on success, false if an event with the same name already exists.

void remove(const std::string &event_name)

Remove a registered event schema.

Parameters

event_name -- [in] Name of the event to remove.

void remove_all()

Remove all registered event schemas.

bool validate_items(const std::string &event_name, const EventItemMap &event_items)

Validate an event payload against a registered schema.

Parameters
  • event_name -- [in] Event name to validate against.

  • event_items -- [in] Event payload to validate.

Returns

true if the payload conforms to the schema, false otherwise.

inline const EventSchema *get_schema(const std::string &event_name)

Look up the schema for a registered event.

Parameters

event_name -- [in] Event name to query.

Returns

const EventSchema* Pointer to the schema, or nullptr if not found.

std::vector<EventSchema> get_schemas() const

Get a snapshot of all registered event schemas.

Returns

std::vector<EventSchema> Copy of the registered schemas.

boost::json::array get_schemas_json()

Export all registered event schemas as JSON.

Returns

boost::json::array JSON representation of every schema.

inline bool has(const std::string &event_name)

Check whether an event schema exists.

Parameters

event_name -- [in] Event name to check.

Returns

true if the event is registered.

inline size_t get_count()

Get the number of registered event schemas.

Returns

size_t Count of registered events.

Signal *get_signal(const std::string &event_name)

Get the in-process signal associated with an event.

Parameters

event_name -- [in] Event name to query.

Returns

Signal* Pointer to the signal, or nullptr if the event does not exist.