How do I store a type as a member?
I am creating a simple scheduler that takes functions as a parameter, enqueues them, and executes them later. The class is designed to be inherited and then enqueue(function_ptr)
called from the methods of the child class.
My problem is that scheduling will happen based on time and this is measured by a 2ms interrupt. You can subscribe to callbacks from the interrupt handler, but they only return the object void*
to the object that subscribed.
So how can I find out what the return type is so that I can throw it and call the appropriate method?
I thought about the template parameter when creating class Scheduler
, but then the callback function doesn't know what the template parameter is. Maybe some way to store a child class Scheduler
inside a member variable is to cast object
before Scheduler
and look into that field, cast it to the final type? I think it can be solved by globally enum
scheduling classes, but that seems like a bad solution.
Or maybe my approach to this is wrong?
Here's the code for the Scheduler. I refrained from posting because perhaps 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 to call the slow function at a later point, and does so with a parameter true
so that the actual computation is done.
What you are looking for are delegates. Since you don't want to use boost or the other delegate implementations mentioned, you can try this.
SomeObject obj;
delegate d = delegate::from_member<SomeObject,
&SomeObject::someMethod>(&obj);
The planner should use delegate objects in your vector. Take a look here: http://www.codeproject.com/Articles/11015/The-Impossibly-Fast-C-Delegates