11#include "kasync_export.h"
17#include <QSharedDataPointer>
26class FutureWatcherBase;
37struct KASYNC_EXPORT
Error
39 Error() : errorCode(0) {};
44 bool operator ==(
const Error &other)
const {
45 return (errorCode == other.errorCode) && (
errorMessage == other.errorMessage);
48 bool operator !=(
const Error &other)
const {
49 return !(*
this == other);
52 operator bool()
const {
53 return (errorCode != 0);
66class KASYNC_EXPORT FutureBase
68 friend struct KAsync::Private::Execution;
69 friend class FutureWatcherBase;
72 virtual ~FutureBase();
76 bool isFinished()
const;
79 void setError(
const Error &error);
80 void addError(
const Error &error);
83 int errorCode()
const;
87 void setProgress(qreal progress);
88 void setProgress(
int processed,
int total);
91 class KASYNC_EXPORT PrivateBase :
public QSharedData
94 explicit PrivateBase(
const KAsync::Private::ExecutionPtr &execution);
95 virtual ~PrivateBase();
97 void releaseExecution();
107 explicit FutureBase();
108 explicit FutureBase(FutureBase::PrivateBase *dd);
109 FutureBase(
const FutureBase &other);
110 FutureBase &operator=(
const FutureBase &other) =
default;
112 void addWatcher(KAsync::FutureWatcherBase *watcher);
113 void releaseExecution();
126class FutureGeneric :
public FutureBase
128 friend class FutureWatcher<T>;
132 void waitForFinished()
const
137 FutureWatcher<T> watcher;
141 watcher.setFuture(*
static_cast<const KAsync::Future<T>*
>(
this));
147 explicit FutureGeneric(
const KAsync::Private::ExecutionPtr &execution)
148 : FutureBase(new Private(execution))
151 FutureGeneric(
const FutureGeneric &) =
default;
152 FutureGeneric &operator=(
const FutureGeneric &) =
default;
155 class Private :
public FutureBase::PrivateBase
158 explicit Private(
const KAsync::Private::ExecutionPtr &execution)
159 : FutureBase::PrivateBase(execution)
162 std::conditional_t<std::is_void<T>::value,
int , T> value;
185class Future :
public FutureGeneric<T>
188 friend class KAsync::Private::ExecutorBase;
190 template<
typename T_>
191 friend class KAsync::FutureWatcher;
198 : FutureGeneric<T>(KAsync::Private::ExecutionPtr())
204 Future(
const Future<T> &other)
205 : FutureGeneric<T>(other)
219 void setValue(
const T &value)
221 dataImpl()->value = value;
231 return dataImpl()->value;
236 return &(dataImpl()->value);
239 const T *operator->()
const
241 return &(dataImpl()->value);
246 return dataImpl()->value;
249 const T &operator*()
const
251 return dataImpl()->value;
264 void waitForFinished()
const;
284 bool isFinished()
const;
310 int errorCode()
const;
328 void setProgress(
int processed,
int total);
335 void setProgress(qreal progress);
338 void setResult(
const T &value)
340 dataImpl()->value = value;
341 FutureBase::setFinished();
346 Future(
const KAsync::Private::ExecutionPtr &execution)
347 : FutureGeneric<T>(execution)
352 inline auto dataImpl()
354 return static_cast<typename FutureGeneric<T>::Private*
>(this->d.data());
357 inline auto dataImpl()
const
359 return static_cast<typename FutureGeneric<T>::Private*
>(this->d.data());
375class Future<void> :
public FutureGeneric<void>
377 friend class KAsync::Private::ExecutorBase;
384 : FutureGeneric<void>(KAsync::Private::ExecutionPtr())
389 Future(
const KAsync::Private::ExecutionPtr &execution)
390 : FutureGeneric<void>(execution)
399class KASYNC_EXPORT FutureWatcherBase :
public QObject
403 friend class FutureBase;
407 void futureProgress(qreal progress);
410 FutureWatcherBase(
QObject *parent =
nullptr);
412 virtual ~FutureWatcherBase();
414 void futureReadyCallback();
415 void futureProgressCallback(qreal progress);
417 void setFutureImpl(
const KAsync::FutureBase &future);
422 KAsync::FutureBase future;
428 Q_DISABLE_COPY(FutureWatcherBase)
445class FutureWatcher :
public FutureWatcherBase
448 friend class KAsync::FutureGeneric<T>;
455 FutureWatcher(
QObject *parent =
nullptr)
456 : FutureWatcherBase(parent)
467 void setFuture(
const KAsync::Future<T> &future)
469 setFutureImpl(*
static_cast<const KAsync::FutureBase*
>(&future));
475 KAsync::Future<T> future()
const
477 return *
static_cast<KAsync::Future<T>*
>(&d->future);
497 void futureProgress(qreal progress);
501 FutureWatcher(
const FutureWatcher<T> &) =
delete;
502 FutureWatcher &operator=(
const FutureWatcher<T> &) =
delete;
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
QDebug operator<<(QDebug dbg, const PerceptualColor::MultiSpinBoxSection &value)
int exec(ProcessEventsFlags flags)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)