I want to send messages containing custom data to X amount of “unknown” listeners (i.e. global broadcast to all classes interested in listening to those messages). juce::Message class seems to be designed for this kind of data, but if I understand the documentation correctly, there is an issue there:
Am I right that MessageListener’s postMessage() sends the message only to itself? If this is the case, how do I send the message to X amount of other/unknown classes?
juce::Message probably isn’t necessary for this. You could simply create a singleton class through which other objects can listen for and post messages.
Untested but something along those lines will probably work. Add any additional post methods you need for different types of messages, and corresponding handler methods in the listener.
Please don’t do this.
Wanting to send messages to anywhere and everywhere in your app is almost always a red flag. It would be way too easy for the wrong thing to post the wrong message and your whole app is broken.
A much better solution would be to pass around the actual object that wants to be listened to so you can strictly control access to what objects can post and handle messages.
I had something similar to a mediator in mind. But instead of having a mediator interface class pointer passed to other classes, I would have used a message for the communication. I think I was overthinking the problem. I’ll go with the mediator instead.