-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new calaos-os API with calaos-ct backend
- Loading branch information
Showing
12 changed files
with
891 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "AsyncJobs.h" | ||
#include <QVariant> | ||
|
||
void AsyncJob::start(const QVariant &previous_data) | ||
{ | ||
jobFunc(this, previous_data); | ||
} | ||
|
||
AsyncJobs::AsyncJobs(QObject *parent): | ||
QObject(parent) | ||
{ | ||
} | ||
|
||
AsyncJobs::~AsyncJobs() | ||
{ | ||
} | ||
|
||
void AsyncJobs::append(AsyncJob *j) | ||
{ | ||
if (!j) return; | ||
|
||
//reparent object to handle jobs memory from AsyncJobs | ||
j->setParent(this); | ||
jobs.enqueue(j); | ||
} | ||
|
||
void AsyncJobs::prepend(AsyncJob *j) | ||
{ | ||
if (!j) return; | ||
|
||
//reparent object to handle jobs memory from AsyncJobs | ||
j->setParent(this); | ||
jobs.prepend(j); | ||
} | ||
|
||
void AsyncJobs::start() | ||
{ | ||
if (running) return; | ||
running = true; | ||
|
||
//start running or queued jobs if any | ||
dequeueStartJob({}); | ||
} | ||
|
||
void AsyncJobs::dequeueStartJob(const QVariant &data) | ||
{ | ||
if (jobs.isEmpty()) | ||
{ | ||
//end of job queue, emit finished signal | ||
//and delete job runner | ||
emit finished(data); | ||
deleteLater(); | ||
return; | ||
} | ||
|
||
currentJob = jobs.dequeue(); | ||
connect(currentJob, SIGNAL(done(QVariant)), this, SLOT(jobDone(QVariant))); | ||
connect(currentJob, SIGNAL(error()), this, SLOT(jobFailed())); | ||
currentJob->start(data); | ||
} | ||
|
||
void AsyncJobs::jobFailed() | ||
{ | ||
emit failed(currentJob); | ||
deleteLater(); | ||
} | ||
|
||
void AsyncJobs::jobDone(const QVariant &data) | ||
{ | ||
dequeueStartJob(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#ifndef ASYNCJOBS_H | ||
#define ASYNCJOBS_H | ||
|
||
#include <QObject> | ||
#include <QByteArray> | ||
#include <QQueue> | ||
#include <functional> | ||
#include <QVariant> | ||
|
||
/* | ||
* Classes for running jobs | ||
* It handles chaining the jobs to start the next one after one has completed successfuly. | ||
* It also handles failure: when a job fails, the job queue is stopped. | ||
* | ||
* The AsyncJobs manages the job queue and it autodeletes itself when finished for convenience. | ||
* | ||
* AsyncJobs queue support adding more jobs to the queue dynamically (even from the callback from | ||
* one running job). This is usefull to add jobs to the queue that are different based on the result | ||
* of the data received from the previous job. | ||
*/ | ||
|
||
class AsyncJob; | ||
|
||
typedef std::function<void(AsyncJob *job, const QVariant &data)> AsyncFunc; | ||
|
||
class AsyncJob: public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
AsyncJob(AsyncFunc jobfn): | ||
jobFunc(std::move(jobfn)) | ||
{} | ||
|
||
public slots: | ||
virtual void start(const QVariant &previous_data); | ||
|
||
virtual void emitSuccess(const QVariant &data = {}) { emit done(data); } | ||
virtual void emitFailed() { emit error(); } | ||
|
||
signals: | ||
void done(const QVariant &data); | ||
void error(); | ||
|
||
protected: | ||
//callback with data from previous Job | ||
AsyncFunc jobFunc = [](AsyncJob *, const QVariant &) {}; | ||
}; | ||
|
||
class AsyncJobs: public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
AsyncJobs(QObject *parent = nullptr); | ||
virtual ~AsyncJobs(); | ||
|
||
void append(AsyncJob *j); | ||
void prepend(AsyncJob *j); | ||
|
||
public slots: | ||
void start(); | ||
|
||
signals: | ||
void finished(const QVariant &data); | ||
void failed(AsyncJob *job); | ||
|
||
private slots: | ||
void dequeueStartJob(const QVariant &data); | ||
void jobDone(const QVariant &data); | ||
void jobFailed(); | ||
|
||
private: | ||
QQueue<AsyncJob *> jobs; | ||
bool running = false; | ||
AsyncJob *currentJob = nullptr; | ||
}; | ||
|
||
#endif // ASYNCJOBS_H |
Oops, something went wrong.