1

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:

  1. Not require implementing virtual interfaces

  2. I don't need thread safety

  3. Convenient registration of event handlers

  4. 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 of Font)

  5. 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.

user1244932
  • 7,352
  • 5
  • 46
  • 103

1 Answers1

0

Yes, this alsready exists in e.g.:

Boost Signals2

There are many samples that should get you going.

Signals2 does offer thread safety, but I guess that doesn't harm.

Alternatively,

Qt

Qt does have a signals/slots implementation, as do many other UI frameworks:

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I update my question with description why Qt not suitable for my case. And I can not see how in boost signals2 I can implement (4)[caching of last changed value + 3 simple additional of new events at the same time], can you please describe? – user1244932 Nov 10 '15 at 00:52
  • `(4)[caching of last changed value + 3 simple additional of new events at the same time]` read as `(4)[caching of last changed value + (5) simple additional of new events at the same time]` – user1244932 Nov 10 '15 at 01:00
  • I implemented required functionality on top of boost signals2, details here: http://stackoverflow.com/q/33640124/1244932 – user1244932 Nov 12 '15 at 12:39