Git Product home page Git Product logo

event's Introduction

Event

Event is a header only library that makes it easy to create event driven programs in C++11.

Usage

Simply include event.hpp, there are no dependencies (other than a compiler that supports the memory and functional C++11 standard library components).

Create an event by using the Event template class:

Event<> my_event;

Events can have none or many arguments of any type you would pass in to a function:

Event<int, int&, const int&, int*, CustomClass> my_event;

Firing an Event will synchronously execute all bound functions. Firing an Event requires supplying the arguments specified in the template. These values will be passed in to the bound functions.

Event<int> my_event;
my_event.fire(0);
my_event.fire(1);

Functions that take the same arguments and return void may be bound to the Event. A bound function will automatically be unbound when the bind ( std::shared_ptr<Event<Args...>::Bind>) falls out of scope. Binds can safely outlive the respective Event. Alternatively a function can be permanently bound using the Event::permanent_bind method.

Event<int> my_event;
{
	// bind for the duration of the bind object
	auto bind = my_event.bind([](int input){
		std::cout << "temporary:" << input << std::endl;
	});
	// permanently bind
	my_event.permanent_bind([](int input){
		std::cout << "permanent: " << input << std::endl;
	});
	// fire, both binds will execute
	my_event.fire(0);
}
// fire, only the permanent bind will execute as the temporary bind has fallen
// out of scope
my_event.fire(1);

The above code will output:

temporary: 0
permanent: 0
permanent: 1

It is also safe to bind and unbind in a function that is bound to an Event, even while that Event is firing:

std::shared_ptr<Event<>::Bind> bind = 0;
Event<> my_event;
// bind a function that automatically unbinds itself after executing once
bind = my_event.bind([&bind](){
	std::cout << "hello world" << std::endl;
	bind = 0;
});
my_event.fire();
my_event.fire();

The above code will output:

hello world

Test

Tests are successful if there is no output. Example build command with gcc on windows:

g++ -ggdb -Wall --std=c++11 test.cpp -o test.exe

event's People

Contributors

esoma avatar

Watchers

James Cloos avatar  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.