Coder Social home page Coder Social logo

events's Introduction

#Events

by Terry Mahaffey

Events is a functional based event system - an alternative to the classical OO Observer pattern. Let's say you have a class, Foo. You can add an event like this

class Foo
{
public:
    Event0<Foo> Changed;
};

You can then subscribe to the Changed event like this:

Foo f;
int token = f.Changed.Register(Bar);

Where Bar can be any C++ functor (a function pointer, a functor object, a lambda if your compiler supports it, etc). The returned token can be used to Unregister later, if desired. If someone calls Fire on the Changed event, Bar is called.

The Fire method of the Event0 class is private (and Foo is granted friendship, so the Foo class can fire the events). If this isn't desired, you can use the PuclicEvent0 object instead:

class Foo
{
public:
    PublicEvent0 Changed;
};

This behaves just as before, except anyone can call the Fire method on the Changed object, not just the Foo class. Note that there is no template parameter in this version.

Events can also take parameters. There are different sets of objects for the various arities, in both normal and public versions. To delcare an event which takes and int, a char, and a float, you'd do this:

class Foo
{
public:
    PublicEvent3<int,char,float> SomethingHappened;
};

When you register for this event, you have to provide a functor which takes the appropriate arguements. When you fire the event, you have to provide the arguements.

Taking from C#, operator+= and operator-= have been overridden to basically alias Register and Unregister. So the following syntax is possible, if you prefer:

Foo f;
f.Changed += []() {
    std::cout << "Foo Changed!\n";
});

This software is made available under the Boost license.

events's People

Contributors

terrymah avatar

Watchers

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