dirk/C++/glibmm/sigc++-connection
Weingarten, 10-02-12
sigc++-connection – Managing the Connection
Yesterday, I learnt about importance of disconnect.
(If the signal handler is a method of a deleted instance which is still connected, the next signal emit will call this method using a wild instance pointer.
This is
For my luck, I use smart pointers and got the point, immediately.
)
I'm not quite sure about the behavior of the sigc::connection .
A little bit more investigation could be helpful.
Source Code
sigc++-connection.cc |
|---|
/** @file sample for sigc++ connection management
*
* Last CVS checkin:
* $Date: $
* $Author: scheff $
*/
/**************************************************************************/
// standard C/C++ header:
#include <iostream>
#include <string>
// Gnome header:
#include <sigc++/sigc++.h>
#include <glibmm/init.h>
using namespace std;
/**************************************************************************/
/// a signal handler
static void onSig(void) { cout << "onSig()" << endl; }
/**************************************************************************/
/** waits for user confirm.
*
* @param action text with next action
*/
static void pause(const char *action)
{
cout << "Press [ENTER] to " << action << ": " << flush;
string str; getline(cin, str);
}
/** provides the main function of application.
*
* @param argc number of command line arguments
* @param argv pointer to array of pointers to strings with command line
* arguments
* @return 0 ... application exited regularly\n
* else ... execution of application aborted
*/
int main(int argc, char *argv[])
{
Glib::init();
pause("start");
{
sigc::signal<void> sig;
{
cout << "1st connection:" << endl;
sigc::connection connection = sig.connect(sigc::ptr_fun(&onSig));
cout << "Emit signal:" << endl;
sig();
{
cout << "2nd connection:" << endl;
sigc::connection connection2 = connection;
cout << "Emit signal:" << endl;
sig();
} cout << "Life time of 2nd connection ended" << endl;
cout << "Emit signal:" << endl;
sig();
} cout << "Life time of 1st connection ended" << endl;
cout << "Emit signal:" << endl;
sig();
}
pause("continue");
{
sigc::signal<void> sig;
{
cout << "1st connection:" << endl;
sigc::connection connection = sig.connect(sigc::ptr_fun(&onSig));
cout << "Emit signal:" << endl;
sig();
{
cout << "2nd connection:" << endl;
sigc::connection connection2 = connection;
cout << "Emit signal:" << endl;
sig();
cout << "Disconnect 2nd connection:" << endl;
connection2.disconnect();
cout << "Emit signal:" << endl;
sig();
} cout << "Life time of 2nd connection ended" << endl;
cout << "Disconnect 1st connection:" << endl;
connection.disconnect();
cout << "Emit signal:" << endl;
sig();
} cout << "Life time of 1st connection ended" << endl;
cout << "Emit signal:" << endl;
sig();
}
// done
pause("finish");
return 0;
}
|
Output
| Console |
|---|
Press [ENTER] to start: ↵
1st connection:
Emit signal:
onSig()
2nd connection:
Emit signal:
onSig()
Life time of 2nd connection ended
Emit signal:
onSig()
Life time of 1st connection ended
Emit signal:
onSig()
Press [ENTER] to continue: ↵
1st connection:
Emit signal:
onSig()
2nd connection:
Emit signal:
onSig()
Disconnect 2nd connection:
Emit signal:
Life time of 2nd connection ended
Disconnect 1st connection:
Emit signal:
Life time of 1st connection ended
Emit signal:
Press [ENTER] to finish: ↵
|
Conclusion
Copy or assignment of connections doesn't influence how often the signal handler is called.
According to the manual, a disconnected sigc::connection may be disconnected again without any (negative) effect.
Files
Last modified: Sun Feb 21 15:03:04 2010