Re: Type safe signal implementation
That all is good, but...
Stefan Chrobot wrote:
template<typename ClassT, typename Arg1T>
void connect(ClassT& object,
void (ClassT::*method)(Arg1T))
{
connect_do<ClassT, Arg1T>(&object, method);
}
template<typename ClassT, typename Arg1T>
void connect_do(ClassT* object = 0,
void (ClassT::*method)(Arg1T) = 0)
Just a nit: any point in not overloading connect()?
[skipped]
struct Test
{
void print(int x)
{
cout << "Test::set " << x << endl;
}
void printMore(int x)
{
cout << "class Test, method printMore with argument x = "
<< x << endl;
}
};
struct OtherTest
{
void inc(int x)
{
cout << "OtherTest::inc " << x + 1 << endl;
}
};
int main()
{
Test t1;
Test t2;
OtherTest t3;
signal sig1;
sig1.connect(t1, &Test::print);
sig1.connect(t2, &Test::printMore);
sig1.connect(t3, &OtherTest::inc);
sig1(4);
signal sig2;
sig2.connect(t1, &Test::print);
sig2.connect(t2, &Test::print);
sig2(5); // signal with int
sig2('5'); // signal with char, nothing happens
}
Right, when signalling with char nothing happens. At all. But not
only desired functions could be not called this way-- the undesired
ones could get called. Try the following:
struct YetAnotherTest
{
void dec(double x)
{
cout << "YetAnotherTest::dec " << x - 1 << endl;
// do some harm: delete user data, etc.
}
};
int main()
{
Test t1;
Test t2;
OtherTest t3;
YetAnotherTest t4;
signal sig1;
sig1.connect(t1, &Test::print);
sig1.connect(t2, &Test::printMore);
sig1.connect(t3, &OtherTest::inc);
sig1(4);
signal sig2;
sig2.connect(t1, &Test::print);
sig2.connect(t2, &Test::print);
sig2.connect(t4, &YetAnotherTest::dec);
sig2(5); // signal with int
sig2('5'); // signal with char, nothing happens
sig2(1.0); // YetAnotherTest::dec breaks in
return 0;
}
Such a type "safety" might become your foe.
I'd rather curious of application where this feature can be useful. As
for me, this potentially introduces problems which are hard to debug.
It would be better to have at least run-time check for a type mismatch,
but I have no idea how one could acheive such a check.
Alex
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]