Re: [sigc] Functor from variable



Am 05.08.2004 20:37:45 schrieb(en) Roger Ferrer Ibáñez:
Hello,

I was just wondering if it is possible in libsigc++ to create a functor
from a variable. So you get a functor that returns the value of this
variable and receives no parameter.

I've seen there's a way to get a similar effect by binding the return of
a function of the same type to a sigc::ref of the variable. Something
like this:

I'm not sure without trying myself, but please let me know whether the following code works:

 #include <sigc++/adaptors/lambda/lambda.h>
 [...]
 readValue.connect(sigc::var(data));
 [...]

Regards,

 Martin


-- Example --
int dummyOne()
{
~  cout << "Calling " << __FUNCTION__ << endl;
~  return 0;
}

int main(int argc, char* argv[])
{
~  int data;
~  sigc::signal<int> readValue;

~  readValue.connect(sigc::bind_return(&dummyOne, sigc::ref(data)));

~  data = 3;
~  cout << readValue() << endl;

~  data = 5;
~  cout << readValue() << endl;
}
-- End of Example --

-- Output --
Calling dummyOne
3
Calling dummyOne
5
-- End of Output --

Is there a way to avoid the unnecessary call to dummyOne? Maybe making
inline the dummy function would reduce it (already small) impact?

Something like

~  readValue.connect(sigc::var_fun(sigc::ref(data)));

sigc::var_fun would create a functor that returns the type of data (int)
and receives no parameter (a 0-ary function).

Thank you for your attention!

--
Roger Ferrer Ibáñez
Correu: rofi ya com
Correu (FIB): b4270692 est fib upc es
Correu (Casa): roger pinchito com
Correu (JEDI): rogerf jedi upc es



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]