Giter Site home page Giter Site logo

cpp-notificationcenter's Introduction

CPP-NotificationCenter

A C++ API inspired by Cocoa's NSNotificationCenter API.

Usage

Using NotificationCenter is simple. In order to use the default center, simply use the static method NotificationCenter::defaultNotificationCenter() like so:

NotificationCenter::defaultNotificationCenter()->addObserver([=]{printf("Hello world!\n");}, "My Observer");

NotificationCenter is intended to be included directly in your projects, as such no library (dynamic or static) is provided.

Supported Compilers

NotificationCenter requires a compiler that supports the following C++11 APIs:

std::mutex
std::function
std::bind
std::shared_ptr

It has been tested on the following compilers (as of December 15th, 2013):

Compiler Name Version Pass/Fail Notes
Clang 3.3 Pass N/A
MSVC 2013 Pass N/A

Adding Observers

Adding observers is a simple porcess. Simply invoke the method NotificationCenter::addObserver on your NotificationCenter passing in a function pointer and string for the notification that this observer should respond to. A couple of examples of how to do this are:

NotificationCenter::defaultNotificationCenter()->addObserver([=]{printf("Hello world!\n");}, "My Observer");
NotificationCenter::defaultNotificationCenter()->addObserver(helloWorldFunc, "My Observer");
Foo myFoo;
NotificationCenter::defaultNotificationCenter()->addObserver(std::bind(&Foo::func, myFoo), "My Observer");

Currently, only void(void) function signatures are supported.

Posting Notifications

Posting notifications can be done with NotificationCenter::postNotification, like so:

NotificationCenter::defaultNotificationCenter()->postNotification("My Observer");

Avoiding Unnecessary Lookups

Notifications can be posted and modified by either string or iterator. Posting or modifying by string incurs a string lookup, which depending on the application may not be ideal. For these situations, the best option when using NotificationCenter is to post and modify by iterator. An example of how to do this is:

NotificationCenter::notification_itr_t notiItr = NotificationCenter::defaultNotificationCenter()->getNotificationIterator("My Observer");
NotificationCenter::defaultNotificationCenter()->addObserver([=]{printf("I'm being posted by an iterator!\n");}, notiItr);
NotificationCenter::defaultNotificationCenter()->postNotification(notiItr);

NotificationCenter::addObserver, NotificationCenter::removeObserver, NotificationCenter::removeAllObservers, and NotificationCenter::postNotification all support notification iterators in overloaded methods.

Multiple NotificationCenters

You can also use more than one instance of NotificationCenter. Although a default notification center is provided, you can also create your own notification centers for whatever purpose you may require them for.

Example Program

The included example program shows you the basics of how to use NotificationCenter. It's not intended to be sophisticated by any means, just to showcase the basics.

Future

As it stands, NotificationCenter is currently limited in that it only supports callbacks with the signature of void(void). In the future, NotificationCenter may have support for additional function signatures either by making NotificationCenter a template, or by allowing one to specify arbitrary data in a similar way to NSNotification's userInfo parameter.

Bugs

I don't expect this to work flawlessly for all applications, and thread safety isn't something that I've tested particularly well. If you find issues, feel free to file a bug. If you wish to contribute, simply fork this repository and make a pull request.

License

NotificationCenter is licensed under the MIT license.

cpp-notificationcenter's People

Contributors

geenz avatar net-cat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cpp-notificationcenter's Issues

RemoveObserver removes all observers

The following code should remove only the b observer and print the AC string, but only C appears in the console output:

auto name = "notify";

auto a = NotificationCenter::defaultNotificationCenter()->addObserver([=] { printf("A"); }, name);
auto b = NotificationCenter::defaultNotificationCenter()->addObserver([=] { printf("B"); }, name);
NotificationCenter::defaultNotificationCenter()->removeObserver(name, b);
auto c = NotificationCenter::defaultNotificationCenter()->addObserver([=] { printf("C"); }, name);

NotificationCenter::defaultNotificationCenter()->postNotification(name);

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.