Hi,
I wanted to implempent a C++ analogue to Javas java.util.Timer.schedule(), but somehow more flexible, that is, not only providing a virtual "run()" from a base class, but being able to add any function.
I´m having some problems with pointers to member-functions:
My classes look like this:
template <class T> struct CallData
{
void (T::*mCall)(T *pObject);
unsigned int mInterval;
unsigned int mDuration;
unsigned int mElapsedTime;
};
template<class T> class Timer
{
public:
void registerCall(std::string pName, void (T::*pCall)(T *pObject),unsigned int pInterval, unsigned int pDuration = 0);
void deleteCall(std::string pName);
void runTimer(unsigned int pElapsedTime, T *pObject);
private:
std::map<std::string, CallData<T>> mCalls;
};
And implementation:
void Timer<class T>::registerCall(std::string pName, void (T::*pCall)(T *pObject),unsigned int pInterval, unsigned int pDuration)
{
CallData<T> lData = { pCall, pInterval, pDuration, 0};
mCalls[pName] = lData;
}
void Timer<class T>::deleteCall(std::string pName)
{
mCalls.erase(pName);
}
void Timer<class T>::runTimer(unsigned int pElapsedTime, T *pObject)
{
for(std::map<std::string, CallData<T>>::iterator i = mCalls.begin(); i != mCalls.end(); i++)
{
i->second.mElapsedTime += pElapsedTime;
if((i->second.mElapsedTime > i->second.mInterval) &&
(i->second.mElapsedTime < i->second.mInterval + i->second.mDuration))
{
i->second... // how to access the function-poiner?
}
}
}
Callbacks should look like this:
static void SomeClass::SomeFunctionCallbackWrapper(SomeClass *pObject)
{
SomeClass* lSelf = pObject;
lSelf->SomeFunction();
}
1st problem is that I couldn´t find out how to call the function via the function pointer (the one stored in the std::map)
Second problem is that the compiler complains about the second parameter in "registerCall()", when I´m trying to add a callback-function, that it can´t convert from '(__cdecl *)(void *)' to 'void (__thiscall SomeClass::* )(T *).
I´m really at a loss here.
If anyone has a suggestion how to solve those problems, or can just tell me that this whole thing is just not possible, that would be great.
Mike