/* <Br/> Andy is going to hold a concert while the time is not decided. <br/> Eric is a fans of Andy who doesn' t want to miss this concert. <br/> Andy doesn' t know Eric. <br/> How Can Eric gets the news when Andy's concert is going to take? <Br/> */<br/>/* <br/> SINGER: Observed <br/> fans: observer <br/> */<br/> # include "stdafx. H "<br/> # include <iostream> <br/> # include <boost/signals2.hpp> <br/> # include <boost/bind. HPP> <br/> # include <string> <br/> using namespace STD; <br/> struct singer <br/>{< br/> // defines the signal type, that is to say, Singer needs to know the fans response method <br/> // That is to say, Singer needs to know the method in which fans will respond to the signal sent by singer. <br/> // or singer needs know how fans use to receive signals from singer <br //> // This is equivalent to the "email" that singer needs to know about fans ", only then can the information be delivered to the "Mailbox" type of fans <br/> // The "Mailbox" type of fans -- void (string time) <br/> typedef boost:: signals2: Signal <void (string time)> signaltype; <br/> typedef signaltype: slot_type slottype; <br/> signaltype m_signal; // define a signal </P> <p> // singer publish signal <br/> void publishtime (string time) <br/>{< br/> m_signal (time); // deliver the m_signal signal containing time information to the fans mailbox. Note, before posting, You must contact a specific fans. You must know who owns this type of mailbox. This action is implemented through subscribe. <Br/>}< br/> // singer provides a registration channel through which fans can register and notify singer, if there is a new signal, send me a message <br/> boost: signals2: Connection subscribe (const slottype & fans) <br/> {// establish a connection between fans and singer. <br/> // you can find that fans needs to call this function, that is, if singer is notified of any message through this channel, it should be notified to mE <br/> return m_signal.connect (fans); <br/>}< br/> }; <br/> struct fans <br/> {<br/> // m_connection: the contact exists throughout the lifecycle of the fans. Once the fans disappear, this connection no longer exists <br/> boost: signals2: Scope D_connection m_connection; <br/> // fans response method, that is, the fans mailbox type. Singer does not need to know what to do. <Br/> void correspond (string time) <br/>{< br/> cout <"I know the concert time:" <time <Endl; <br/>}< br/> // fans needs to determine which singer he wants to follow (observe) <br/> void Watch (singer & singer) <br/> {<br/> // call Singer's subscribe function (Channel) to notify singer of its email address <br/> m_connection = singer. subscribe (boost: BIND (& fans: correspond, this, _ 1); <br/>}< br/> }; <br/> int main (INT argc, char * argv []) <br/>{< br/> singerandy; // Liu Dehua <br/> fan Seric; // Eric <br/> Eric. watch (Andy); // Eric informs Andy Lau that I want to follow your trend. Please send me your latest information <br/> Andy. publishtime ("2010/10/01"); // Liu Dehua releases the latest information. Once the information is published, Eric's email -- void correspond (string time) receives the information, and respond to -- cout <.... <Br/> return 0; <br/>}< br/>
Reference:
Http://www.cppprog.com/boost_doc/doc/html/signals2/tutorial.html
Http://www.cppprog.com/2009/0430/111.html
Http://www.cppprog.com/boost_doc/