What is a signal slot?

  • Simply put, the signal slot is an implementation, or a distillation, of the observer mode.
  • A signal is an event that can be observed, or at least a notification that the event has occurred; A slot is an observer, usually a function that is called when the object being observed changes — so to speak, when a signal is emitted. You can connect the signal to the slot and form an observer-observed relationship; Signals are emitted when events or states change; At the same time, the signaler is obligated to call all registered functions (slots) that are interested in the event (signal).
  • Signals and slots are many-to-many. A single signal can be connected to multiple slots, and a slot can listen for multiple signals.
  • In addition, the signal can have additional information.

Use of signal slot

  • Signal slots are great tools, but how can you use them better? There are three things to note about direct function calls.
    • A signal slot call may take more time/space than a direct function call;
    • You may not be able to use inline;
    • It may not be friendly to code readers.
  • The biggest benefit we gain from using signal slots for decoupling is that the objects at both ends of the connection do not need to know anything about each other.
  • You can implement an application in which every function call is triggered by a signal. This is technically perfectly fine, but it is unlikely to be feasible, as the use of signal slots will undoubtedly compromise code readability and system performance. How you balance this is also important to consider.

# # sigslot library

  • The official address

  • Signal slot systems in C++ are commonly used in three ways: boost signals,sigslot,sigc++. The SIGSlot library is relatively simple to use.

  • Sigslot is a thread-safe, type-safe, C++ open source library for the sig/slot mechanism, which is the mechanism for sending and receiving messages between objects. There is only one header file sigslot.h.

  • The basic functions are:

    1. connect
    2. disconnect
    3. emit
  • Sigslot advantages

    1. Don’t worry about null callback, it will automatically disconnect when the callback object is destructed
    2. Support multithreading, thread safety, locks
  • Sigslot shortcomings

    1. Only void functions can be called. No return value is supported. The Signals library in Boost has a similar architecture, supporting return values but introducing other libraries in Boost
    2. Slot has no priority and cannot be dynamically reordered in the callback queue
  • Slot functions (called functions) are normal member functions, with the following limitations:

    1. The return value must be void
    2. The number of slot parameters ranges from 0 to 8
    3. Classes implementing slots must inherit from has_slots<>
  • The first two are the sigslot library author restrictions, which the author has made after weighing various factors. You can modify the SIGslot code to remove the restrictions if you feel it is necessary, while the last one is the mechanical basis of sigslot and must be followed unless you write a new SIGslot yourself.

  • Note that the SIGSlot library is designed so that when an unconnected signal is sent, no processing is done and no errors are emitted.

Basic usage mode

  • Include header file
#include "sigslot.h"
Copy the code
  • Change (” Typename must precede nested dependency type names “)
// In sigslot.h 420, will:
typedef sender_set::const_iterator const_iterator;
    / / changed to:
typedef typename sender_set::const_iterator const_iterator;
Copy the code
  • Signal0 to signal8: Signal class: as a member of the class
class mySg
{
    sigc::signal0<>                 sg1;    / / no parameters
    sigc::signal2<char*, double="">    sg2;    // 2 parameters
}
Copy the code
  • Connection (slot function: as a class member, the class needs to inherit has_slots<>, and the return value of slot function must be void)
class mySlot: public : has_slots<>
{
public:
    void on_func1(a){}                       // No parameter, corresponding to signal
    void on_func2(char*, double)(a){}        // 2 parameters
};
mySg    sig;
mySlot  slt;
sig.sg1.conncent(&slt,&mySlot::on_func1);
sig.sg2.conncent(&slt,&mySlot::on_func2);
Copy the code
  • Disconnection (Disconnection: use disconnect() and disConnect_all ())
sig.sg1.disconnect(&slt);
sig.sg1.disconnect_all();
Copy the code
  • Emiting (Sending signals: you can either use the () operator directly or call signal’s emit function)
sig.sg1.emit();
sig.sg2("str".0.1);
Copy the code