Git Product home page Git Product logo

picoevents's Introduction

picoevents

A simple mechanism to notify events between various UI elements Not thread-safe (but can be easily made so with mutexes protecting the add / replace / remove / notify methods

A typical use case is, you have a button with a on/off state that can be changed in different parts of the application, and you also need other objects to react when the state of the button changes

so first you define an event:

picoevents::Event<const int> buttonStateChangedEvent;

you'll have somewhere, something that hold the current state with get/set methods

 bool getCurrentButtonState() const
 {
	return _buttonState;
 }
 void setCurrentButtonState(bool value) const
 {
	buttonStateChangedEvent.notify(value);
 }

Note how setCurrentButtonState doesn't change the value, but instead notifies the event. So you need to handle the event:

buttonStateChangedEvent.add([&](bool v) { this->_buttonState=v;});

Now any object in the UI can change the state of the button by calling:

buttonStateChangedEvent.notify(value);

and any object in the UI can be notifed of button change by adding:

buttonStateChangedEvent.add([&](bool v) { dosomething.... });

All of this, without these objects having any knowledge of each other !

You can also prepare a notification, then trigger it at a later time:

auto notifier = buttonStateChangedEvent.makeNotifier(false);
notifier.trigger();  // call buttonStateChangedEvent.notify(false);

That way you can prepare a notification on a thread, and trigger it on a different one.

picoevents's People

Contributors

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