๋๋ ํ ํ์ด ์๋ ์ฅ์ ์ธ์ด์ง๋ง ์ธ์์ ๋ฉ์ฉกํ ๋ชธ์ ๊ฐ์ง ์ ์ ์ ์ธ ์ฅ์ ์ธ์ด ์ผ๋ง๋ ๋ง์๊ฐ - ๊น์ ๊ธฐ ์ค๋ด์ฒด์ก๊ด ๊ด์ฅ ๋ฏธ๋์ด ๋ค์ ์ธํฐ๋ทฐ์์. ํ์ชฝ ํ์ด ์๋ ๋ถ๊ตฌ์ ๋ชธ์ผ๋ก ๋ฌด์ํ์ด ๊ฒฉํฌ๊ธฐ ํ์ญ์ ๋ฐ๋ ์์ ์ ์๊ธฐํ๋ฉฐ.
๏ปฟ * ์๋ฌธ๋งํฌ :
http://libsigc.sourceforge.net/libsigc1_2/manual/html/
http://libsigc.sourceforge.net/libsigc1_2/manual/html/
1.1 ์ ์๋๊ธฐ #
There are many situations in which it is desirable to decouple code that detects an event, and the code that deals with it. This is especially common in GUI programming, where a toolkit might provide user interface elements such as clickable buttons but, being a generic toolkit, doesn't know how an individual application using that toolkit should handle the user clicking on it.
C์ธ์ด์์ ์ฝ๋ฐฑ์ ์ผ๋ฐ์ ์ผ๋ก ์ดํ๋ฆฌ์ผ์ด์
์ด ํจ์ ํฌ์ธํฐ์ void * ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง '๋ฑ๋ก' ํจ์๋ฅผ ํธ์ถํจ์ผ๋ก์ ๋ค๋ฃจ์ด์ง๊ฒ ๋ฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ๊ฒ ์ฃ .
void clicked(void *data);
button * okbutton = create_button("ok");
static char somedata[] = "This is some data I want the clicked() function to have";
register_click_handler(okbutton, clicked, somedata);
ํด๋ฆญ๋๋ฉด, ํดํท์ register_click_handler() ํจ์์ ์ ๋ฌ๋ data ํฌ์ธํฐ๊ฐ์ ์ฌ์ฉํ์ฌ clicked()์ ํธ์ถํ๊ฒ ๋ ๊ฒ์
๋๋ค.
์ด๊ฒ์ C์์ ๋์ํ๊ธด ํ์ง๋ง, ํ์
์์ ์ฑ์ ๋ณด์ฅํด์ฃผ์ง ์์ต๋๋ค. ๋ค์๋งํ์๋ฉด cliecked()์๋ char *๋ง๊ณ ๋ค๋ฅธ ํํ์ ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ๋ฃ์ง ์๋๋ค๋ ๊ฒ์ ์ปดํ์ผ์์ ์์ ๋ณด์ฅํด์ฃผ์ง ์์ต๋๋ค.
C++ ํ๋ก๊ทธ๋๋จธ์ ์
์ฅ์์๋ ํ์
์์ ์ฑ์ ๋น์ฐํ ์ํ๊ฒ ๋ฉ๋๋ค. ๋ํ ์ฝ๋ฐฑ์ผ๋ก์ ์์ ๋กญ๊ฒ ๋
๋ฆฝ๋ ์ ์ญ ํจ์๋ณด๋ค๋ ๋ค๋ฅธ ๊ฒ์ ์ฌ์ฉํ๋ ค๊ณ ํ ๊ฒ์
๋๋ค.
libsigc++์ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋ฐฑ์ผ๋ก์ ์ฌ์ฉ๋ ์ ์๋ ๋์๋ค์ค ํ๋์ ๋ํ ์ฐธ์กฐ๋ฅผ ๋ด์๋๋ ์ฌ๋กฏ์ ๊ฐ๋
์ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
- ์์ ์์์์ ๊ฐ์ ๋ ๋ฆฝ ์ ์ญํจ์
- ์ฐ์ฐ์ ()๋ฅผ ์ค๋ฒ๋ก๋ฉํ ๊ฐ์ฒด ํฌ์ธํฐ(ํจ์์(functor))
- ๋งด๋ฒ ๋ณ์ ํฌ์ธํฐ์ ์ด๋ฅผ ํธ์ถํ ๊ฐ์ฒด ์ธ์คํด์ค(๊ฐ์ฒด๋ SigC::Object๋ก๋ถํฐ ์์๋ฐ์์ผ๋ง ํฉ๋๋ค)
์ด๋ฌํ ์์
๋ค์ ๊ตฌ์ถํ๊ธฐ ์ฝ๊ฒ ํ๊ธฐ์ํด, slot์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ์ค๋ฒ๋ก๋ฉ๋ ํ
ํ๋ฆฟ ํด๋์ค๊ฐ ์ ๊ณต๋ฉ๋๋ค. slot์ ๋งค๊ฐ๋ณ์(๋๋, ํ์ํ ๋งค๊ฐ๋ณ์๊ฐ ๋ด๊ฒจ์ ธ์๋ ๊ณณ)๋ฅผ ๊ฐ์ง๋ฉฐ ์ฐ์ฐ์()๋ฅผ ์ฌ์ฉํ์ฌ ํธ์ถ๊ฐ๋ฅํ ์ผ๋ฐ Slot ํ์
์ ๋ฐํํฉ๋๋ค.
๋ฐ๋์ ๊ฐ๋
์ผ๋ก, libsigc++์ ์ ํธ(signal)์ ์ ๊ณตํ์ฌ, ํด๋ผ์ด์ธํธ๊ฐ ์ฌ๋กฏ๋ค์ ์ ๊ทผํ ์ ์๋๋ก ํฉ๋๋ค. ์ ํธ๊ฐ ์ผ์ง๋ฉด(emit), ๋ชจ๋ ์ฐ๊ฒฐ๋ ์ฌ๋กฏ๋ค์ ์ญํธ์ถ๋ฉ๋๋ค.
2.1 ๊ฐ๋จํ ์์ #
The terminology for signals and slots has come under some criticism for not being as intuitive as it maybe could have been, but with a little experience it won't cause a problem. Honest.
So to get some experience, lets look at a simple example...
Lets say you and I are writing an application which informs the user when aliens land in the car park. To keep the design nice and clean, and allow for maximum portability to different interfaces, we decide to use LibSigC++ to split the project in two parts.
I will write the AlienDetector class, and you will write the code to inform the user. (Well, OK, I'll write both, but we're pretending, remember?)
Here's my class:
class AlienDetector
{
public:
AlienDetector();
void run();
SigC::Signal0<void> detected;
};
(I'll explain the type of detected later.)
Here's your code that uses it:
void warn_people()
{
cout << "There are aliens in the carpark!" << endl;
}
int main()
{
AlienDetector mydetector;
mydetector.detected.connect( SigC::slot(warn_people) );
mydetector.run();
return 0;
}
Pretty simple really - you call the connect() method on the signal to connect your function. connect() takes a slot parameter (remember slots are capable of holding any type of callback), so you convert your warn_people() function to a slot using the slot() function.
To compile this example from the downloadable example code, use:
g++ example1.cc -o eg1 `pkg-config --cflags --libs sigc++-1.2`Note that those `` characters are backticks, not single quotes. Run it with ./eg1(Try not to panic when the aliens land!)
2.2 ๋งด๋ฒํจ์๋ฅผ ์ฌ์ฉํ๊ธฐ #
Suppose you found a more sophisticated alien alerter class on the web, such as this:
class AlienAlerter : public SigC::Object
{
public:
AlienAlerter(char const* servername);
void alert();
private:
// ...
};
(Handily it derives from SigC::Object already. This isn't quite so unlikely as you might think; all appropriate bits of the popular gtkmm library do so, for example.)
You could rewrite your code as follows:
int main()
{
AlienDetector mydetector;
AlienAlerter myalerter("localhost"); // added
mydetector.detected.connect( SigC::slot(myalerter, &AlienAlerter::alert) ); // changed
mydetector.run();
return 0;
}
Note that only 2 lines are different - one to create an instance of the class, and the line to connect the method to the signal.
This code is in example2.cc, which can be compiled in the same way as example1.cc
2.3 ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง ์ ํธ #
Functions taking no parameters and returning void are quite useful, especially when they're members of classes that can store unlimited amounts of safely typed data, but they're not sufficient for everything.
What if aliens don't land in the carpark, but somewhere else? Let's modify the example so that the callback function takes a std::string with the location in which aliens were detected.
I change my class to:
class AlienDetector
{
public:
AlienDetector();
void run();
SigC::Signal1<void, std::string> detected; // changed
};
The only line I had to change was the signal line (in run() I need to change my code to supply the argument when I emit the signal too, but that's not shown here).
The name of the type is 'SignalN', where N is the number of arguments that the slots should take. The template parameters are the return type, then the argument types.
libsigc++์ Signal ํ
ํ๋ฆฟ ๋งค๊ฐ๋ณ์์ ์๋ฅผ ๋ฌดํํ๊ฒ ์ ์ํ๋๋ก ํ๊ณ ์์ง ์๊ณ , ๋ช
์์ ์ผ๋ก Signal0์์ Signal5๊น์ง ์ ์ํ๊ณ ์์ผ๋ฉฐ ์ด์ ๋๋ผ๋ฉด ๋๋ถ๋ถ์ ์ฌ๋๋ค์ด ์ถฉ๋ถํ๋ค๊ณ ์๊ธฐํ๊ณ ์์ต๋๋ค.(๋ง์ฝ M4 ์คํฌ๋ฆฝํธ์ ๋ํด ์ง์์ด ์๋ค๋ฉด, ์์ค๋ฅผ ๊ณ ์ณ ํ์ํ๋งํผ ๋ ๋๋ฆด์๋ ์์ต๋๋ค)
The types in the function signature are in the same order as the template parameters, eg:
SigC::Signal1<void, std::string>
void function(std::string foo);
So now you can update your alerter (for simplicity, lets go back to the free-standing function version):
void warn_people(std::string where)
{
cout << "There are aliens in " << where << "!" << endl;
}
int main()
{
AlienDetector mydetector;
mydetector.detected.connect( SigC::slot(warn_people) );
mydetector.run();
return 0;
}
Easy.
2.4 ์ฐ๊ฒฐํด์ #
If you decide you no longer want your code to be called whenever a signal is emitted, you must remember the return value of connect(), which we've been ignoring until now.
connect() returns a SigC::Connection object, which has a member disconnect(). This does just what you think it does.
3.1 ๋นจ๋ฆฌ ๋ชจ์๋ฅผ ๋ฐ๊ฟ์ฐ๊ธฐ(recap) #
If all you want to do is use gtkmm, and connect your functionality to its signals, you can probably stop reading here.
You might benefit from reading on anyway though, as this section is going to be quite simple, and the 'Rebinding' technique from the next section is occasionally useful.
We've already covered the way the types of signals are made up, but lets recap:
A signal is an instance of a template, named SigC::SignalN where N is the number of arguments taken, 0-5. The template arguments are the types, in the order they appear in the function signature that can be connected to that signal; that is the return type, then the argument types.
To provide a signal for people to connect to, you must make available an instance of that SigC::Signal. In AlienDetector this was done with a public data member. That's not considered good practice usually, so you might want to consider making a member function that returns the signal by reference. (This is what gtkmm does.)
Once you've done this, all you have to do is emit the signal when you're ready. Look at the code for AlienDetector::run():
void AlienDetector::run()
{
sleep(3); // wait for aliens
detected.emit(); // panic!
}
As a shortcut, Signal defines operator() as a synonym for emit(), so you could just write detected(); as in the second example version:
void AlienDetector::run()
{
sleep(3); // wait for aliens
detected("the carpark"); // this is the std::string version, looks like
// they landed in the carpark afterall.
}
3.2 ๋ฐํ๊ฐ์ด ๋ญ์ง? #
If you only ever have one slot connected to a signal, or if you only care about the return value of the last registered one, it's quite straightforward:
SigC::Signal0<int> somesignal; int a_return_value; a_return_value = somesignal();If you care about every return value things are a little more complicated. See the section on Marshallers for more info.
4.1 ๋ค์ bind ํ๊ธฐ #
Suppose you already have a function that you want to be called back when a signal is emitted, but it takes the wrong argument types. For example, lets try to attach the warn_people(std::string) function to the detected signal from the first example, which didn't supply a location string.
Just trying to connect it with:
myaliendetector.detected.connect(SigC::slot(warn_people));
results in a compile-time error, because the types don't match. This is good! This is typesafety at work. In the C way of doing things, this would have just died at runtime after trying to print a random bit of memory as the location - ick!
We have to make up a location string, and bind it to the function, so that when detected is emitted with no arguments, something adds it in before warn_people is actually called.
We could write it ourselves - it's not hard:
void warn_people_wrapper() // note this is the signature that 'detected' expects
{
warn_people("the carpark");
}
but after our first million or so we might start looking for a better way. As it happens, LibSigC++ has one.
SigC::bind(slot, arg);
binds arg as the argument to slot, and returns a new slot of the same return type, but with one fewer arguments.
Now we can write:
myaliendetector.detected.connect(SigC::bind( SigC::slot(warn_people), "the carpark" ) );
If the input slot has multiple args, the rightmost one is bound.
The return type can also be bound with bind_return(slot, returnvalue); though this is not so commonly useful.
So if we can attach the new warn_people() to the old detector, can we attach the old warn_people (the one that didn't take an argument) to the new detector?
Of course, we just need to hide the extra argument. This can be done with SigC::hide, eg.
myaliendetector.detected.connect( SigC::hide<std::string>( SigC::slot(warn_people) ) );
The template arguments are the types to hide (from the right only - you can't hide the first argument of 3, for example, only the last).
hide_return effectively makes the return type void.
4.2 ํ์ ์ฌ์ง์ ํ๊ธฐ #
A similar topic is retyping. Perhaps you have a signal that takes an int, but you want to connect a function that takes a double.
This can be achieved with the retype template. retype has template arguments just like Signal - return value, signal types.
It's a function template that takes a slot, and returns a slot. eg.
void dostuff(double foo)
{
}
SigC::Signal1<void,int> asignal;
asignal.connect( retype<void,int>( slot(&dostuff) ) );
If you only want to change the return type, you can use retype_return. retype_return needs only one template argument.
4.3 ๋ง์ฌ๋ฌ(marshaller) #
When I first mentioned return values, I said that more advanced handling of multiple return values was possible with Marshallers.
A Marshaller is a class that gets fed all the return values as they're returned. It can do a couple of things:
- It can stop the emit process at any point, causing no further slots to be called
- It can return a value, of any type
As an example, here's the averaging marshaller:
class Averager
{
public:
// we must typedef InType and OutType for the SigC library
typedef double OutType;
typedef int InType;
Averager()
: total_(0), number_(0)
{}
OutType value() { return (double)total_/(double)number_; } // avoid integer division
static OutType default_value() { return 0; }
// This is the function called for each return value.
// If it returns 'true' it stops here.
bool marshal(InType newval)
{
total_ += newval; // total of values
++number_; // count of values
return false; // continue emittion process
};
private:
int total_;
int number_;
};
To use this, we pass the type as an extra template argument when defining the Signal, eg.
SigC::Signal0<int,Averager> mysignal;
Now we can do:
double average_of_all_connected_slots = mysignal();
Each connected slot will be called, its value passed to an instance of Averager and that Averager's value() will be returned.
In the downloadable examples, this is example6.cc.








