/** * Copyright 2013 (c) Anna Schumaker. */ #ifndef OCARINA_CORE_IDLE_H #define OCARINA_CORE_IDLE_H /** * The idle queue is used to schedule function calls to run at a * later time. It is expected that a higher layer can determine * when the application is idle and call idle::run_task() accordingly. * * The idle layer keeps a count of the number of tasks added to the queue * since the last time the queue was empty. Whenever a task is scheduled, * the "queued" count is incremented by 1. When tasks are run, the "serviced" * count is incremented by 1. These counters are reset to 0 once the queue * is empty. */ namespace idle { /** * Base class used by the templated IdleTask class. This lets * us create an idle queue storing IdleBase pointers to get * around potential templating issues. */ class IdleBase { protected: /** * Adds the IdleBase to the idle queue and increments * the "queued" counter by 1. */ void schedule(); public: IdleBase(); /**< IdleBase constructor. */ virtual ~IdleBase(); /**< IdleBase destructor. */ virtual void run() = 0; /**< Run the idle task. */ }; /** * Generic IdleTask class. This class is templated to allow * for scheduling functions that take different types of data * parameters. */ template class IdleTask : public IdleBase { private: void (*_func)(T &); /**< Function to call when run. */ T _data; /**< Data to pass to the function. */ public: /** * IdleTask constructor. * * @param func Function to call when running this task. * @param data Data to pass to the function. */ IdleTask(void (*)(T &), T); ~IdleTask(); /**< IdleTask destructor. */ void run(); /**< Call func(), passing data as the argument. */ }; /** * Creates a new IdleTask and adds it to the queue. * * @param func Function to call when running the new task. * @param data Data to pass to the function. */ template static inline void schedule(void (*func)(T &), T data) { new IdleTask(func, data); } /** * Run the next task on the idle queue and increment the "serviced" * counter. If this was the last task then the "queued" and * "serviced" values are reset to 0. * * @return True if there are still tasks on the queue, false otherwise. */ bool run_task(); /** * Use the values of the queued and serviced counters to find the * overall process of the idle queue (serviced / queued). * * @return The percentage of IdleTasks that have been run. */ float get_progress(); }; #include "idle.hpp" #endif /* OCARINA_CORE_IDLE_H */