# ChangeBroadcaster::addListener(Listener\*, lambda)

**URL:** <https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325>\
**Category:** General JUCE discussion\
**Created:** [May 10, 2020, 8:26pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325 "2020-05-10T20:26:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 10, 2020, 8:26pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/1 "2020-05-10T20:26:34Z")

</div>

@ the JUCE team:

Has there been any thoughts or discussion of adding per-listener lambda callbacks to ChangeBroadcaster?

It would eliminate the `changeListenerCallback(ChangeBroadcaster*)` member function we need to implement.

If we want to have multiple objects listen to a single `ChangeBroadcaster`, it would be really helpful:

```auto
struct BackgroundThread : Thread, ChangeBroadcaster { ... };

```

and then you could add listeners to it:

```auto
backgroundThread.addListener(&widgetA, [](){ /* what WidgetA does when the thread fires */ });
backgroundThread.addListener(&widgetB, [](){ /* */ });

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 10, 2020, 9:02pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/2 "2020-05-10T21:02:48Z")

</div>

Here is a hacky way to add the functionality (untested) using a wrapper class:

```auto
/*
 this class wraps a ChangeBroadcaster, adding the ability to set lambda callbacks per listener

 we inherit from ChangeListener privately to act as a wrapper around a ChangeBroadcaster 
 held internally, which actually performs the messaging.

 We inherit from ChangeBroadcaster publicly, so other ChangeListeners can still listen to 
 us and use the changeListenerCallback() function if they want.
 */
struct EnhancedChangeBroadcaster : public ChangeBroadcaster, private ChangeListener
{
private:
    /* the changeBroadcaster we wrap that actually does the messaging */
    ChangeBroadcaster changeBroadcaster;
public:
    EnhancedChangeBroadcaster()
    {
        changeBroadcaster.addChangeListener(this);
    }
    
    ~EnhancedChangeBroadcaster()
    {
        changeBroadcaster.removeChangeListener(this);
    }

```

```auto
    /*
     we replicate the ChangeBroadcaster interface for triggering callbacks.
     */
    void sendChangeMessage() { changeBroadcaster.sendChangeMessage(); }
    void sendSynchronousChangeMessage() { changeBroadcaster.sendSynchronousChangeMessage(); }
    void dispatchPendingMessages() { changeBroadcaster.dispatchPendingMessages(); }
private:
    struct LambdaChangeListener
    {
        ChangeListener* changeListener = nullptr;
        std::function<void()> func = nullptr;
        
        LambdaChangeListener(ChangeListener* cl, std::function<void()> f) :
        changeListener(cl), func(std::move(f)) { }
    };

    std::vector<LambdaChangeListener> listeners;
    
    /*
     here is where we intercept the ChangeBroadcaster callback,
     and call the lambda for each changeListener that has been registered with us.
     */

```

```auto
    void changeListenerCallback (ChangeBroadcaster* source)
    {
        for( auto& l : listeners )
        {
            if( l.changeListener != nullptr )
            {
                if( l.func )
                    l.func();
                l.changeListener->changeListenerCallback(this);
            }
        }
    }
public:
    /*
     here is where we replicate the rest of the ChangeBroadcaster interface
     */
    void addChangeListener(ChangeListener* l, std::function<void()> func = nullptr)
    {
        auto existing = std::find_if(listeners.begin(),
                                     listeners.end(),
                                     [l](const LambdaChangeListener& lcl)
                                     {
                                         return lcl.changeListener == l;
                                     });
        if( existing == listeners.end() )
        {
            listeners.emplace_back(l, func);
        }
    }

```

```auto
    void removeChangeListener(ChangeListener* l)
    {
        auto existing = std::find_if(listeners.begin(),
                                     listeners.end(),
                                     [l](const LambdaChangeListener& lcl)
        {
            return lcl.changeListener == l;
        });
        
        listeners.erase(existing);
    }
    
    void modifyListenerCallback(ChangeListener* l, std::function<void()> func)
    {
        auto existing = std::find_if(listeners.begin(),
                                     listeners.end(),
                                     [l](const LambdaChangeListener& lcl)
                                     {
                                         return lcl.changeListener == l;
                                     });
        if( existing != listeners.end() )
        {
            existing->func = std::move(func);
        }
    }

```

```auto
    void removeAllChangeListeners()
    {
        listeners.clear();
    }
};

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 12, 2020, 1:03am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/4 "2020-05-12T01:03:13Z")

</div>

Were you going to revise your suggestion? I had something similar already in place but the final behavior isn’t the same as my example above. I’m curious about your thoughts @yfede

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [May 12, 2020, 8:28am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/5 "2020-05-12T08:28:26Z")

</div>

Honestly, I withdrew my post because I was not sure it fitted your case. Your reimplementation of a ChangeBroadcaster that is also a ChangeListener got me all confused so I may be missing your point entirely, but my suggestion was:

With a listener class that simply calls a lambda when its change callback is triggered (I see you have a similar class in your code):

```auto
class LambdaChangeListener : public ChangeListener
{
    std::function <void (ChangeBroadcaster*)> onChange;

    void changeListenerCallback (ChangeBroadcaster* source) override
    {
        if (onChange)
            onChange (source); 
    }
};

```

And then use as many of those as members (without the need to inherit from them) to define the behavior you desire. For example (I tried to grasp the idea of your initial code):

```auto
class MyBeautifulClass
{
public:
    MyBeautifulClass()
    {
        widget1Response.onChange = [this] (ChangeBroadcaster*) { widget1.doSomething (); }
        widget2Response.onChange = [this] (ChangeBroadcaster* source) { widget2.doSomethingWith (source); }

        backgroundThread.addListener (&widget1Response);
        backgroundThread.addListener (&widget2Response);
    }  

private:
    SomeComponent widget1;
    LambdaChangeListener widget1Response;

    SomeOtherComponent widget2;
    LambdaChangeListener widget2Response;
};

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 12, 2020, 8:47am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/6 "2020-05-12T08:47:50Z")

</div>

Honestly what we really need is something like this added to ChangeBroadcaster:

```auto
void addCallback(std::function<void()> callback)
{
    callbacks.push_back(std::move(callback));
} 

```

and then

```auto
void ChangeBroadcaster::callListeners()
{
    changeListeners.call ([this] (ChangeListener& l) { l.changeListenerCallback (this); });

    for( auto & cb : callbacks )
    {
        if( cb )
            cb();
    }
}

```

and add this as a private member:

```auto
std::vector< std::function<void()> > callbacks;

```

The implementation that I showed a few posts prior shows how to enable the above idea, but with the ability to remove lambda callbacks and also change the lambda callback for a registered listener.  
That is something you can’t do with what I’ve shown in this post, as far as I know.  
I don’t think this is really possible:

```auto
void removeCallback(std::function<void()> lambda) 
{
    callbacks.erase(std::remove_if(callbacks.begin(), 
                                   callbacks.end(), 
                                   [&lambda](const auto& callback){ return lambda == callback; }), 
                    callbacks.end());
}

```

It wouldn’t be very DRY to pass the same lambda to that `addCallback` function, and then pass it to `removeCallback()` later, unless you’re storing it as a variable somewhere. This is the part of C++ I’m not totally 100% on.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 12, 2020, 8:53am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/7 "2020-05-12T08:53:28Z")

</div>

I also had this initial idea:

```auto
struct LambdaChangeListener : ChangeListener
{
    LambdaChangeListener(ChangeBroadcaster* cb_, std::function<void()> f) :
    cb(cb_),
    func(std::move(f))
    {
        cb->addChangeListener(this);
    }
    ~LambdaChangeListener() override
    {
        cb->removeChangeListener(this);
    }
    void changeListenerCallback(ChangeBroadcaster*) override
    {
        if( func )
            func();
    }
private:
    ChangeBroadcaster* cb;
    std::function<void()> func;
};

```

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [May 12, 2020, 8:59am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/8 "2020-05-12T08:59:11Z")

</div>

I don’t think you can compare `std::function` objects like this, I think you can only compare them to `nullptr`. Even if you could I wouldn’t expect them to return equal even if they’re created with the sample lambda as std::function is a fairly complex type erased beast.

There seems to be a bit of conflation between “lambda” and “std::function” function here.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [May 12, 2020, 9:02am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/9 "2020-05-12T09:02:27Z")

</div>

> [@matkatmusic](#):
>
> I also had this initial idea:

Looks like an almost identical proposal was done some time ago:

> [@Scoped-Listeners / Modernize listeners](https://forum.juce.com/t/scoped-listeners-modernize-listeners/23742):
>
> I’ve made this little class, and it really revolutionised my programming style. Its a scoped listener, and very easily combines the listener (old-school) functionality with the ability to attach std::function lambdas to it (a more modern approach). It’s not a very sophisticated, its very plain and simple. Example: class TestComponent : public Component { public: TestComponent( ColourSelector\* selector, MidiKeyboardComponent\* keyboard ) { // attach updateUI to the ChangeBroadcaster of Col…

My thoughts about this: it would be really nice if it were possible, with some template magic, to do something like

`ScopedListener <ChangeListener> listener (broadcaster);`

`ScopedListener <Button::Listener> listener (button);`

etc.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [May 12, 2020, 9:04am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/10 "2020-05-12T09:04:40Z")

</div>

> [@matkatmusic](#):
>
> It wouldn’t be very DRY to pass the same lambda to that `addCallback` function, and then pass it to `removeCallback()` later, unless you’re storing it as a variable somewhere

You could assign identifiers to them when adding, to remove them later. The ids could be string or int… but I personally wouldn’t go with that route because it feels like overcomplicating and still not very RAII.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [May 12, 2020, 10:01am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/11 "2020-05-12T10:01:32Z")

</div>

The approach taken by `boost::signals2` is to return a `Connection` object after registering a callback. To sever a connection, you call a `detach` (I forget the exact name) member function on the `Connection`. By default, `Connections` stay attached indefinitely, but they can also be converted to instances of `ScopedConnection`, which will detach automatically when they go out of scope.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 12, 2020, 7:51pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/12 "2020-05-12T19:51:26Z")

</div>

Is there any chance JUCE could get something like this?

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [May 12, 2020, 8:07pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/13 "2020-05-12T20:07:53Z")

</div>

I think it’s fairly unlikely. Too much code relies on the current listener system to change or remove it, and adding a completely separate listener system would be confusing. GUI controls already have both a ‘ListenerList’ and ‘onChange’ lambdas, and I don’t think adding a third way of achieving the same thing is a good idea.

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [May 12, 2020, 8:09pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/14 "2020-05-12T20:09:57Z")

</div>

@matkatmusic [https://github.com/palacaze/sigslot/blob/master/include/sigslot/signal.hpp](https://github.com/palacaze/sigslot/blob/master/include/sigslot/signal.hpp) is a single header file and might do all that you need.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 12, 2020, 9:14pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/15 "2020-05-12T21:14:16Z")

</div>

> [@reuk](#):
>
> GUI controls already have both a ‘ListenerList’ and ‘onChange’ lambdas, and I don’t think adding a third way of achieving the same thing is a good idea.

I’m not requesting it for GUI controls. I’m requesting it for ChangeBroadcaster, which is often used to signal that a background thread finished doing some work.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 13, 2020, 7:19am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/16 "2020-05-13T07:19:06Z")

</div>

I think this would depend on whatever class you’re using as the Listener to have a standardized interface with regard to adding listeners, and removing them.

Some classes have `addListener`, others have `addChangeListener`, others have other types of listener.

You could use some `if constexpr (std::is_same<T, Button::Listener>::value)` to decide if you’re going to use `addListener` or `if constexpr (std::is_same<T, ChangeListener>::value)` to decide if you’re going to use `addChangeListener`.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [May 13, 2020, 8:02am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/17 "2020-05-13T08:02:28Z")

</div>

Yes, something like that, or the generic implementation of `ScopedListener` could assume that `addListener()` and `removeListener()` are the methods to use, and templace specializations could be provided for deviations to that rule (e.g. `ChangeListener`)

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 13, 2020, 8:08am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/18 "2020-05-13T08:08:43Z")

</div>

So…

```auto
template<typename ListeneeType, typename ListenerType>
struct ScopedListener
{
    ScopedListener(ListeneeType& listenee, ListenerType& listener) :
    objectToListenTo(&listenee),
    objectThatIsListening(&listener)
    {
        objectToListenTo->addListener(objectThatIsListening);
    }

    ~ScopedListener()
    {
        objectToListenTo->removeListener(objectThatIsListening);
    }
private:
    ListeneeType* objectToListenTo = nullptr;
    ListenerType* objectThatIsListening = nullptr;
};

```

?

I would need to understand a usage example to think about how to expand it.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 13, 2020, 9:01am UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/19 "2020-05-13T09:01:43Z")

</div>

I think `objectToListenTo` would need to be some kind of WeakReference, in case the object is deleted before the listener (in which case we just skip the removeListener).

For Components this could be a Component::SafePointer, but for any other it works only, if the ListeneeType implements JUCE\_DECLARE\_WEAK\_REFERENCEABLE, which is not the majority.

Seems it enters a terretory where it creates considerable overhead, but I am not the one to say.

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [May 13, 2020, 4:56pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/20 "2020-05-13T16:56:43Z")

</div>

Yeah, I thought about throwing that in, but it would have complicated the example coee.  
Weakreference isn’t expensive to use. The class adds an if() statements when you try to use operator-\> but that’s really it. What kind of overhead were you referring to?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 13, 2020, 10:38pm UTC](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325/21 "2020-05-13T22:38:36Z")

</div>

Oh, I was just thinking that every class that can inherit ChangeBroadcaster needs to be WEAK\_REFERENCEABLE, so that would mean add it to all classes.

Not the processing overhead, so overhead was probably the wrong word, I meant massive changes.

Thinking about it again, maybe adding the WEAK\_REFERENCEABLE in the ChangeBroadcaster class is an option?

[Next page](https://forum.juce.com/t/changebroadcaster-addlistener-listener-lambda/39325.md?page=2)
