libsaria: Queue an idle task to update the library

I don't actually perform a library update at this point, but I do create
a new task and add it to the update queue.  When the run_task() function
is called, the task is removed from the queue and deleted.
This commit is contained in:
Bryan Schumaker 2011-09-12 14:17:22 -04:00
parent c0a80623f5
commit b43fcc4b05
15 changed files with 171 additions and 14 deletions

View File

@ -1,6 +1,15 @@
#ifndef LIBSARIA_IDLE_H
#define LIBSARIA_IDLE_H
class IdleTask
{
public:
IdleTask();
virtual ~IdleTask() = 0;
virtual void run_task() = 0;
};
void libsaria_idle_task();
void libsaria_queue_task(IdleTask *);
#endif /* LIBSARIA_IDLE_H */

View File

@ -0,0 +1,9 @@
#ifndef LIBSARIA_LIBRARY_H
#define LIBSARIA_LIBRARY_H
#include <string>
using namespace std;
void libsaria_add_library(string);
#endif /* LIBSARIA_LIBRARY_H */

View File

@ -7,3 +7,13 @@ void libsaria_idle_task()
{
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.
*/
void libsaria_queue_task(IdleTask *task)
{
idle.queue_task(task);
}

View File

@ -1,5 +1,6 @@
#include "idle.h"
#include <libsaria/print.h>
Idle::Idle()
{
@ -11,5 +12,16 @@ Idle::~Idle()
void Idle::run_task()
{
IdleTask *task;
if (idle_queue.size() == 0)
return;
task = idle_queue.front();
idle_queue.pop();
task->run_task();
delete task;
}
void Idle::queue_task(IdleTask *task)
{
idle_queue.push(task);
}

View File

@ -4,18 +4,19 @@
#include <queue>
using namespace std;
#include "task.h"
#include <libsaria/idle.h>
class Idle
{
private:
queue<IdleTask> idle_queue;
queue<IdleTask*> idle_queue;
public:
Idle();
~Idle();
void run_task();
void queue_task(IdleTask *);
};
#endif /* LIBSARIA_IDLE_COMPONENT_H */

View File

@ -1,5 +1,5 @@
#include "task.h"
#include <libsaria/idle.h>
IdleTask::IdleTask()
{

View File

@ -1,11 +0,0 @@
#ifndef LIBSARIA_IDLE_TASK_H
#define LIBSARIA_IDLE_TASK_H
class IdleTask
{
public:
IdleTask();
~IdleTask();
};
#endif /* LIBSARIA_IDLE_TASK_H */

10
libsaria/library.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "library/library.h"
static Library library;
void libsaria_add_library(string dir)
{
library.add_path(dir);
library.update_path(dir);
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,10 @@
#include "library.h"
Library::Library()
{
}
Library::~Library()
{
}

View File

@ -0,0 +1,23 @@
#ifndef LIBSARIA_LIBRARY_SOURCE_H
#define LIBSARIA_LIBRARY_SOURCE_H
#include <map>
#include <string>
using namespace std;
#include "path.h"
class Library
{
private:
map<string, LibraryPath> path_map;
public:
Library();
~Library();
void add_path(string);
void update_path(string);
};
#endif /* LIBSARIA_LIBRARY_SOURCE_H */

11
libsaria/library/path.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "path.h"
LibraryPath::LibraryPath(string dir)
{
path = dir;
}
LibraryPath::~LibraryPath()
{
}

34
libsaria/library/path.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef LIBSARIA_LIBRARY_PATH_H
#define LIBSARIA_LIBRARY_PATH_H
#include <string>
using namespace std;
#include <libsaria/idle.h>
class LibraryPath
{
private:
string path;
public:
LibraryPath(string);
~LibraryPath();
void set_dir(string);
void update();
};
class UpdateTask : public IdleTask
{
private:
LibraryPath *path;
string dir;
public:
UpdateTask(LibraryPath *, string);
~UpdateTask();
void run_task();
};
#endif /* LIBSARIA_LIBRARY_PATH_H */

View File

@ -0,0 +1,39 @@
#include <libsaria/print.h>
#include <libsaria/idle.h>
#include "library.h"
#include "path.h"
void Library::add_path(string dir)
{
path_map.insert( pair<string, LibraryPath>(dir, LibraryPath(dir)) );
}
void Library::update_path(string dir)
{
map<string, LibraryPath>::iterator it;
it = path_map.find(dir);
if (it != path_map.end())
it->second.update();
}
void LibraryPath::update()
{
UpdateTask *task = new UpdateTask(this, path);
libsaria_queue_task(task);
}
UpdateTask::UpdateTask(LibraryPath *lib_path, string scan_dir)
{
path = lib_path;
dir = scan_dir;
}
UpdateTask::~UpdateTask()
{
}
void UpdateTask::run_task()
{
}