Event is an event notification library implemented in C++11 that provides IO, timer and signal events, all events will be triggered asynchronously. At first it only supported the linux platform, and later it will implement more platforms in the submodule common.
The library supports code style checking, the checker is cpplint, a command line tool that checks for style issues in C/C++ files according to the Google C++ Style Guide.
Before building, you need to make sure that cpplint is installed on the system, if not, you can use pip to install.
pip install cpplint
Then you can build the library.
make
You can build examle with the following command, these examples will be placed under ./build/{platform}/bin
,the premise of executing them is to link the library, see Install.
make example
Install the library to your system or the specified path(Set by the environment variable INSTALL_DIR
),as shown in the following command, the library will be installed under /lib
.
make INSTALL_DIR=/ install
Note: See helloworld.cpp to get more information.
- Contains the necessary header files and use namespace event
#include <event/event.hpp>
#include <event/bus.hpp>
#include <event/callback.hpp>
#include <queue>
#include <iostream>
#include <platform/lock.hpp>
- Implement your own event, but need to inherit from Event
class MyEvent: public event::Event {
public:
MyEvent(const char *msg = nullptr): msg(msg) {}
const char *getMsg() const {
return msg;
}
private:
const char *msg;
};
- Implement your own event bus, but need to inherit from Bus
class MyBus: public event::Bus<MyEvent> {
public:
void addEvent(MyEvent *e, const event::Callback<MyEvent> *cb) override {
mutex.lock();
events.push(new EventState(e, cb));
mutex.unlock();
}
void delEvent(MyEvent *e) {}
int dispatch() {
mutex.lock();
EventState *s = events.front();
events.pop();
mutex.unlock();
s->cb->onEvent(s->e);
delete s;
return -1;
}
private:
class EventState {
public:
EventState(MyEvent *e, const event::Callback<MyEvent> *cb):
e(e), cb(cb) {}
MyEvent *e;
const event::Callback<MyEvent> *cb;
};
std::queue<EventState *> events;
platform::Lock mutex;
};
- Implement the callback of event
class MyEventCb: public event::Callback<MyEvent> {
public:
void onEvent(MyEvent *e) const override {
std::cout << "msg: " << e->getMsg() << std::endl;
}
};
- Trigger event
MyEvent e("hello world");
MyBus bus;
MyEventCb cb;
bus.addEvent(&e, &cb);
bus.dispatch();