[quote=“steffen”][quote=“jules”]At some point I’ll probably remove it.
[…]…so a better choice for that parameter would probably have been a ChangeBroadcaster*, but in fact I don’t think I ever use it at all, and it’s probably better if you don’t either![/quote]
I really hope you’re only thinking about the parameter in ChangeBroadcaster::send(Synchronous)ChangeMessage. I’m using it a lot, and I can’t think of a single time I passed it anything else than ‘this’.
But some of my classes heavily rely on that they can attach themselves to multiple ChangeBroadcasters, and can distinguish which one has changed by that parameter. So removing this parameter in ChangeListener::changeListenerCallback would mean heavy refactoring for me. I’d be happy to see the type to change to ChangeBroadcaster* though, as currently I wasn’t able to dynamic_cast the void* pointer (in cases where my ChangeListener knows what type to expect, and wants to call a specific method of that type)[/quote]
My humble opinion here is that using dynamic_cast<> and relying on RTTI (what I mentioned above) is actually the same thing, so it’s far from ideal (many blog articles explain why, but basically, it’s not really standard so implementations might vary depending on which compiler you use, and it’s not efficient)
Looking for inspiration, I had a look at juce’s button class and I really liked the way events are implemented.
Baiscally, Jules defines a class Button::Listener inside Button and this class defines 2 pure virtual methods :
virtual void buttonClicked (Button* button) = 0;
virtual void buttonStateChanged (Button*) =0
Then if you make your main component inherit from Button::Listener, you have to implement those 2 methods, and then in your implementation, you definitely know you’re dealing with a button.
Basically, instead of having a single “catch-all” changeListenerCallback method with a bunch of conditional statements to determine which object actually changed, you have different listener methods per object type (Button::Listener::buttonClicked(), MyStuff::Listener::myStuffChanged() and so on …) , and therefore,
- you don’t need RTTI
- you don’t have if or switch statements (so better efficiency)
- you can choose whch parameters you want to pass to your callback instead of relying on a single void* ( MyStuff::Listener::myStuffChanged(int parameter1, someclass parameter2)
- you have to maintain your own listener list and manually call the callbacks, however, most of this work is already done in Juce (see ListenerList)