Feature request: Delegates


It would be great if there was a built-in set of JUCE classes to provide the typical delegate functionality
one finds in other languages like C#, Java or REALbasic.

I know there are libraries ( e.g. BOOST ) and various solutions out there, but it would be great if there were something like this “straight outta the box” :slight_smile:

Also - does anyone know if the new C++11 language changes give anything like this but built into the language ? ( doubt it )


If you have c++11 then you have std::function, std::bind, and lambdas. If you don’t have c++11 but you do have a modern compiler then you have std::tr1, which provides function and bind. If you have neither c++11 or std::tr1 then you can always used boost.


Robust support for functors with bound parameters (which are a superset of delegate functionality) and possibly lambdas are almost certain to exist in your development environment, or will be fairly soon. Integrating such features into JUCE seems like a duplication of effort.


As Vinn says its (possibly) on the cards, using the features he mentioned. It was also briefly discussed in this thred.


I’m going to state that traditional delegates are boring. They address the “problem” of form while doing nothing to address function. What is more interesting is a system of delegates that is friendly to concurrent programming.

std::future is the C++11 industry solution but in my opinion they are bulky and hard to read. Instead, I prefer my own system which is almost just like the juce::ListenerList, with the twist that when you call addListener(), you can specify on which thread you would like to be called (!). Everything is handled for you.

You can see how this works, in the vf::Listeners implementation in VFLib

The heavy lifting in this implementation is the part where all conflicts related to calling listeners that are about to be removed, or have been removed, from other threads are resolved - plus a lock free and mostly wait-free garbage collected free store which avoids ABA without using 64-bit CAS:

Listeners< ListenerClass > Class Template Reference

Example code:

void audioDeviceIOCallback (const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples)

        // Notify listeners.
        listeners.call (&Listener::onOutputLevelChanged, newOutputLevel);

Note the clear syntax in listeners.call(). Don’t be fooled, this ain’t the juce::ListenerList, all Listener objects which register via addListener() get to specify an additional CallQueue on which they would like the member function to get invoked.