Beyond the C ++ standard library: An Introduction to boost-library 12.1 Signals

Source: Internet
Author: User
Library 12. Signals

    How does the signals library improve your program?

    How does signals apply to standard databases?

    Signals

    Usage

    Signals Summary

    Tail note

 

How does the signals library improve your program?
  • Flexible multi-point callback for functions and function objects

  • Robust triggers and event processing mechanisms

  • Compatible with function object factories, such as boost. Bind and boost. Lambda

The boost. Signals library embodies the signals and slots. The signal refers to something that can be "throttled", and the slot is the connector that receives the signal. This is a well-known design pattern with some other namesObserver, signals/slots, Publisher/subscriber, eventsAnd event targets. This design pattern can be used in a variety of situations; the most common is in Gui code, used to make specific actions (for example, a user clicks a button) loose connection with other actions (button changes its appearance and executes a business logic. Signals and slots are useful in many cases. The Triggering Conditions for decoupling actions (signals) and Code for processing them (one or more slots ). It can be used to dynamically change the behavior of processing code, allow the same signal to correspond to multiple processes, or reduce type dependence through the abstract association between one signal and the slot type. By using boost. signals, you can create some signals to receive slots for any given function feature, that is, the slots accept any type of parameters. This method makes the database very flexible; it is suitable for any range of signal requirements. By decoupling the signal source from the processor, the system becomes more robust in both physical and logical dependencies. It allows the signal type to be completely ignorant of the slot type, and vice versa. This is necessary for higher levels of reusability, and it helps to break the dependency loop. Therefore, a signal and slot library are not only related to object-oriented callback, but also to the robustness of the entire system using it.

 

How does signals apply to standard databases?

There is no callback tool in the C ++ standard library, which is obviously needed. Boost. signals is designed with the same attitude as the standard library. It is an outstanding extension of the standard library toolbox.

 

Signals

Header file: "Boost/signals. HPP"

A single header file contains the entire library.

"boost/signals/signal.hpp"

ContainsSignal.

"boost/signals/slot.hpp"

ContainsSlotClass definition.

"boost/signals/connection.hpp"

Contains classesConnectionAndScoped_connection.

To use this library, you can include header files."Boost/signals. HPP"To make sure the entire library is available, or you can include a separate header file as required. The core part of the boost. Signals library is defined in the namespace.Boost, Advanced features are defined inBoost: Signals.

Below isSignalLater, we will briefly introduce the most important member functions. For a complete reference, see the online document of signals.

namespace boost {

template<typename Signature,
// Function type R(T1, T2, ..., TN)
typename Combiner = last_value<R>,
typename Group = int,
typename GroupCompare = std::less<Group>,
typename SlotFunction = function<Signature> >
class signal : public signals::trackable,
private noncopyable {
public:
signal(const Combiner&=Combiner(),
const GroupCompare&=GroupCompare());

~signal();

signals::connection connect(const slot_type&);
signals::connection connect(
const Group&,
const slot_type&);

void disconnect(const Group&);

std::size_t num_slots() const;

result_type operator()
(T1, T2, ..., TN);
};
}

Type

Let's take a look.SignalTemplate parameters. Except the first parameter, other parameters have corresponding default values, which helps you understand the basic meaning of these parameters. The first template parameter is the signature of the called function. In thisSignalS,SignalItself is the called entity. When declaring this signature, use the same syntax as the signature of a common function.[1]. For example,DoubleAnd accept a typeIntThe function signature of the parameter should be as follows:

[1] careful readers have noticedBoost: FunctionThis is also the case.

signal<double(int)>

CombinerA parameter represents a function object.SignalCall all connected slots. It also determines how to combine the results returned by these calls. The default type isLast_valueIt simply returns the call result of the last slot.

GroupsThe parameter is used to combine all connectionsSignalIs a type of slot. By connecting to different slot groups, you can preset the call Slot Sequence and disconnect the slot groups.

GroupcompareParameters determine how to sortGroupsThe default value isSTD: less <group>Generally, it is correct. IfGroupsIf a custom type is used, other sorting methods may be required.

Finally,SlotfunctionThe parameter indicates the slot function type. The default value isBoost: Function. I don't have any reason to change this default value. This template parameter is used to define the slot type. The method is publicTypedef slot <slotfunction> slot_type.

Member Functions
signal(const Combiner&=Combiner(),
const GroupCompare&=GroupCompare());

ConstructSignalYou can inputCombinerIt is an object that calls the corresponding slot when the signal arrives and processes the returned value.

~signal();

The Destructor disconnects all connected slots during the destructor.

signals::connection connect(const slot_type& s);

ConnectFunction push slotSConnectSignal. Function pointer, function object,BindExpressions or lambda expressions can be used as slots.ConnectReturnsSignals: ConnectionIs the handle of the created connection. By using this handle, the slot can beSignalDisconnect, or you can test whether the slot is connected.

signals::connection connect(const Group& g, const slot_type& s);

ThisConnectThe overload version of is similar to the previous one, but it also sets the slotSConnect to groupG. Connecting a slot to a group means that whenSignalWhen it is generated, the slots belonging to the earlier group will be called first (that is, called in the group order,SignalTemplateGroupcompareParameters define the group order), and all slots belonging to the group will be called before slots that do not belong to the group (maybe only some slots are in the group ).

void disconnect(const Group& g);

Disconnect all GroupsGConnected slots.

std::size_t num_slots() const;

Returns the current connectionSignalNumber of slots. To test whether the slot is empty, call the FunctionEmptyInstead of callingNum_slotsAnd test whether the returned value is 0, becauseEmptyHigher efficiency.

result_type operator()(T1, T2, ..., TN);

SignalS is called using the call operator. When a signal is generated, an appropriate parameter must be passed to the call operator.SignalSignature (that is, DeclarationSignalType ). The parameter type must be implicitly converted to the type required by the signal, so that the call can be successful.

There are other types in boost. signals. We will discuss them in the rest of this chapter. We will also discussSignalUsefulTypedefS.

 

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.