subber.hpp contains a C++14 implementation of the publisher-subscriber pattern. The purpose of subber is simplicity. This is illustrated by the following self-contained sample code:
#include "subber.hpp"
struct SimpleEvent { };
struct Foo : public Subber<SimpleEvent> {
void notified(const SimpleEvent& event) override {
printf("Foo received SimpleEvent\n");
}
};
int main(int argc, char** argv) {
SimpleEvent simpleEvent;
Foo foo;
publish(simpleEvent); //"Foo recieved SimpleEvent" is printed
return 0;
}
It is not uncommon that a class should listen to several events. Any class can have multiple subbers (=listen to several events) as shown in the following example:
#include "subber.hpp"
struct SimpleEvent { };
struct OtherEvent { };
struct Foo : public Subber<SimpleEvent, OtherEvent> {
void notified(const SimpleEvent& event) override { /* do something */ }
void notified(const OtherEvent& event) override { /* do some other thing */ }
};
int main(int argc, char** argv) {
SimpleEvent simpleEvent;
OtherEvent otherEvent;
Foo foo;
publish(simpleEvent, otherEvent); //Something is done and some other thing is done
return 0;
}
Note that you can publish several events at once!
Subber does not guarantee any call-order of notified.
A more rigorous example can be found in example.cpp.
subber compiles on g++ 6.1.1 using the --std=c++14 flag.
Happy coding 👍!