Original Post
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. 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: 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.
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;
};
iScheduler->AddTask(1.0, CGui::Instance())->Update());