Giter VIP home page Giter VIP logo

multifunction's Introduction

Multicast delegate for C++

C# has the nice concept of a multicast delegate which is the generalisation of a function pointer for multiple targets.

In other words, it’s kind of like an observer with nice syntax. (And I’m intentionally leaving out the details here.)

Usage

The usage is pretty straight-forward and echoes C#’s as much as possible.

void f(int n) { cout << "f: " << n << "\n"; }

util::multifunction<void(int)> event;

auto cookie = event += f;
event += [](int n) { cout << "[]: " << n << "\n"; };

event(42);
// Output:
// f: 42
// []: 42

event -= cookie;

event(23);
// Output:
// []: 23

A multi-function behaves essentially like a function – it’s callable, and defines the appropriate typedefs such as result_type. It also supports adding and removing listeners, via the functions operator+= and operator-=.

Notice that in C#, you’d use the listener delegate itself as an argument to -=. Unfortunately, that’s not possible in C++ because function-like objects are not generally identifiable, so std::function doesn’t define equality comparison. We wouldn’t know which object to remove.

Although this could be worked around for many (but not all) cases, multi-functions employ a slightly different approach:

Adding a listener to a multi-function returns a “cookie” of an opaque type identifying the listener that has been added (hat-tip to Abyx for coming up with the idea).

Removing a cookie more than once does nothing. Adding a function more than once, on the other hand, causes the function to be invoked several times. (Does this make sense?)

util::multifunction supports all function-like objects that are supported by std::function. It defines all the same member types and member functions, with the exception of assign and operator bool which don’t make sense in util::multifunction.

Calling a util::multifunction with a non-void return type returns the value of the last call only. The order in which the listeners are called is undefined, though, so the result might not always be usable.

multifunction's People

Contributors

klmr avatar

Watchers

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.