Listening to multiple Value objects - Differentiating between?

I seem to have missed something about the “Value” class, as I don’t understand how to differentiate, when having more than one Value object registered as listener in the same class, for example in my PluginEditor which has;

void MutineerAudioProcessorEditor::valueChanged (Value & v)

So right now when either of my two Value objects change value, they call the above same “valueChanged”. So my question is how do I know which Value object made the call?

How about Value::refersToSameSourceAs?

if (v.refersToSameSourceAs(someOtherValue))
   do something relevant


1 Like

I personally like to scope such listeners in objects, each taking it’s own function:

using CallbackFunc = const std::function<void()>;

struct ValueListener : juce::Value::Listener
    ValueListener(juce::Value& valueToUse, const CallbackFunc& callbackToUse)
        : value(valueToUse)
        , callback(callbackToUse)

    ~ValueListener() override { value.removeListener(this); }

    void valueChanged(juce::Value&) override { callback(); }

    juce::Value& value;
    CallbackFunc callback;

And then you can attach different listeners to different objects, also removing the need to call removeListener():

juce::Value a, b;

ValueListener listenerA {a, [] { std::cout << "Value A changed\n"; }};
ValueListener listenerB {b, [] { std::cout << "Value B changed\n"; }};

Great two options, thank you very much both of you!