Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Scheduling system not accepting function pointer

Started by Eldritch May 30, 2006 at 10:39 AM 1 replies 650+ views
Original Post
Eldritch
Eldritch
I have implemented a scheduling system into my appication so I can register functions that are to be run at a specific time interval. The scheduler consists of "tasks" that store the time interval (double) and a function pointer (typedef void(f*)(void)) to the function I want to run within the interval.

class CScheduler
{
public:

	// Constructor.
	CScheduler();

	// Destructor.
	~CScheduler();

	// Adding a task.
	void AddTask(double aTimeIntervalInMicro, f* aTaskFunction);

	// Removing a task.
	void RemoveTask(int aIndex);

	// Getting system time.
	DWORD GetTime(); 

	// Updating scheduler.
	void Update();

	// Getting singleton instance.
	static CScheduler* Instance();

private:

	// List of registered tasks.
	std::vector<CTask*> iTasks;

	// Singleton instance.
	static CScheduler* iInstance;
};


class CTask
{
public:

	// Constructor.
	CTask(double aTimeIntervalInMicro, f* aTaskFunction)
	{
		iTimeInterval = aTimeIntervalInMicro;
		iTaskFunction = aTaskFunction;
	}

	// Time interval for task.
	double iTimeInterval;

	// Task to trigger.
	f* iTaskFunction;

	// Last update for taks.
	double iLastUpdate;
};

When I want to register a task in the scheduler, I cannot really figure out how to type in the function. I want to register functions from my system classes, which are all singleton, and the functions I want to call are non-static members. Here is what I attempted, but it did not quite work:

iScheduler->AddTask(1.0, CGui::Instance())->Update());

The compiler (Visual Studio 2005) tells me that I should type &CGui::Update as the function pointer, but that will not work as the update function is a non-static member. Without having to make every single member function of every class I have static, since I want to be able to register any function, what could I do? Has anybody imlemented a scheduling system and managed to make a function pointer or something similar? Then I am hoping you can share some tips.
omgomghilol
omgomghilol
Boost's function and bind libraries will make your life infinitely easier here; use them. However, if you insist on doing it the hard way, take a look at this. It will help you through everything you need for function pointers and method pointers.

Note that if you don't use boost, you'll have to store a point to the object as well as a pointer to the method in order to use a method pointer.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.