Code Monkey home page Code Monkey 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

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.