Hello,
I am currently implementing a timer/callback system using Don Clugston's fastdelegates. (see http://www.codeproject.com/KB/cpp/FastDelegate.aspx)
Here is the starting code:
struct TimerContext
{
};
void free_func( TimerContext* )
{
}
struct Foo
{
void member_func( TimerContext* )
{
}
};
Foo f;
MulticastDelegate< void (TimerContext*) > delegate;
delegate += free_func;
delegate += bind( &Foo::member_func, &f );
Okay, but now, i wish the user to be able to subclass TimerContext
to store and send his own structures to the callbacks.
The purpose here is to prevent the user from having to downcast the TimerContext
himself
struct TimerContext
{
};
struct MyTimerContext : TimerContext
{
int user_value;
};
void free_func( TimerContext* )
{
}
void free_func2( MyTimerContext* )
{
}
struct Foo
{
void member_func( TimerContext* )
{
}
void member_func2( MyTimerContext* )
{
}
};
Foo f;
MulticastDelegate< void (TimerContext*) > delegate;
delegate += free_func;
delegate += free_func2;
delegate += bind( &Foo::member_func, &f );
delegate += bind( &Foo::member_func2, &f );
As you guessed, GCC won't let me do that :)
error: invalid conversion from `void (*)(MyTimerContext*)' to `void (*)(TimerContext*)'
error: initializing argument 1 of `delegate::Delegate<R ()(Param1)>::Delegate(R (*)(Param1)) [with R = void, Param1 = TimerContext*]'
So now my question is:
If I force the cast using reinterpret_cast
, it'll work, but will it be safe ?
PS: These are time-critical callbacks, heavy virtual-oriented solutions are considered impracticable :/