Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think it is easier, and more scalable, to define a set of event types that each observer can listen to. Then you register the observer to listen to that particular event type. The observed then keeps a list of observers registered for each event, and notifies them if and when the event occurs. Using a combination of <code>std::function</code>, <code>std::bind</code> (or <code>boost</code> equivalents), it is easy to register callbacks for a given event type. You could put the callbacks in a map of event type to callback.</p> <p>For example, something along these lines (almost pseudo-code, has not been tested)</p> <pre><code>class Publisher { public : void subscribe(const std::string&amp; event, std::function&lt;void(double)&gt; callback) { m_subscribers[s].push_back(callback); } void publish(const std::string&amp; event) const { for (auto&amp; f : m_subscribers[event]) f( some double );} void event(const std::string&amp; event) const { publish(event);} private: // map of event types (here simply strings) to list of callbacks std::map&lt;std::string&amp;, std::list&lt;std::function&lt;void(const std::string&amp;)&gt;&gt;&gt; m_subscribers; }; struct Foo { void foo(double x) { std::cout &lt;&lt; "Foo received message: " &lt;&lt; x &lt;&lt; "\n"; } }; struct Bar { void bar(double x) { std::cout &lt;&lt; "Bar received message: " &lt;&lt; x &lt;&lt; "\n"; } }; int main() { Publisher pub; Foo f0; Foo f1; Bar bar0; pub.subscribe("RED", std::bind(&amp;Foo::foo, &amp;foo0, _1)); pub.subscribe("GREEN", std::bind(&amp;Foo::foo, &amp;foo1, _1)); pub.subscribe("WHITE", std::bind(&amp;Foo::foo, &amp;foo1, _1)); pub.subscribe("RED", std::bind(&amp;Bar::bar, &amp;bar0, _1)); pub.subscribe("BLUE", std::bind(&amp;Bar::bar, &amp;bar0, _1)); pub.subscribe("MAGENTA", std::bind(&amp;Bar::bar, &amp;bar0, _1)); // trigger a "GREEN" event pub.event("GREEN"); } </code></pre> <p>Here, the observers (or subscribers) register to some events, represented by strings here, and their registered callbacks get called when this event happens. In the example above I manually trigger an event to illustrate the mechanism. </p> <p>This event-callback mechanism allows to decouple the actual items from the callback action. The Observed (or publisher) knows what parameter to pass the callback for a given event, and which callbacks to call, so the observers are not dependent on the internal data of the observed object.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload