GitXplorerGitXplorer
z

event

public
2 stars
1 forks
0 issues

Commits

List of commits on branch master.
Verified
bf626e11585117d9f3de291f0a187395301f25b8

Merge pull request #10 from KNpTrue/dev

zzebin-wu committed 5 years ago
Unverified
5d70ea0aa20225f565c0510c2a824354fa70820c

Add some doxygen.

zzebin-wu committed 5 years ago
Unverified
72597060bf02ce89d4b87fc5f2af0fe7cfbc2d98

Add DEBUG macro definition and delete class Object

zzebin-wu committed 5 years ago
Verified
b8d874957b6e02416190aedc7111b42ac8961aaa

Merge pull request #9 from KNpTrue/dev

zzebin-wu committed 5 years ago
Unverified
21af2f95a48460975ee346c0aa492a823050f2e2

Add some doxygen

zzebin-wu committed 5 years ago
Unverified
151189c3ca15d46a2ad87872195bc60dbf3a2e30

Update submodule version

zzebin-wu committed 5 years ago

README

The README file for this repository.

Event

license

Contents

Introduction

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.

Code style

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.

Build

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

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

Usage

Note: See helloworld.cpp to get more information.

  1. 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>
  1. 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;
};
  1. 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;
};
  1. 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;
    }
};
  1. Trigger event
    MyEvent e("hello world");
    MyBus bus;
    MyEventCb cb;
    bus.addEvent(&e, &cb);
    bus.dispatch();

License

MIT © 2020 KNpTrue.