Coder Social home page Coder Social logo

events's Introduction

Events

This repository contains a simple implementation of an event dispatcher. Its design is inspired by the venerable Observer pattern, but it differs in several ways:

  • The event dispatcher offers a public interface for notifying observers. An event may thus be triggered externally. The client does not need to know anything about the observers.
  • All participants in the event system are very loosely coupled. The dispatcher only knows about a base event class. Observers only need to provide a slot with a pre-defined interface.
  • Observers may subscribe and unsubscribe from receiving events.

Technical details

This implementation works using pure C++11 features. At the heart of the event processing are arbitrary function objects. Each observer needs to implement a slot with a certain interface. These function objects are then stored in a map, which is indexed by an event descriptor. Currently, the event descriptor is simply a const char*. This seemed straightforward to implement. The event descriptor has been updated to handle arbitrary types, but the map idea is still being kept in place. Upon posting new events, the dispatcher only needs to traverse the map and call all correspond slots.

Please see the file Demo.cc for a simple demonstration.

Limitations

Currently, I am assuming that everything runs in a single thread. The processing gets slightly more complicated once observers are permitted to receive objects in another thread. In this case, I would suggest using std::shared_ptr to ensure that an event is not prematurely destroyed. Maybe I will implement this someday...

Criticism

I would welcome any critical comments regarding the design. I think that the system is relatively small, efficient, and decoupled. Please help me improve the code if you beg to differ!

License

The code is released under the MIT license.

events's People

Contributors

pseudomanifold avatar root3287 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

events's Issues

[Suggestion] Event Types.

Hello,
I've noticed that you posted a blog on a simple event system over here. One of the limitations that you put is, "Another issue is a systematic problem: I cannot guarantee the uniqueness of the self-described events. If a client chooses to implements events A and B and both return, say "Foo" as their event type, the dispatcher will not be able to separate between these events."

A solution to this issue is to make the Dispatcher and Event into a classes template. Then we can make a struct that defines different types. For example.

// Defines the different event types for A
enum class EventTypeA{
e1, e2, e3, e4
};

// Defines different event types for B
enum class EventTypeB{
e1, e2, e3, e4
};

Dispatcher<EventTypeA> _dispatcherA;
Dispatcher<EventTypeB> _dispatcherB;

_dispatcherA.subscribe(EventTypeA::e1, [](const Event<EventTypeA>& event){
    // Received for type e1 for Event A.  Process what you want here.
});
_dispatcherB.subscribe(EventTypeB::e1, [](const Event<EventTypeB>& event){
    // Received for type e1 for Event B.  Process what you want here.
});

To implement this you need to change event to:

template<typename T>
class Event
{
public:
  virtual~ Event();

  virtual T type() const = 0;
};

Then in the EventDispatcher

template<typename T>
class Dispatcher
{
public:

  using SlotType = std::function< void( const Event<T>& ) >;

  void subscribe( const T& descriptor, SlotType&& slot );

  void post( const Event<T>& event ) const;

private:

 std::map< Event::DescriptorType, std::vector<SlotType> > _observers;
};

Any idea how to unsubscribe a handling function?

When I am Implementing a similar system, I found that if I use std::bind to create some std::function, I can't compare the addresses of these std::functions. So I can't implement a method like removeEvent("SomeEventName", bind(.........))

Threads

I understand you mentioned that the threads are currently not supported, but did you get a chance to implement it?

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.