I am building a simple scheduler, which takes functions as parameter, puts them in a queue and executes them at a later time. The class is intended to be inherited, and later enqueue(function_ptr)
to be called from the methods of the child class.
My problem is that the scheduling will be happening based on time, and that is measured with a 2ms interrupt. One can subscribe for callbacks from the interrupt handler, but those return only a void*
to the object that got subscribed.
So how can I know what is the type of the object being passed back, so that I can cast it and call the appropriate method?
I was thinking about template parameter at class Scheduler
creation, but then the callback function doesn't know what the template parameter was. So maybe some way of storing Scheduler
's child class inside a member variable - cast object
to Scheduler
and looking at that field, cast it to the final type? I guess this can be solved by a global enum
of schedulable classes, but that seems like a bad solution.
Or maybe my whole approach of this is wrong?
Here is some code for the Scheduler. I was refraining from posting, because possibly my whole approach is wrong.
class Scheduler {
private: std::vector<QueueItem> m_queue;
protected: void schedule(void *foo_ptr);
public: void callback_2ms_passed(); // Note that we are getting only 'this' as a parameter.
}
class Child1 : public Scheduler, public OtherClasses {
void slow_foo(bool execute_immediately = false) {
if(!execute_immediately)
schedule(slow_foo);
else
// Do slow stuff.
}
}
The idea is that the scheduler decides when at a later moment to call the slow function, and does it with parameter true
so that the actual calculations are made.