using boost::shared_ptr with boost::signal
Hi, I want to use boost::shared_ptr with boost::signal. class A { private: int _i; public: A() : _i( 0) {} void operator()( int, std::string & s) { i+= 1; } void operator()( std::string & s, int) { i+= 2; } }; boost::shared_ptr< A > a( new A() ); boost::signal< void( int, std::string &) > rsig; boost::singal< void( std::string &, int) > lsig; rsig.connect( a); lsig.connect( a); rsig(0,"called from right side"); lsig("called from left side",0); Because a is a smart_ptr this code would not compile because operator() is not a member function. If I dereference the smart pointer when I connect to signal, changes of a are never know by the other signal instance. Maybe I can use a proxy class which aggregates the smart pointer but maybe there is a better solution?! with best regards, Oliver -- "Der Spezialist ist ein Barbar, dessen Unwissenheit nicht allumfassend ist." Master's Voice, S.LEM
Oliver Kowalke wrote:
Hi,
I want to use boost::shared_ptr with boost::signal.
class A { private: int _i; public: A() : _i( 0) {}
void operator()( int, std::string & s) { i+= 1; }
void operator()( std::string & s, int) { i+= 2; } };
boost::shared_ptr< A > a( new A() ); boost::signal< void( int, std::string &) > rsig; boost::singal< void( std::string &, int) > lsig;
rsig.connect( a); lsig.connect( a);
rsig(0,"called from right side"); lsig("called from left side",0);
Because a is a smart_ptr this code would not compile because operator() is not a member function. If I dereference the smart pointer when I connect to signal, changes of a are never know by the other signal instance.
What's wrong with: rsig.connect(*a); lsig.connect(*a); When you now trigger rsig, a.operator()(int, std::string &s) is called. When you now trigger lsig, a.operator()(std::string &s,0) is called. What do you mean that "changes of 'a' are never know by the other signal instance" ?
Am Samstag, 24. Juli 2004 22:22 schrieb Edward Diener:
What's wrong with:
rsig.connect(*a); lsig.connect(*a);
When you now trigger rsig, a.operator()(int, std::string &s) is called. When you now trigger lsig, a.operator()(std::string &s,0) is called.
What do you mean that "changes of 'a' are never know by the other signal instance" ?
In your suggestion the instance of A where the smart pointer points to has the value _i == 0 (copies were created)! I want a value of 3! boost::signal< void( int, std::string) > rsig; boost::signal< void( std::string, int) > lsig; boost::shared_ptr< A > aptr( new A() ); rsig.connect( *aptr); lsig.connect( *aptr); rsig(0,"called from right side"); lsig("called from left side",0); std::cout << aptr->get() << std::endl; // should print 3, but aptr has value 0 A proxy (with operator()(int, string) and operator(string,0)) which contains the smart pointer aptr could be connected to signal but maybe there is a better solution?! thx, Oliver -- "Der Spezialist ist ein Barbar, dessen Unwissenheit nicht allumfassend ist." Master's Voice, S.LEM
On Jul 25, 2004, at 11:32 AM, Oliver Kowalke wrote:
Am Samstag, 24. Juli 2004 22:22 schrieb Edward Diener: A proxy (with operator()(int, string) and operator(string,0)) which contains the smart pointer aptr could be connected to signal but maybe there is a better solution?!
It does seem like there should be a better solution, but I don't have one at the moment. Typically, one is binding to some other member function and therefore can use: boost::bind(&X::foo, shared_ptr_to_x, _1, _2) The closest you can get without writing a proxy is to use boost::ref(*shared_ptr_x), but _only_ if you know that there will be another shared_ptr to that x that lives longer than the signal. It's not a very good solution, I know :( Doug
Hi, I'd like to know the performance of the signal/slot mechanism if I connect n objects to a signal instance and disconnecting one slot. Is the signals package designed for speed? with best regards, Oliver -- "Der Spezialist ist ein Barbar, dessen Unwissenheit nicht allumfassend ist." Master's Voice, S.LEM
On Aug 1, 2004, at 12:00 PM, Oliver Kowalke wrote:
I'd like to know the performance of the signal/slot mechanism if I connect n objects to a signal instance and disconnecting one slot.
According to the docs, it's O(lg n) for each connect and disconnect.
Is the signals package designed for speed?
It's designed to be efficient, but has not been "tweaked" for optimal performance. Doug
participants (3)
-
Doug Gregor
-
Edward Diener
-
oliver.kowalke@t-online.de