- #include "stdafx.h"
- #include <iostream>
- #include <boost/signals2.hpp>
- #include <boost/bind.hpp>
- #include <string>
- using namespace std;
- struct Singer
- {
- typedef boost::signals2::signal<void (string time)> signalType;
- typedef signalType::slot_type slotType;
- signalType m_signal;
-
- void PublishTime(string time)
- {
- m_signal(time);
- }
- boost::signals2::connection Subscribe(const slotType& fans)
- {
-
- return m_signal.connect(fans);
- }
- };
- struct Fans
- {
- boost::signals2::scoped_connection m_connection;
- void Correspond(string time)
- {
- cout<<"I know the concert time: "<<time<<endl;
- }
-
- void Watch(Singer& singer)
- {
- m_connection = singer.Subscribe(boost::bind(&Fans::Correspond, this, _1));
- }
- };
- int main(int argc, char* argv[])
- {
- Singer Andy;
- Fans Eric;
- Eric.Watch(Andy);
- Andy.PublishTime("2010/10/01");
- return 0;
- }
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/