Sign in to follow this  

Callback scheduler, pointers to member-functions

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
I'm sorry that this won't be much help as I've not done much in C++ for some time, and used function pointers in much more, but:

Generally in modern C++ the common method is to simply provide a common function pointer (or better yet, a boost::function object), and then use a binder like boost::bind/boost::mem_fn or std::ptr_fun to adapt the member function for common use.

Share this post


Link to post
Share on other sites

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this