UCILoader 1.1.2
Small C++ library that allows user to connect to a chess engines via UCI protocol.
Loading...
Searching...
No Matches
EngineEvent.h
1#pragma once
2
3#include <cstdint>
4#include <mutex>
5#include <list>
6#include <memory>
7#include <functional>
8#include "UCI.h"
9
10
11namespace UCILoader {
12
43
44 public:
48 virtual ~EngineEvent() {};
49
72 virtual uint32_t getType() const = 0;
73
101 virtual const void* getPayload() const = 0;
102 };
103
104
122 template<class Payload, uint32_t EventCode>
124 Payload value;
125 public:
126
132 ConcreteEvent(const Payload& p) : value(p) {};
138 uint32_t getType() const override{
139 return EventCode;
140 }
141
147 virtual const void * getPayload() const override { return &value; };
148 };
149
162 uint32_t code;
163 public:
169 NoPayloadEvent(uint32_t c) : code(c) {};
170
176 uint32_t getType() const override {
177 return code;
178 }
179
185 virtual const void* getPayload() const override { return nullptr; };
186 };
187
188
189
190 namespace NamedEngineEvents {
191
193 const uint32_t EmitterConnected = 1u;
195 const uint32_t EmitterDestroyed = 2u;
197 const uint32_t EngineSynchronized = 4u;
199 const uint32_t SearchStarted = 8u;
201 const uint32_t SearchCompleted = 16u;
203 const uint32_t EngineCrashed = 32u;
205 const uint32_t InfoClampReceived = 64u;
207 const uint32_t InfoReceived = 128u;
208
209
216 using EmitterConnectedEvent = NoPayloadEvent;
217
224 using EmitterDestroyedEvent = NoPayloadEvent;
225
232 using EngineSynchronizedEvent = NoPayloadEvent;
233
240 using SearchStartedEvent = NoPayloadEvent;
241
249 using SearchCompletedEvent = NoPayloadEvent;
250
256 using EngineCrashedEvent = NoPayloadEvent;
257
263 // InfoClampReceived event type defined above
264
270 // InfoReceived event type defined above
271
272
277 static EngineSynchronizedEvent makeSynchronizedEvent() { return NoPayloadEvent(EngineSynchronized); };
282 static SearchStartedEvent makeSearchStartedEvent() { return NoPayloadEvent(SearchStarted); };
287 static SearchCompletedEvent makeSearchCompletedEvent() { return NoPayloadEvent(SearchCompleted); };
292 static EngineCrashedEvent makeEngineCrashedEvent() { return NoPayloadEvent(EngineCrashed); };
293
301 template<class Move>
302 static ConcreteEvent<std::vector<Info<Move>>, InfoClampReceived> makeInfoClampEvent(const std::vector<Info<Move>> & clamp) { return ConcreteEvent<std::vector<Info<Move>>, InfoClampReceived>(clamp); };
303
311 template<class Move>
312 static ConcreteEvent<Info<Move>, InfoReceived> makeInfoEvent(const Info<Move> & i) { return ConcreteEvent<Info<Move>, InfoReceived>(i); };
313 };
314
315 class EventEmitter;
316
348 friend EventEmitter;
349
350 std::list<EventEmitter*> publishers;
351
352 public:
356 virtual ~EventReceiver() {
357 this->unlinkAll();
358 }
359
365 void unlinkAll();
366
386 virtual uint32_t eventFilter() = 0;
387
406 virtual void receiveEvent(const EngineEvent* event) = 0;
407 };
408
431 std::function<void(const EngineEvent*)> clb;
432 uint32_t allowedEvents;
433 public:
440 FunctionCallbackEventReceiver(std::function<void(const EngineEvent*)> callback, uint32_t allowedEvents) : clb(callback), allowedEvents(allowedEvents) {};
447 FunctionCallbackEventReceiver(std::function<void()> callback, uint32_t allowedEvents) : clb([callback](const EngineEvent*) {callback(); }), allowedEvents(allowedEvents) {};
448
454 virtual void receiveEvent(const EngineEvent* event) override;
460 virtual uint32_t eventFilter() override { return allowedEvents; };
461 };
462
495 mutable std::mutex lock;
496 std::list<std::shared_ptr<EventReceiver>> receivers;
497 protected:
498
502 virtual ~EventEmitter();
503
510 int countActiveReceivers() const;
522 void emit(const EngineEvent* event);
523 public:
534 void unlink(const EventReceiver* receiver);
535
549 void connect(std::shared_ptr<EventReceiver> receiver);
550
571 void connect(std::function<void(const EngineEvent*)> callback, uint32_t eventFilter);
572
591 void connect(std::function<void()> callback, uint32_t eventFilter);
592 };
593
594
595
596}
597
Template for creating typed event objects with a payload.
Definition: EngineEvent.h:123
virtual const void * getPayload() const override
Get pointer to the stored payload.
Definition: EngineEvent.h:147
uint32_t getType() const override
Get the event type code.
Definition: EngineEvent.h:138
ConcreteEvent(const Payload &p)
Constructor that stores the payload.
Definition: EngineEvent.h:132
Base class for events emitted by an EngineInstance.
Definition: EngineEvent.h:42
virtual ~EngineEvent()
Virtual destructor for proper cleanup of derived classes.
Definition: EngineEvent.h:48
virtual uint32_t getType() const =0
Get the type code of this event.
virtual const void * getPayload() const =0
Get the payload data associated with 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
virtual ~EventEmitter()
Virtual destructor ensuring proper cleanup.
Definition: EngineEvent.cpp:15
void emit(const EngineEvent *event)
Emit an event to all connected receivers.
Definition: EngineEvent.cpp:25
void unlink(const EventReceiver *receiver)
Disconnect a specific receiver from future events.
Definition: EngineEvent.cpp:36
Base class for event observers that receive engine events.
Definition: EngineEvent.h:347
void unlinkAll()
Disconnect this receiver from all connected emitters.
Definition: EngineEvent.cpp:3
virtual void receiveEvent(const EngineEvent *event)=0
Handle an incoming event.
virtual ~EventReceiver()
Virtual destructor that disconnects from all connected emitters.
Definition: EngineEvent.h:356
virtual uint32_t eventFilter()=0
Get the event filter bitmask for this receiver.
Adapter class that wraps a callback function as an EventReceiver.
Definition: EngineEvent.h:430
FunctionCallbackEventReceiver(std::function< void(const EngineEvent *)> callback, uint32_t allowedEvents)
Constructor for callbacks that receive the full event.
Definition: EngineEvent.h:440
FunctionCallbackEventReceiver(std::function< void()> callback, uint32_t allowedEvents)
Constructor for simple parameterless callbacks.
Definition: EngineEvent.h:447
virtual uint32_t eventFilter() override
Get the event filter bitmask.
Definition: EngineEvent.h:460
virtual void receiveEvent(const EngineEvent *event) override
Handle the event by invoking the stored callback.
Definition: EngineEvent.cpp:11
Simple event type with no associated payload.
Definition: EngineEvent.h:161
virtual const void * getPayload() const override
Get the payload (always nullptr for NoPayloadEvent).
Definition: EngineEvent.h:185
NoPayloadEvent(uint32_t c)
Constructor that stores the event type code.
Definition: EngineEvent.h:169
uint32_t getType() const override
Get the event type code.
Definition: EngineEvent.h:176