UCILoader 1.1.2
Small C++ library that allows user to connect to a chess engines via UCI protocol.
Loading...
Searching...
No Matches
UCILoader::EventEmitter Class Reference

Base class for objects that emit engine events. More...

#include <EngineEvent.h>

Inheritance diagram for UCILoader::EventEmitter:
UCILoader::EngineInstance< Move >

Public Member Functions

void unlink (const EventReceiver *receiver)
 Disconnect a specific receiver from future events.
 
void connect (std::shared_ptr< EventReceiver > receiver)
 Connect a custom event receiver.
 
void connect (std::function< void(const EngineEvent *)> callback, uint32_t eventFilter)
 Connect a callback function that receives the full event.
 
void connect (std::function< void()> callback, uint32_t eventFilter)
 Connect a simple parameterless callback function.
 

Protected Member Functions

virtual ~EventEmitter ()
 Virtual destructor ensuring proper cleanup.
 
void emit (const EngineEvent *event)
 Emit an event to all connected receivers.
 

Detailed Description

Base class for objects that emit engine events.

EventEmitter manages a collection of EventReceiver instances and notifies them when events occur. Multiple receivers can be connected and will all be notified synchronously when an event is emitted.

Thread Safety: Emitter uses internal locking to protect receiver list during additions/removals. However, event delivery is synchronous - receivers are called from the thread that calls emit().

Usage Pattern:

EventEmitter emitter;
// Connect with lambda
emitter.connect(
[](const EngineEvent* e) {
std::cout << "Event type: " << e->getType() << std::endl;
},
NamedEngineEvents::SearchCompleted
);
// Connect custom receiver
auto receiver = std::make_shared<MyEventReceiver>();
emitter.connect(receiver);
Base class for events emitted by an EngineInstance.
Definition: EngineEvent.h:42
virtual uint32_t getType() const =0
Get the type code of this event.
Base class for objects that emit engine events.
Definition: EngineEvent.h:494
void connect(std::shared_ptr< EventReceiver > receiver)
Connect a custom event receiver.
Definition: EngineEvent.cpp:59
See also
EventReceiver for implementing custom receivers
EngineInstance for a real-world user of EventEmitter

Member Function Documentation

◆ connect() [1/3]

void UCILoader::EventEmitter::connect ( std::function< void()>  callback,
uint32_t  eventFilter 
)

Connect a simple parameterless callback function.

Parameters
callbackFunction invoked for each matching event (takes no parameters)
eventFilterBitmask of NamedEngineEvents constants indicating which events to trigger the callback

Similar to the full event callback version, but for callbacks that don't need event details. Useful for simple notifications where you just need to know an event occurred.

Example:

engine->connect(
[]() { std::cout << "Search done!" << std::endl; },
NamedEngineEvents::SearchCompleted
);

◆ connect() [2/3]

void UCILoader::EventEmitter::connect ( std::function< void(const EngineEvent *)>  callback,
uint32_t  eventFilter 
)

Connect a callback function that receives the full event.

Parameters
callbackFunction invoked for each matching event (receives const EngineEvent*)
eventFilterBitmask of NamedEngineEvents constants indicating which events to receive

Wraps the callback in a FunctionCallbackEventReceiver and connects it. Useful for simple event handling without creating a custom EventReceiver class.

Example:

engine->connect(
[](const EngineEvent* e) {
std::cout << "Event type: " << e->getType() << std::endl;
},
NamedEngineEvents::SearchCompleted | NamedEngineEvents::EngineCrashed
);

◆ connect() [3/3]

void UCILoader::EventEmitter::connect ( std::shared_ptr< EventReceiver receiver)

Connect a custom event receiver.

Parameters
receiverShared pointer to EventReceiver to connect

Registers a custom EventReceiver to receive events. The emitter maintains a shared_ptr to the receiver, ensuring it remains alive as long as connected.

The first event sent will be EmitterConnected with no payload.

See EventReceiver documentation for implementing custom receivers.

◆ emit()

void UCILoader::EventEmitter::emit ( const EngineEvent event)
protected

Emit an event to all connected receivers.

Parameters
eventThe EngineEvent to deliver

Synchronously delivers the event to all receivers whose eventFilter() includes the event type. The delivery happens in the calling thread. Receivers are called in the order they were connected.

This method is typically called only by subclasses (e.g., EngineInstance).

◆ unlink()

void UCILoader::EventEmitter::unlink ( const EventReceiver receiver)

Disconnect a specific receiver from future events.

Parameters
receiverPointer to the EventReceiver to disconnect

Safely removes the receiver from the emitter's receiver list. The receiver is not deleted (caller retains ownership). Safe to call even if receiver is not connected.


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