This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/example/doric/async/DoricAsyncResult.cpp

32 lines
711 B
C++
Raw Normal View History

2021-02-04 16:59:58 +08:00
#include "DoricAsyncResult.h"
2021-05-31 16:00:06 +08:00
#include <QWaitCondition>
2021-02-04 16:59:58 +08:00
DoricAsyncResult::DoricAsyncResult() {}
2021-05-31 16:00:06 +08:00
void DoricAsyncResult::setResult(QString result) {
this->result = result;
this->resultCallback();
}
2021-02-04 16:59:58 +08:00
2021-05-25 10:45:45 +08:00
void DoricAsyncResult::setError(QString exception) { this->result = exception; }
2021-02-04 16:59:58 +08:00
2021-05-31 16:00:06 +08:00
bool DoricAsyncResult::hasResult() { return !(result.isEmpty()); }
QString DoricAsyncResult::getResult() { return this->result; }
QString DoricAsyncResult::waitUntilResult() {
if (hasResult()) {
return this->result;
}
2021-02-04 16:59:58 +08:00
2021-05-31 16:00:06 +08:00
QMutex mutex;
QWaitCondition condition;
this->resultCallback = [&condition]() { condition.wakeAll(); };
2021-02-04 16:59:58 +08:00
2021-05-31 16:00:06 +08:00
mutex.lock();
condition.wait(&mutex);
mutex.unlock();
return this->result;
}