Sigslot Introduction _ Other

Source: Internet
Author: User
The most recent project uses the Libjingle,libjingle-bottom package message mechanism, based on its has_slots, searches for its data discovery is a very useful C + + library, first to its brief introduction.

First, the slot mechanism. Slot systems are commonly used in three kinds: boost signals,sigslot,sigc++ basic skills: 1.connect 2.disconnect 3.emit   Sigslot advantages 1. Don't worry about empty callbacks, Automatically disconnect 2 when the callback object is destructor. Supports multiple threads, thread safety, lock 3.   Disadvantages 1. Only the void type function can be recalled, and the return value is not supported. The signals library schema in boost is similar to supporting the return value, but the introduction of other library 2.slot in boost has no priority, and the sequential   slot functions in the callback queue cannot be dynamically adjusted (the callback function) is a normal member function, with the following limitations: 1. The return value must be void 2, the slot parameter range is 0-8 3, the class that implements the slot must inherit from the Has_slots<> 1,2 is the limitation of the Sigslot Library author, and the author makes the decision after weighing various factors, If you feel the need you can modify the Sigslot code to cancel the limit, and   3 is the sigslot mechanism basis that must be adhered to unless you write yourself a sigslot   NOTE: The design of the Sigslot library, when sending a signal without a connection, Without any processing, there will be no errors issued. The following is a basic introduction to the Sigslot library. 1.          profile       Sigslot is a thread-safe, type-safe, with C + + implementation of the Sig/slot mechanism (Sig/slot mechanism is the mechanism of sending and receiving messages between objects) open source code base. is a very useful library, only a header file sigslot.h. 2.    Sigslot Instance       modern C + + projects typically contain a large number of C + + classes and objects, which are called between objects through member functions, with the disadvantage that when classes and objects are of a large scale, Each other must remember which interfaces are provided, and the details of the interface, which are inconvenient. For example: We have a switch class and a light class, and now we need to correlate the two, that is, by using the Switch to control the state of the light, we may need to add a different class Toggleswitch to associate the two: class Switch { Public:      virtual void clicked () = 0; }; Class Light {public:      void Togglestate ();      void Turnon ();   & nbsp;  void turnoff (); }; Class Toggleswitch:public Switch {public:      Toggleswitch (light& lp) {M_LP = LP;}       virtual void clicked () {M_lp. Togglestate ();} Private:      light& M_LP; }; Light LP1, LP2; Toggleswitch TSW1 (LP1), TSW2 (LP2); This is fully functional, but imagine the amount of work that would be less common if there were a large number of classes that needed to interact with each other.   Use the sig/slot mechanism to solve the above situation, do not need to care about the interface details of the association class, sigslot implementation of the switch and light the above functions are as follows: class Switch {public:       Signal0<> clicked; }; Class Light:public has_slots<> {public:      void Togglestate ();      vo ID Turnon ();      void turnoff (); }; Switch SW1, SW2; Light LP1, LP2;          Sigslot mechanism to implement this function compared to the first method, the switch class has a SIGNAL0 member, and the light class needs to be from has_slots&Lt;> inheritance, nothing else changes, but eliminates the toggleswitch of writing an inheritance class to implement the two associations. Here is a simple code to implement the functionality.   #include <iostream> using namespace std;   #include "sigslot.h" using namespace Sigslot; You must add the Sigslot namespace//When you use VS debugging, you need to add typename const int TRUE = 1 For many of the custom template structure types in sigslot.h. const int FALSE = 0; Class Switch {public:        signal0<> clicked///the signal here is without parameters, SIGNALN is represented with several parameters}; Class Light:public has_slots<> {public:        Light (bool state) {b_state = State;d Isplaystate ();}        void Togglestate () {b_state =!b_state;displaystate ();}//Response to message         void Turnon () {b_state = True;displaystate ();}        void Turnoff () {b_state = False;displaystate ();}        void Displaystate () {cout<< " The ' is ' <<b_state<<endl;} Private:        bool b_state; }; void Main () {       Switch SW1, Sw2,all_on,all_off;        Light LP1 (TRUE), LP2 (FALSE);        SW1. Clicked.connect (&lp1,&light::togglestate); Bindings        SW2. Clicked.connect (&lp2,&light::togglestate);        all_on. Clicked.connect (&lp1,&light::turnon);        all_on. Clicked.connect (&lp2,&light::turnon);        All_off. Clicked.connect (&lp1,&light::turnoff);        All_off. Clicked.connect (&lp2,&light::turnoff);          SW1. Clicked ();        SW2. Clicked ();        all_on. Clicked ();        All_off. Clicked ();          SW1. Clicked.disconnect (&AMP;LP1);        SW2. Clicked.disconnect (&AMP;LP2);        all_on. Clicked.disconnect (&AMP;LP1);        all_on. Clicked.disconnect (&AMP;LP2);        All_off. Clicked.disconnect (&AMP;LP1);        All_off. Clicked.disconnect (&AMP;LP2); }     3  .   parameter type         Sig/slot can be either with parameters or without, with a maximum of 8 parameters. In retrospect, the signal0<> clicked of the switch class, called the SIG, is used to emit the signal, while the member function of the class light that inherits has_slots<> is void Togglestate () turnon () Turnoff (), called slot, is the signal processing function. The core of Sigslot is here, through the two sets of correspondence to achieve the message interaction between objects. A sig is a member variable that is shaped like   signal+n<type1,type2......>     followed by n means that signal can receive several parameters, of any type, up to 8. This is specified in the library, of course, if the actual development requires more parameters, you can modify the Sigslot library. Slot is a member function that is shaped like: void Slotfunction (Type1,type2 ...)    need to remember that the slot class must inherit the has_slots<>; member function's return value must be a void type. This is the limitations of this library, of course, if the actual development needs to return the value, can also modify the Sigslot library to implement. It also needs to be noted that slot's prototype needs to be consistent with the sig. How to say, is that signal can only with the same number of parameters with the SThe lot function is bound, and the signal parameters are passed directly to slot.   4.    Sigslot Library usage   Send signal (SIG, that is, sig/slot SIG, the signals mentioned below equate to this meaning): Signal1<char *, int> ReportError; For example, the above one reporterror this signal when calling ReportError ("something went Wrong", Err_something_wrong); The ReportError emit member function is automatically invoked to emit a signal. Sent to whom. The   connection information number establishes the correspondence between SIG and slot by calling the sig's connect function. The Connect function receives two parameters, one is the address (pointer) of the message destination object, and the other is the member function pointer (slot) of the destination object. In order for the entire mechanism to work effectively, the destination class must inherit from Has_slots<>, and the Sig/slot parameter type must be consistent. A sig can also be connected to multiple slot so that each connection slot receives the signal each time the sig is signaled.   Disconnect signal connections between the sig and slot by invoking the disconnect function of the sig, with only one parameter: The address of the destination object. In general, you do not need to call the Disconnect function explicitly, and the disconnect disconnects the sig and slot are automatically invoked in the SIG class and the class of the destination class (which contains slot functions). You can also disconnect all slot of the sig using Disconnect_all.   ALL_ON. Clicked.connect (&lp1,&light::turnon); all_on. Clicked.connect (&lp2,&light::turnon);/Ibid. all_on. Clicked.disconnect_all ();  

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.