Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Boost(2) Signal and Slot

Published on| September 8th, 2009 | No Comment.
Summary:
This article, Qt signals just like you describe.
Based on the sample using Qt signals and slots of the article, Boost I would like to write and get a similar effect in describing how.

Does, immediately, try using the sample source.
Program used here is a sample program that is easy to understand and related sites listed below, some are changed.


Related articles: Qt (2) examine the signals and slots

Sample source code you used here:

Try console application
To ensure easy operation, related article: Qt (2) to create the application as well as examine the console signals and slots.

※ sample source code used here is a related article: Qt (2) The original was changed to examine the signals and slots.

Prepare one sample file 3.

  • sample.cpp
    - Main routine.
  • counter.h
    - The Tesutokurasuhedda.
  • counter.cpp
    - The Tesutokurasusosu.

Let's take a look at their source code.

[counter.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/signals.hpp>
using namespace std;
using namespace boost;
 
class Counter 
{
public:
	Counter(const string sname);
 
	int value() const;
	void clear();
 
//public slots:
public :
	void setValue(int value) ;
	void setValue2(int value) ;
 
//signals:
//	void valueChanged(int newValue);
	boost::signal<void (int)> valueChanged;
 
private:
	int m_value;
	string m_name;
 
};
I have an argument to the constructor of the object name.
This output information below, I think what will happen to help Rika Waka moved.
※ dare, Qt has left some parts of the comment.

Qt,
Slot definition, "public slot:" write later.
Definition of the signal, "signals:" You described later.

However, Boost does not require such a description.

However, Qt provides signal, was one of the methods of treatment, Boost, the signal is the one attribute. (Substance, which is like a chunk of the callback function. )


[counter.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "counter.h"
 
Counter::Counter(const string sname) 
{
	m_value = 0;
	m_name = sname;
}
 
int Counter::value() const
{
	return m_value;
}
 
void Counter::setValue(int value) 
{
	if (value != m_value) {
		m_value = value;
		cout << "send:[" << m_name << "] " << value << endl;
//		emit valueChanged(value);
		valueChanged(value);
		cout << "completed:[" << m_name << "] " << value << endl;
	} else {
		cout << "Ignore:[" << m_name << "] " << value << endl;
	}
}
When you call setValue method, if only to set an attribute with a different number of arguments inside the numbers, it is to issue a signal.
This output information below, I think what will happen to help Rika Waka moved.

If the numbers match the numbers with the internal attributes of the argument is ignored.

We got the name of the output arguments of each object in the root.

Qt provides a signal when you call, "emit" is used in a conventional, Boost does not need anything. Attribute as a function call.
To this area, Boost Rashii and speaking, and I also feel like it.


[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "counter.h"
 
 
int main(int argc, char *argv[])
{
	Counter a("a");
	Counter b("b");
 
//	QObject::connect(&a, SIGNAL(valueChanged(int)),
//	              &b, SLOT(setValue(int)));
//	QObject::connect(&b, SIGNAL(valueChanged(int)),
//	              &a, SLOT(setValue(int)));
 
	a.valueChanged.connect(
			boost::bind(&Counter::setValue, &b, _1));
	b.valueChanged.connect(
			boost::bind(&Counter::setValue, &a, _1));
 
	a.setValue(12);
	b.setValue(48);
}

Qt, if you do connect,
Signal the occurrence of an object, signaling method, the object receiving slot, the method was defined as the slot.

Boost in, connect the signal directly to the object that defines a function slots.
Here, it is treated as a function slots, boost:: bind using the object + will be able to notify the appropriate signal to the object that is defined as the method raise.

Here boost:: bind well, Boost is a feature, is commonly used. boost:: bind is a man and I think we may wish to check often.

※ Since the articles: Qt (2) from the slot and check the signal is extracted and described where we are operating in this sample.

Now, the source code line 10 to 13 QObject:: connect There is a description, please see frequently.

a. valueChanged -> b. setValue flow to line 10.
b. valueChanged -> a. setValue flow to line 12.

a, b., each together to issue a signal to receive. Definition of cross-coupled. However,, [counter.cpp] Slot (receiver) setValue in the signal, and issues.

In other words, a. ValueChanged -> b. SetValue -> b. ValueChanged -> a. SetValue Nearby, finally, is not to issue a signal from the top and is expected to continue the process indefinitely.

However, [counter.cpp] Slot (receiver) setValue, if the numbers match the numbers with the internal attributes of the argument is ignored.
Therefore,陥Rimasen infinite loop.

In addition, this process is easy to fall into a deadlock handling well. The process is called to meet each other.
 オブジェクト a   -->   オブジェクト b  
                 <--      
Now, how will this run result. Do you think it will be processed.
Previous Qt applications as well as create a console, create a console application, but here, let's run.

1
C:\temp> qmake "CONFIG+=console"
The following is the result of its execution.

1
2
3
4
5
6
7
8
9
10
send:[a] 12
send:[b] 12
Ignore:[a] 12
completed:[b] 12
completed:[a] 12
send:[b] 48
send:[a] 48
Ignore:[b] 48
completed:[a] 48
completed:[b] 48

Worked to clean. But like a fine thread, in this process is normally to be OK.
(Which I tried here, a single task, single-CPU environment because, really, and should not try to do many more things)



As described here, Boost version will work.

In this way, Boost signals, slots, Qt as it's replaced, it almost seems. Originally, Qt signals, is modeled after the slot, so there is a history from Deu implemented, it may be a natural.

Also, Boost in 1.4.0, Signal2 has appeared. The sample, as well Signal2 that has been mentioned in the same source code.
Come and please your reference.



Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ