GithubHelp home page GithubHelp logo

travishaagen / eventbus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gelldur/eventbus

0.0 1.0 0.0 262 KB

A lightweight and very fast event bus / event framework for C++11

License: Apache License 2.0

C++ 84.13% CMake 15.87%

eventbus's Introduction

EventBus GitHub version

  Build status for Travis   Build status for Appveyor

Simple and very fast event bus. The EventBus library is a convenient realization of the observer pattern. It works perfectly to supplement the implementation of MVC logic (model-view-controller) in event-driven UIs

EventBus Diagram

EventBus was created because I want something easy to use and faster than CCNotificationCenter from cocos2d-x library. Of course C++11 support was mandatory.

EventBus main goals:

  • Fast
  • Easy to use
  • Strongly typed
  • Free
  • tiny (~15 KB)
  • Decouples notification senders and receivers
  • on every platform you need (cross-platform)

Brief @ presentation

Presentation google docs

Sample

You can checkout sample/ If you want to play with sample online checkout this link: wandbox.org

Usage

  1. Store bus
// store it in controller / singleton / std::sharted_ptr whatever you want
Dexode::EventBus bus;
  1. Define events
namespace Event // optional namespace
{
	struct Gold
	{
		int goldReceived = 0;
	};

	struct OK {}; // Simple event when user press "OK" button
}
  1. Subscribe
// ...
bus.listen<Event::Gold>
			([](const auto& event) // listen with lambda
			{
				std::cout << "I received gold: " << event.goldReceived << "!" << std::endl;
			});

HudLayer* hudLayer;
// Hud layer will receive info about gold
bus.listen<Event::Gold>(std::bind(&HudLayer::onGoldReceived,hudLayer,std::placeholders::_1));
  1. Notify
//Inform listeners about event
bus.notify(Event::Gold{12}); // 1 way

bus.notify<Event::Gold>({12}); // 2 way

Event::Gold myGold{12};
bus.notify(myGold); // 3 way

Lambda listener:

struct SampleEvent {};
Dexode::EventBus bus;
//...
int token = bus.listen<SampleEvent>([](const SampleEvent& event) // register listener
{
});

//If we want unlisten exact listener we can use token for it
bus.unlistenAll(token);

Listener is identified by token. Token is returned from EventBus::listen methods. We can register multiple listeners on one token:

Dexode::EventBus bus;
struct SampleEvent {};
//...
int token = bus.listen<SimpleEvent>([](const auto& event) // register listener
{
});

bus.listen<SimpleEvent>(token, [](const auto& event) // another listener
{
});

bus.unlistenAll(token);//Now those two lambdas will be removed from listeners

If you don't want to handle manually with token you can use EventCollector class. It is useful when we want to have multiple listeners in one class. So above example could look like this:

Dexode::EventBus bus;
struct SampleEvent {};
Dexode::EventCollector collector{&bus};
//...
collector.listen<SampleEvent>([](const SampleEvent& event) // register listener
{
});

collector.listen<SampleEvent>([](const SampleEvent& event) // another listener
{
});

collector.unlistenAll();//Now those two lambdas will be removed from listeners

Or as component of class:

class Example
{
public:
	Example(const std::shared_ptr<Dexode::EventBus>& bus)
			: _collector{bus}
	{
		_collector.listen<SimpleEvent>(std::bind(&Example::onEvent1, this, std::placeholders::_1));
		_collector.listen<OtherEvent>(std::bind(&Example::onEvent2, this, std::placeholders::_1));
	}

	void onEvent1(const SimpleEvent& event)
	{
	}

	void onEvent2(const OtherEvent& event)
	{
	}

private:
	Dexode::EventCollector _collector;// use RAII
};

//EventCollector sample
std::shared_ptr<Dexode::EventBus> bus;
Example ex{bus};
//...
bus.notify<int>("event1", 2);

Add to your project

EventBus can be added as ADD_SUBDIRECTORY to your cmake file. Then simply link it via TARGET_LINK_LIBRARIES Example:

# No tests/benchmarks target won't be added. Root CMakeLists is for development.
ADD_SUBDIRECTORY(path/to/EventBus/lib)
ADD_EXECUTABLE(MyExecutable
		main.cpp
		)

TARGET_LINK_LIBRARIES(MyExecutable PUBLIC Dexode::EventBus)

Also if you want you can install library and add it at any way you want. Eg.

mkdir -p lib/build/
cd lib/build
cmake -DCMAKE_BUILD_TYPE=Relase -DCMAKE_INSTALL_PREFIX=~/.local/ ..

cmake --build . --target install
# OR
make && make install

Now in Release/install library is placed.

Or, you can install the library through your package manager (dpkg, rpm, etc). E.g.

mkdir -p lib/build/
cd lib/build

# For most RH-based Distributions
cmake -DCMAKE_BUILD_TYPE=Relase -DCPACK_GENERATOR="RPM" ..

# For most Debian-based Distributions
cmake -DCMAKE_BUILD_TYPE=Relase -DCPACK_GENERATOR="DEB" ..

# Or for both of them
cmake -DCMAKE_BUILD_TYPE=Relase -DCPACK_GENERATOR="RPM;DEB" ..

cmake --build . --target package
# Or
make package

# For most Debian-based systems
sudo dpkg -i EventBus*.deb

# For most RH-based systems
sudo rpm -i EventBus*.rpm
# OR
sudo yum install EventBus*.rpm

Performance (could be outdated)

I have prepared some performance results. You can read about them here Small example:

check10NotificationsFor1kListeners                                     263 ns        263 ns    2668786 sum=-1.76281G
check10NotificationsFor1kListeners_CCNotificationCenter              11172 ns      11171 ns      62865 sum=54.023M

checkNotifyFor10kListenersWhenNoOneListens                              18 ns         18 ns   38976599 sum=0
checkNotifyFor10kListenersWhenNoOneListens_CCNotificationCenter     127388 ns     127378 ns       5460 sum=0

Issues ? GitHub issues

Please report here issue / question / whatever, there is chance 99% I will answer ;)

If you have any questions or want to chat use gitter.

Join the chat at https://gitter.im/EventBusCpp/Lobby

Tesing and metrics

// TODO

LoC Coverage Status

Thanks to GitHub contributors Open Source Helpers

For modern cmake refer

License

EventBus source code can be used according to the Apache License, Version 2.0. For more information see LICENSE file

If you don't like to read to much here is sumup about license Apache 2.0

eventbus's People

Contributors

gelldur avatar aang23 avatar stanislawkabacinski avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.