|
Boost : |
From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2004-11-21 13:05:38
Sorry if my question seems somehow weird. I already posted a similar one
to the users list but with no result. So I try again but also post a small
example to make my question clear.
The purpose of the example is to autoconnect a couple of member classes
to a containing class without the need to explicitely call the constructors
of the member classes:
#include <boost/signal.hpp>
#include <boost/any.hpp>
#include <boost/bind.hpp>
boost::any pinit; // todo: should be thread local in MT version
template<typename Function>
struct ctor_signal_push : public boost::signal<Function>{
ctor_signal_push() { pinit = this; } // todo: make it a stack
};
template<typename Signature, typename Function>
void ctor_connect(Function fn)
{
boost::any_cast<ctor_signal_push<Signature>* >(pinit)->connect(fn);
// can this be done without the Signature parameter?
// i.e. can the Signature be deduced from Function? (which might be
a functor)
}
class A;
class B {
public:
B() { ctor_connect<void (A*)>(boost::bind(init, this, _1)); }
void init(A* p) { pOuter = p; };
A* pOuter;
};
class C {
public:
C() { ctor_connect<void (A*)>(boost::bind(init, this, _1)); }
void init(A* p) { pOuter = p; };
A* pOuter;
};
class A {
public:
A();
ctor_signal_push<void (A*)> init;
B aB;
C aC;
B aB1;
B aB2;
// ctor_signal_pop;
};
A::A()
{
init(this); // connect all member classes
}
int main(int argc, char* argv[])
{
A aA;
return 0;
}
This does work well so far, but I would like to get rid of the explicit
"void (A*)"
parameter in
ctor_connect<void (A*)>(boost::bind(init, this, _1));
Is this possible?
Perhaps I am even trying to do something that I should better avoid
because there is
a better solution available. Does anyone know?
Thank you for any hints,
Roland
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk