libsaria: Remove ReadTask

I replaced it with the generic idle task I just created.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-11-23 10:38:49 -05:00 committed by Anna Schumaker
parent 5244fe9605
commit 1ac51775c3
5 changed files with 42 additions and 33 deletions

View File

@ -12,6 +12,7 @@ namespace libsaria
int size();
int run_task();
void schedule(void (*)());
void schedule(void (*)(void *), void *);
void queue_task(IdleTask *, bool);
void cancel_all(void *);
float progress();

View File

@ -29,15 +29,4 @@ class WriteTask : public IdleTask
void run_task();
};
class ReadTask : public IdleTask
{
private:
string filepath;
void (*func)(ifstream &);
public:
ReadTask(string, void (*)(ifstream &));
~ReadTask();
void run_task();
};
#endif /* LIBSARIA_IDLE_TASK_H */

View File

@ -1,5 +1,6 @@
// Copyright (c) 2012 Bryan Schumaker.
#include <fs.h>
#include <idle.h>
#include <task.h>
#include <print.h>
@ -43,6 +44,23 @@ static bool compare_files(string &one, string &two)
return a < b;
}
struct ReadData
{
string filepath;
void (*func)(ifstream &);
};
static void read_cb(void *d)
{
ifstream stream;
struct ReadData *data = (struct ReadData *)d;
stream.open(data->filepath.c_str());
data->func(stream);
stream.close();
delete data;
}
namespace libsaria
{
@ -129,7 +147,10 @@ namespace libsaria
void app::read(string file, void (*func)(ifstream &))
{
new ReadTask(appdir + "/" + file, func);
struct ReadData *data = new struct ReadData;
data->filepath = appdir + "/" + file;
data->func = func;
idle::schedule(read_cb, data);
}
void app::read_now(string file, void (*func)(ifstream &))

View File

@ -14,9 +14,12 @@ class GIdleTask : public IdleTask
{
private:
void (*func)();
void (*func2)(void *);
void *data;
public:
GIdleTask(void (*)());
GIdleTask(void (*)(void *), void *);
~GIdleTask();
void run_task();
};
@ -26,13 +29,22 @@ GIdleTask::GIdleTask(void (*f)())
func = f;
}
GIdleTask::GIdleTask(void (*f)(void *), void *d)
{
func2 = f;
data = d;
}
GIdleTask::~GIdleTask()
{
}
void GIdleTask::run_task()
{
func();
if (func)
func();
else
func2(data);
}
static void do_task(IdleTask *task)
@ -62,6 +74,12 @@ namespace libsaria
queue_task(task, false);
}
void idle::schedule(void (*func)(void *), void *data)
{
GIdleTask *task = new GIdleTask(func, data);
queue_task(task, false);
}
int idle::run_task()
{
IdleTask *task;

View File

@ -29,23 +29,3 @@ bool WriteTask::should_cancel(void *d)
{
return d == data;
}
ReadTask::ReadTask(string file, void (*fn)(ifstream &))
{
filepath = file;
func = fn;
queue();
}
ReadTask::~ReadTask()
{
}
void ReadTask::run_task()
{
ifstream stream;
stream.open(filepath.c_str());
func(stream);
stream.close();
}