
Peter Dimov wrote:
Something along the lines of:
template<class Sig> class slot { private:
shared_ptr< slot_impl<Sig> > pi_;
public:
template<class F> slot( F f ): pi_( new slot_impl<Sig>( f ) ) {} result_type operator()( ... ) { return (*pi_)( ... ) } template<class T> void track( shared_ptr<T> pt ) { pi_->track( pt ); } };
template<class Sig> class signal { private:
std::vector< slot<Sig> > slots_; mutable mutex mx_;
public:
// illustration only
void operator()( ... ) const { scoped_lock lock( mx_ ); std::vector< slot<Sig> > tmp( slots_ ); lock.unlock();
for_each( tmp.begin(), tmp.end(), bind( &slot<Sig>::operator(), _1, .... ) ); } };
allows your example to be restated as:
slot
make_observer_slot( ... ) { return slot ( some_observer( ... ) ).track( whatever ); } // ...
signal
sig; sig.connect( make_observer_slot() ); with some_observer no longer needing to have a dependency on signals.
Hm, I think that still bears a higher level of dependency between connection site and observer - this time the observer needs to know the signal's signature. While you can use bind(...) on an ordinary function class, it would be difficult to forward the tracking to the true slot (I'd call the independently created one a slot model) after binding. Regards Timmo Stange