AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH events library documentation

Introduction

AnCH events library aims to provide event programming facilities (observers, event handlers and an asynchronous event bus).
It is based on C++ template. This is not a library but only header files.

This library is based on observer/observable design pattern. So the base classes are anch::events::Observer<T> and anch::events::Observable<T>.

Prerequisites

  • anch-utils header files
  • pthread library

Installation

CMake target anch::events (header only):

find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::events)

See installation.

Conception

AnCH events library is header only.
Events are Event<T>: a body of type T and headers (string map).

Observer/Observable

Observer<T> implements handle(const Event<T>&) . Observable<T> notifies its observers synchronously with notifyObservers . Observers can add or remove observers while being notified.

Event handler

EventHandler<T, Derived> is an observer which calls a member function of Derived : a class can inherit several event handlers to handle several event types. It can register itself in an event bus, and unsubscribe .

Event bus

EventBus<T> notifies its observers asynchronously, in firing order, from a dedicated thread: fireEvent does not wait for observers. EventBus<T>::getInstance() is the global bus ; other buses are plain objects. A bus destructor delivers the queued events before stopping its thread.

Example

#include "events/eventBus.hpp"
#include "events/eventHandler.hpp"
#include "events/observable.hpp"
#include "events/observer.hpp"
#include <iostream>
#include <string>
// Observer: handles events of one type
class Printer: public anch::events::Observer<std::string> {
public:
virtual void handle(const anch::events::Event<std::string>& event) noexcept override {
std::cout << "observer: " << event.body << std::endl;
}
};
struct Created {
std::string name;
};
struct Deleted {
int id;
};
// Event handlers: one class handles several event types
class Audit: public anch::events::EventHandler<Created, Audit>, public anch::events::EventHandler<Deleted, Audit> {
public:
Audit(): anch::events::EventHandler<Created, Audit>(&Audit::onCreated),
anch::events::EventHandler<Deleted, Audit>(&Audit::onDeleted) {
}
void onCreated(const anch::events::Event<Created>& event) {
std::cout << "created: " << event.body.name << " by " << event.headers.at("user") << std::endl;
}
void onDeleted(const anch::events::Event<Deleted>& event) {
std::cout << "deleted: " << event.body.id << std::endl;
}
};
int
main() {
// Synchronous notification
Printer printer;
observable.addObserver(printer);
observable.notifyObservers("hello");
observable.removeObserver(printer);
Audit audit;
created.addObserver(audit);
created.notifyObservers(Created{"Rex"}, {{"user", "admin"}});
// Asynchronous notification: events are delivered by the bus thread, in firing order
{
bus.addObserver(audit);
bus.fireEvent(Deleted{1});
bus.fireEvent(Deleted{2});
} // bus destructor delivers queued events
return 0;
}

Output:

observer: hello
created: Rex by admin
deleted: 1
deleted: 2