code format & class prefix
This commit is contained in:
16
doric-Qt/doric/async/DoricAsyncCall.h
Normal file
16
doric-Qt/doric/async/DoricAsyncCall.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef ASYNC_CALL_H
|
||||
#define ASYNC_CALL_H
|
||||
|
||||
#include <QThreadPool>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
|
||||
class DoricAsyncCall {
|
||||
|
||||
public:
|
||||
static void ensureRunInThreadPool(QThreadPool *threadPool,
|
||||
std::function<void()> lambda) {
|
||||
QtConcurrent::run(threadPool, lambda);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ASYNC_CALL_H
|
38
doric-Qt/doric/async/DoricAsyncResult.cpp
Normal file
38
doric-Qt/doric/async/DoricAsyncResult.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "DoricAsyncResult.h"
|
||||
|
||||
DoricAsyncResult::DoricAsyncResult() {}
|
||||
|
||||
DoricAsyncResult::DoricAsyncResult(QJSValue result) { this->result = result; }
|
||||
|
||||
void DoricAsyncResult::setResult(QJSValue result) {
|
||||
this->result = result;
|
||||
if (this->callback != NULL) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
void DoricAsyncResult::setError(QJSValue exception) {
|
||||
this->result = exception;
|
||||
if (this->callback != NULL) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
bool DoricAsyncResult::hasResult() { return !result.equals(EMPTY); }
|
||||
|
||||
QJSValue DoricAsyncResult::getResult() { return this->result; }
|
||||
|
||||
void DoricAsyncResult::setCallback(DoricCallback *callback) {
|
||||
this->callback = callback;
|
||||
if (this->result.isError()) {
|
||||
this->callback->onError(result);
|
||||
this->callback->onFinish();
|
||||
} else if (!result.equals(EMPTY)) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
DoricSettableFuture *DoricAsyncResult::synchronous() { return NULL; }
|
34
doric-Qt/doric/async/DoricAsyncResult.h
Normal file
34
doric-Qt/doric/async/DoricAsyncResult.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef ASYNCRESULT_H
|
||||
#define ASYNCRESULT_H
|
||||
|
||||
#include <QJSValue>
|
||||
|
||||
#include "DoricCallback.h"
|
||||
#include "DoricSettableFuture.h"
|
||||
|
||||
static QJSValue EMPTY(QJSValue::NullValue);
|
||||
|
||||
class DoricAsyncResult {
|
||||
private:
|
||||
QJSValue result = EMPTY;
|
||||
DoricCallback *callback;
|
||||
|
||||
public:
|
||||
DoricAsyncResult();
|
||||
|
||||
DoricAsyncResult(QJSValue result);
|
||||
|
||||
void setResult(QJSValue result);
|
||||
|
||||
void setError(QJSValue exception);
|
||||
|
||||
bool hasResult();
|
||||
|
||||
QJSValue getResult();
|
||||
|
||||
void setCallback(DoricCallback *callback);
|
||||
|
||||
DoricSettableFuture *synchronous();
|
||||
};
|
||||
|
||||
#endif // ASYNCRESULT_H
|
15
doric-Qt/doric/async/DoricCallback.h
Normal file
15
doric-Qt/doric/async/DoricCallback.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef CALLBACK_H
|
||||
#define CALLBACK_H
|
||||
|
||||
#include <QJSValue>
|
||||
|
||||
class DoricCallback {
|
||||
public:
|
||||
virtual void onResult(QJSValue result) = 0;
|
||||
|
||||
virtual void onError(QJSValue error) = 0;
|
||||
|
||||
virtual void onFinish() = 0;
|
||||
};
|
||||
|
||||
#endif // CALLBACK_H
|
17
doric-Qt/doric/async/DoricSettableFuture.cpp
Normal file
17
doric-Qt/doric/async/DoricSettableFuture.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "DoricSettableFuture.h"
|
||||
|
||||
void DoricSettableFuture::set(QJSValue result) {
|
||||
if (mReadyLatch == NULL) {
|
||||
qDebug() << "Result has already been set!";
|
||||
return;
|
||||
}
|
||||
mResult = result;
|
||||
delete mReadyLatch;
|
||||
}
|
||||
|
||||
QJSValue DoricSettableFuture::get() {
|
||||
mReadyLatch->lock();
|
||||
return mResult;
|
||||
}
|
19
doric-Qt/doric/async/DoricSettableFuture.h
Normal file
19
doric-Qt/doric/async/DoricSettableFuture.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef SETTABLE_FUTURE_H
|
||||
#define SETTABLE_FUTURE_H
|
||||
|
||||
#include <QJSValue>
|
||||
|
||||
#include "utils/DoricCountDownLatch.h"
|
||||
|
||||
class DoricSettableFuture {
|
||||
private:
|
||||
QJSValue mResult;
|
||||
DoricCountDownLatch *mReadyLatch = new DoricCountDownLatch();
|
||||
|
||||
public:
|
||||
void set(QJSValue result);
|
||||
|
||||
QJSValue get();
|
||||
};
|
||||
|
||||
#endif // SETTABLE_FUTURE_H
|
@@ -1,16 +0,0 @@
|
||||
#ifndef ASYNC_CALL_H
|
||||
#define ASYNC_CALL_H
|
||||
|
||||
#include <QThreadPool>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
|
||||
class AsyncCall {
|
||||
|
||||
public:
|
||||
static void ensureRunInThreadPool(QThreadPool *threadPool, std::function<void()> lambda)
|
||||
{
|
||||
QtConcurrent::run(threadPool, lambda);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ASYNC_CALL_H
|
@@ -1,56 +0,0 @@
|
||||
#include "async_result.h"
|
||||
|
||||
AsyncResult::AsyncResult()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
AsyncResult::AsyncResult(QJSValue result)
|
||||
{
|
||||
this->result = result;
|
||||
}
|
||||
|
||||
void AsyncResult::setResult(QJSValue result)
|
||||
{
|
||||
this->result = result;
|
||||
if (this->callback != NULL) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncResult::setError(QJSValue exception)
|
||||
{
|
||||
this->result = exception;
|
||||
if (this->callback != NULL) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncResult::hasResult()
|
||||
{
|
||||
return !result.equals(EMPTY);
|
||||
}
|
||||
|
||||
QJSValue AsyncResult::getResult()
|
||||
{
|
||||
return this->result;
|
||||
}
|
||||
|
||||
void AsyncResult::setCallback(Callback *callback)
|
||||
{
|
||||
this->callback = callback;
|
||||
if (this->result.isError()) {
|
||||
this->callback->onError(result);
|
||||
this->callback->onFinish();
|
||||
} else if (!result.equals(EMPTY)) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
SettableFuture* AsyncResult::synchronous()
|
||||
{
|
||||
return NULL;
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
#ifndef ASYNCRESULT_H
|
||||
#define ASYNCRESULT_H
|
||||
|
||||
#include <QJSValue>
|
||||
|
||||
#include "callback.h"
|
||||
#include "settable_future.h"
|
||||
|
||||
static QJSValue EMPTY(QJSValue::NullValue);
|
||||
|
||||
class AsyncResult
|
||||
{
|
||||
private:
|
||||
QJSValue result = EMPTY;
|
||||
Callback *callback;
|
||||
|
||||
public:
|
||||
AsyncResult();
|
||||
|
||||
AsyncResult(QJSValue result);
|
||||
|
||||
void setResult(QJSValue result);
|
||||
|
||||
void setError(QJSValue exception);
|
||||
|
||||
bool hasResult();
|
||||
|
||||
QJSValue getResult();
|
||||
|
||||
void setCallback(Callback *callback);
|
||||
|
||||
SettableFuture *synchronous();
|
||||
};
|
||||
|
||||
#endif // ASYNCRESULT_H
|
@@ -1,15 +0,0 @@
|
||||
#ifndef CALLBACK_H
|
||||
#define CALLBACK_H
|
||||
|
||||
#include <QJSValue>
|
||||
|
||||
class Callback {
|
||||
public:
|
||||
virtual void onResult(QJSValue result) = 0;
|
||||
|
||||
virtual void onError(QJSValue error) = 0;
|
||||
|
||||
virtual void onFinish() = 0;
|
||||
};
|
||||
|
||||
#endif // CALLBACK_H
|
@@ -1,20 +0,0 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "settable_future.h"
|
||||
|
||||
void SettableFuture::set(QJSValue result)
|
||||
{
|
||||
if (mReadyLatch == NULL)
|
||||
{
|
||||
qDebug() << "Result has already been set!";
|
||||
return;
|
||||
}
|
||||
mResult = result;
|
||||
delete mReadyLatch;
|
||||
}
|
||||
|
||||
QJSValue SettableFuture::get()
|
||||
{
|
||||
mReadyLatch->lock();
|
||||
return mResult;
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
#ifndef SETTABLE_FUTURE_H
|
||||
#define SETTABLE_FUTURE_H
|
||||
|
||||
#include <QJSValue>
|
||||
#include "utils/count_down_latch.h"
|
||||
|
||||
class SettableFuture {
|
||||
private:
|
||||
QJSValue mResult;
|
||||
CountDownLatch *mReadyLatch = new CountDownLatch();
|
||||
|
||||
public:
|
||||
void set(QJSValue result);
|
||||
|
||||
QJSValue get();
|
||||
};
|
||||
|
||||
#endif // SETTABLE_FUTURE_H
|
Reference in New Issue
Block a user