事件系统

[English]

API 参考

公共头文件: #include "brookesia/service_manager/event/definition.hpp"

Header File

事件注册表

公共头文件: #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.

参数

event_schema -- [in] Schema to add.

返回

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.

参数

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.

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

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

返回

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.

参数

event_name -- [in] Event name to query.

返回

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.

返回

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

boost::json::array get_schemas_json()

Export all registered event schemas as JSON.

返回

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

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

Check whether an event schema exists.

参数

event_name -- [in] Event name to check.

返回

true if the event is registered.

inline size_t get_count()

Get the number of registered event schemas.

返回

size_t Count of registered events.

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

Get the in-process signal associated with an event.

参数

event_name -- [in] Event name to query.

返回

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