How to use Boost::signals::signal

Source: Internet
Author: User

Labored to finish boost::signals ppt. And then it's about doing the exercises.

By speaking PPT, I find it's good to say: You know it's one thing. It's another thing for you to be clear about others. There are really some things you understand, but in language to very difficult to express, is it difficult to express with the language to expose their understanding of what is not a real understanding?

Orz Just after the PPT, really the understanding of boost::singals deepened a layer. Okay, nonsense, don't say more, now to see two exercises (PPT content is basically the Boost official site introduction Boost::singlas use of the chapter, not posted here):

First question:

Topic 1: Achieving a scene in everyday life

Descriptive narrative:

The doorbell rang, the nurse opened the door, and the baby woke up with distress.

Requirements:

There are at least two nurses and two babies. And a nurse going to open the door is a random event (random number). Only when a certain condition is met to open the door, for example, random number Randx meet 500<randx%1000<999 time to open the door, of course, suppose there is a nurse to open the door, the rest of the nurses do not have to open the door! The doorbell rang. Waking a baby is also a random event. When certain conditions are met. The baby woke up crying. Not awake to continue sleeping _~~_.

The problem is to achieve a simple observation of this pattern, assuming that it is not boost::signals to achieve, we have to follow the observation pattern to write, first of all, there must be a base class of observers, and then n-derived observer. The second is to define a base class for the observer, then the M-observer, and the following is an implementation:

<span style= "FONT-SIZE:14PX;" >namespace testring {//Observer base class Cperson{public:cperson (const std::string& vname): M_name (vname) {}virtual ~ CPerson () {}//observer receives notification (trigger signal) Action of Virtual Void Act () {std::cout<< "no action \ n";} Const std::string GetName () const {return m_name;} Private:std::string m_name;};/ /A derived subclass, actual observer class Cnurse:public Cperson{public:cnurse (const std::string& vname): CPerson (vname) {}virtual void AC T () {std::cout<< "Nurse" <<getname () << "Come and open the door\n";}};/ /a derived subclass. Actual Observer class Cbaby:public Cperson{public:cbaby (const std::string& vname): CPerson (vname) {}virtual Void Act () {STD::C out<< "Baby" <<getname () << "weak up and cry\n";}};/ /Observer base class Cring{public:cring () {}virtual ~cring () {}void ring ()//Trigger signal {__NOTIFYV ();} Add observer void Add (cperson* vperson) {m_person.push_back (Vperson);} Private:std::vector<cperson*> m_person;//notify all observers virtual void __notifyv () {Std::vector<cperson*>::iterator it = M_person.begin (); while (It! = M_person.end ()) (*it++)->act ();}}; Class Cguest:public Cring{public:cguest () {}virtual ~cguest () {}};void test () {cguest Guest; Cnurse Nurse ("LiSi"); Cbaby Baby ("Zhangsan"); Guest.add (&nurse); Guest.add (&baby); Guest.ring ();//Trigger Signal}}</span>
Why do you use Boost::signals? The reason is that the above code is highly coupled and can be used boost::signals to reduce coupling:

<span style= "FONT-SIZE:14PX;" > #pragma once#include <algorithm> #include <boost/ref.hpp> #include <boost/bind.hpp> #include <boost/signals2.hpp>namespace test_signal_work{class cgate{typedef boost::signals2::signal<void (bool, Const std::string&) > Signal_type;typedef signal_type::slot_type slot_type;public:boost::signals2:: Connection Connect (const slot_type& vslot) {return m_enterorlivesig.connect (vslot);} void Enter (const std::string& vcarid) {M_enterorlivesig (true, Vcarid);} void Leave (const std::string& Vcarid) {M_enterorlivesig (false, Vcarid);} Private:signal_type M_enterorlivesig;}; Class Ccarinformation{typedef Boost::signals2::signal<void (const std::string&) > Signal_type;typedef Signal_type::slot_type slot_type;typedef std::vector<std::string> cars_type;public:boost::signals2:: Connection Connect (const slot_type& vslot) {return m_sig.connect (vslot);} void Active (bool vEnter, const std::string& vcarid) {vEnter? Enter (Vcarid): Leave (Vcarid);} Do not do this, can be used boost::bindvoid operator () (bool vEnter, const std::string& Vcarid) {active (VEnter, Vcarid);} Private:void Enter (const std::string& vcarid) {cars_type::iterator It = Std::find (M_carsinfo.begin (), m_ Carsinfo.end (), Vcarid), if (It = = M_carsinfo.end ()) {m_carsinfo.push_back (Vcarid); std::cout << "Car" << Vcarid << "enter!" << Std::endl;} Else{m_sig (Vcarid);}} void Leave (const std::string& vcarid) {cars_type::iterator It = Std::find (M_carsinfo.begin (), M_carsinfo.end (), Vcarid); if (It = M_carsinfo.end ()) {std::cout << "car" << vcarid << "leave!" << Std::endl;m_car Sinfo.erase (It);} Else{m_sig (Vcarid);}} Private:signal_type m_sig;cars_type M_carsinfo;}; Class Cguard {public:cguard (const std::string& vname): M_guardname (vname) {}void Active (const std::string& Vcarid) {std::cout << m_guardname << ' knew that ' << ' there is a exception with car ' << Vcarid & lt;< std:: Endl;} void operator () (const std::string& Vcarid) {active (Vcarid);} Private:std::string M_guardname;}; void Test_fun_1 () {Cguard ZS ("Zhangsan"); Cguard LS ("LiSi"); Ccarinformation Info; Cgate Gate1; Cgate Gate2; Gate1.connect (Boost::ref (Info)); Gate2.connect (Boost::ref (Info)); Info.connect (LS); Info.connect (ZS);//Test 1gate1.enter ("CA1001"); Gate2.enter ("CB1002"); Gate1.leave ("CB1002");//Test 2gate2.leave ("CA1003"); Gate1.enter ("CA1004"); Gate2.enter ("CA1004"); Gate1.leave ("CA1004");}} </span>


Look at the second example:

Topic 2: Simulating a simple parking monitor

Descriptive narrative:

When the vehicle enters or leaves the car park, the monitor will receive a notification message (such as a unique mark of the car's license plate number, enter or leave time, where only the vehicle grade is used), so that the monitor can track the entry and exit of every two vehicles, and the monitor may be fraudulent behavior when the alarm notification security.

A simple parking lot monitor can be implemented with three classes: Cgate. Ccarinformation,cguard.

class cgate

{

public :

   void enter (const std :: string & vcarid );

   void leave (const std :: string & vcarid );

};

Cgate must have enter and leave functions. Send the information of the vehicle to ccarinformation.

class ccarinformation

{

Public:

void Active (bool vEnter, const std::string& Vcarid);

Private:

std::vector<std::string>m_carsinfo;

};

Ccarinformation receive cgate information must have an active to take the corresponding measures. For example, the information on the abnormal side of a car issued an alert notification Cguard.

class Span style= "COLOR: #010001" >cguard

{

public :

< Span style= "font-size:14px" >   void active (const std :: string & vcarid );

private :

   std :: string m_guardname ;

};

Assuming ccarinformation alerts, Cguard must react.

Requirements to implement features:

There are two doors in the parking lot. Every door is accessible. and inform the ccarinformation of the vehicle's access information to track each car.

When the same car enters (or leaves two times) two times, an alarm is issued to notify all security guards. Every door has a security guard.

Test examples:

int Main ()

{

// Test 1

Gate1. Enter ("CA1001");

Gate2. Enter ("CB1002");

Gate1. Leave ("CB1002");

// Test 2

Gate2. Leave ("CA1003");

Gate1. Enter ("CA1004");

Gate2. Enter ("CA1004");

Gate1. Leave ("CA1004");

}

Test 1 output:

Car CA1001 enter!

Car CB1002 enter!

Car CB1002 leave!

Test 2 output:

Zhansan knew that there is a exception with car ca1003!

LiSi knew that there is a exception with car ca1003!

Car CA1004 enter!

Zhansan knew that there is a exception with car ca1004!

LiSi knew that there is a exception with car ca1004!

Car CA1004 leave!

This sample is slightly more complex than the previous example:

#pragma once#include <algorithm> #include <boost/ref.hpp> #include <boost/bind.hpp> #include < Boost/signals2.hpp>namespace test_signal_work{class cgate{typedef boost::signals2::signal<void (bool, const std::string&) > Signal_type;typedef signal_type::slot_type slot_type;public:boost::signals2::connection Connect (const slot_type& vslot) {return m_enterorlivesig.connect (vslot);} void Enter (const std::string& vcarid) {M_enterorlivesig (true, Vcarid);} void Leave (const std::string& Vcarid) {M_enterorlivesig (false, Vcarid);} Private:signal_type M_enterorlivesig;}; Class Ccarinformation{typedef Boost::signals2::signal<void (const std::string&) > Signal_type;typedef Signal_type::slot_type slot_type;typedef std::vector<std::string> cars_type;public:boost::signals2:: Connection Connect (const slot_type& vslot) {return m_sig.connect (vslot);} void Active (bool vEnter, const std::string& vcarid) {vEnter? Enter (Vcarid): Leave (Vcarid);} Don't do this, you can.With boost::bindvoid operator () (bool vEnter, const std::string& Vcarid) {active (VEnter, Vcarid);} Private:void Enter (const std::string& vcarid) {cars_type::iterator It = Std::find (M_carsinfo.begin (), m_ Carsinfo.end (), Vcarid), if (It = = M_carsinfo.end ()) {m_carsinfo.push_back (Vcarid); std::cout << "Car" << Vcarid << "enter!" << Std::endl;} Else{m_sig (Vcarid);}} void Leave (const std::string& vcarid) {cars_type::iterator It = Std::find (M_carsinfo.begin (), M_carsinfo.end (), Vcarid); if (It = M_carsinfo.end ()) {std::cout << "car" << vcarid << "leave!" << Std::endl;m_car Sinfo.erase (It);} Else{m_sig (Vcarid);}} Private:signal_type m_sig;cars_type M_carsinfo;}; Class Cguard {public:cguard (const std::string& vname): M_guardname (vname) {}void Active (const std::string& Vcarid) {std::cout << m_guardname << ' knew that ' << ' there is a exception with car ' << Vcarid & lt;< Std::endl;} void operator () (const STD::STring& Vcarid) {active (Vcarid);} Private:std::string M_guardname;}; void Test_fun_1 () {Cguard ZS ("Zhangsan"); Cguard LS ("LiSi"); Ccarinformation Info; Cgate Gate1; Cgate Gate2; Gate1.connect (Boost::ref (Info)); Gate2.connect (Boost::ref (Info)); Info.connect (LS); Info.connect (ZS);//Test 1gate1.enter ("CA1001"); Gate2.enter ("CB1002"); Gate1.leave ("CB1002");//Test 2gate2.leave ("CA1003"); Gate1.enter ("CA1004"); Gate2.enter ("CA1004"); Gate1.leave ("CA1004");}}




How to use Boost::signals::signal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.