Typical situation: you have an application, that save and load preferences
(save and load handled by class Config
).
Imagine there's such a preference for fonts of some GUI elements.
So we have
struct Font;
struct Config {
const Font& getFontForSpecificGUIElement() const;
//save to disk if Font changed
void setFontForSpecificGUIElement(Font);
};
Whenever the font is changed I need to inform all instances of class SpecificGUIElement
of that change.
How can achieve such notification functionality?
To keep Config
simple I want to decouple that functionality. Besides, there will many properties like this (30-50 of them).
So in general I want:
Not require implementing virtual interfaces
I don't need thread safety
Convenient registration of event handlers
I need notification for new subscribed receivers, with last recent notification if were any (I mean, as in my example
Font
changed, and when new GUI elements subscribe on such event, they receive, event automatically triggered for them, and they receive last variant ofFont
)Convenient registration of event type (I mean in item above we create some kind of value cache, so it should be simple add new one)
I suppose this is common enough requirements, so may be there already solution, in popular C++ libraries, like boost ?
Update:
Why Qt not suitable for me. I see two approaches
with Qt, QEvent
subsystem. But because of it is impossible
send event to unknown listeners it is no go. Why not signal/slots, first you need inherit QObject
, second to implement (4), I have to create MessageBus
class to cache last values, and adding new events starts require to much work:
add data field, add new signal, add function(X) to emit new signal, because of signal actually protected
functions,
plus in (X) i need compare last value with new one.