From 707e7b3427bdc81dabc9c63b9aad75bb4ccc12e1 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Thu, 27 Oct 2011 15:07:16 -0400 Subject: [PATCH] libsaria: Create an idle namespace This cleans up the idle task code a lot, and reduces the size of ocarina.bin to less than 1MB (with full debugging). It probably won't stay there long... --- include/libsaria/idle.h | 13 +++++----- libsaria/idle.cpp | 37 --------------------------- libsaria/idle/idle.cpp | 56 ++++++++++++++++++++++++----------------- libsaria/idle/idle.h | 16 +++--------- libsaria/idle/task.cpp | 25 +++++++++--------- 5 files changed, 57 insertions(+), 90 deletions(-) delete mode 100644 libsaria/idle.cpp diff --git a/include/libsaria/idle.h b/include/libsaria/idle.h index da89a8b3..5a8058bb 100644 --- a/include/libsaria/idle.h +++ b/include/libsaria/idle.h @@ -8,15 +8,16 @@ class IdleTask virtual ~IdleTask() = 0; virtual void run_task() = 0; - int queue(); - int queue_front(); + void queue(); + void queue_front(); }; namespace libsaria { - int idle_task(); - int queue_task(IdleTask *); - int queue_task_front(IdleTask *); -} + namespace idle + { + int run_task(); + }; +}; #endif /* LIBSARIA_IDLE_H */ diff --git a/libsaria/idle.cpp b/libsaria/idle.cpp deleted file mode 100644 index d30d7e56..00000000 --- a/libsaria/idle.cpp +++ /dev/null @@ -1,37 +0,0 @@ - -#include -#include "idle/idle.h" - -static Idle idle; - -int libsaria::idle_task() -{ - return idle.run_task(); -} - -/* - * This will queue a pre-constructed idle task - * It should be constructed through `new` before calling - * and will be deleted by the idle queue once run. - * - * IDLE_TASK_QUEUED will only be triggered when the queue - * goes from size is increased from 0 to 1. This will tell - * the UI that idle tasks have been queued. - */ -static int queue_task_generic(IdleTask *task, bool front) -{ - int queue_size = idle.queue_task(task, front); - if (queue_size == 1) - trigger_callback(IDLE_TASK_QUEUED); - return queue_size; -} - -int libsaria::queue_task(IdleTask *task) -{ - return queue_task_generic(task, false); -} - -int libsaria::queue_task_front(IdleTask *task) -{ - return queue_task_generic(task, true); -} diff --git a/libsaria/idle/idle.cpp b/libsaria/idle/idle.cpp index 79dec283..6c9d80a6 100644 --- a/libsaria/idle/idle.cpp +++ b/libsaria/idle/idle.cpp @@ -1,31 +1,41 @@ +#include +using namespace std; + +#include #include "idle.h" -Idle::Idle() -{ -} +static deque idle_queue; -Idle::~Idle() +namespace libsaria { -} -int Idle::run_task() -{ - IdleTask *task; - if (idle_queue.size() == 0) + int idle::run_task() + { + IdleTask *task; + if (idle_queue.size() == 0) + return idle_queue.size(); + task = idle_queue.front(); + idle_queue.pop_front(); + task->run_task(); + delete task; return idle_queue.size(); - task = idle_queue.front(); - idle_queue.pop_front(); - task->run_task(); - delete task; - return idle_queue.size(); -} + } -int Idle::queue_task(IdleTask *task, bool front) -{ - if (front == true) - idle_queue.push_front(task); - else - idle_queue.push_back(task); - return idle_queue.size(); -} + /* + * IDLE_TASK_QUEUED will only be triggered when the queue + * size is increased from 0 to 1. This will tell the UI + * that idle tasks have been queued. + */ + int idle::queue_task(IdleTask *task, bool front) + { + if (front == true) + idle_queue.push_front(task); + else + idle_queue.push_back(task); + if (idle_queue.size() == 1) + trigger_callback(IDLE_TASK_QUEUED); + return idle_queue.size(); + } + +}; diff --git a/libsaria/idle/idle.h b/libsaria/idle/idle.h index 62f183f8..e5bb94a3 100644 --- a/libsaria/idle/idle.h +++ b/libsaria/idle/idle.h @@ -1,22 +1,14 @@ #ifndef LIBSARIA_IDLE_COMPONENT_H #define LIBSARIA_IDLE_COMPONENT_H -#include -using namespace std; - #include -class Idle +namespace libsaria { - private: - deque idle_queue; - - public: - Idle(); - ~Idle(); - - int run_task(); + namespace idle + { int queue_task(IdleTask *, bool); + }; }; #endif /* LIBSARIA_IDLE_COMPONENT_H */ diff --git a/libsaria/idle/task.cpp b/libsaria/idle/task.cpp index 854ccfab..7d20ee03 100644 --- a/libsaria/idle/task.cpp +++ b/libsaria/idle/task.cpp @@ -1,20 +1,21 @@ #include +#include "idle.h" -IdleTask::IdleTask() +/* + * The IdleTask should be constructed through `new` and not + * allocated on the stack since it will be deleted by the + * idle queue once it has run. + */ +IdleTask::IdleTask() {} +IdleTask::~IdleTask() {} + +void IdleTask::queue() { + libsaria::idle::queue_task(this, false); } -IdleTask::~IdleTask() +void IdleTask::queue_front() { -} - -int IdleTask::queue() -{ - return libsaria::queue_task(this); -} - -int IdleTask::queue_front() -{ - return libsaria::queue_task_front(this); + libsaria::idle::queue_task(this, true); }