ICF 3.1.1.10
Technical documentation of ICF Libraries
CGeneralTaskPromise.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// STL includes
10#include <memory>
11#include <list>
12
13// QT includes
14#include <QtCore/QSemaphore>
15#include <QtCore/QMutex>
16#include <QtCore/QThread>
17#include <QtCore/QThreadPool>
18
19// ICF includes
20#include <iproc/ITaskPromise.h>
21#include <iproc/CTaskState.h>
22
23
24namespace iproc
25{
26
27
33class CGeneralTaskPromise: virtual protected ITaskPromise
34{
35public:
41
47
51 void SetCancel();
52
57 void SetFinalStatus(Status status);
58
62 bool IsCanceled() const;
63
64 // Reimplemented (iproc::ITaskPromise)
65 Status GetStatus() const override;
66
73 static CTaskState RunConcurrent(std::function<bool (const CGeneralTaskPromise&)> func, QThread::Priority priority = QThread::Priority::InheritPriority);
74
84 static CTaskState RunPooled(QThreadPool& threadPool, std::function<bool (const CGeneralTaskPromise&)> func, int priority = 0);
85
86 typedef std::list<std::function<CTaskState ()>> Sequence;
95 static CTaskState RunSequence(Sequence&& functions, std::unique_ptr<istd::IPolymorphic>&& statePtr = nullptr);
96
97protected:
103 virtual void OnFinalStatusSet(Status status);
108
109 // Reimplemented (iproc::ITaskPromise)
110 bool SetReadyHandler(Handler* handlerPtr) override;
111 void OnCanceledByUser() override;
112 ITaskPromise::Status WaitForFinished(const double* periodPtr = nullptr) override;
113
114private:
115 std::atomic<Status> m_operationStatus;
116 Handler* m_handlerPtr;
117 QSemaphore m_runningStateSemaphore;
118 mutable QMutex m_statusMutex;
119 mutable QRecursiveMutex m_handlerMutex;
120};
121
122
123} // namespace iproc
124
125
General implementation of iproc::ITaskPromise.
static CTaskState RunSequence(Sequence &&functions, std::unique_ptr< istd::IPolymorphic > &&statePtr=nullptr)
Run list of functions in the sequence.
bool IsCanceled() const
Check if this promise is canceled.
CTaskState StartAsyncTask()
Start async .
static CTaskState RunPooled(QThreadPool &threadPool, std::function< bool(const CGeneralTaskPromise &)> func, int priority=0)
Run concurrent some code thread pool.
void SetCancel()
Say this promise to be canceled.
virtual void OnFinalStatusSet(Status status)
Called when the final status was set.
CGeneralTaskPromise()
Construct promise in canceled state.
void SetFinalStatus(Status status)
Set final state of this operation promise.
ITaskPromise::Status WaitForFinished(const double *periodPtr=nullptr) override
Wait for operation finished.
void OnCanceledByUser() override
Called when user forced to cancel this operation.
void InitRunningState()
Reset current.
bool SetReadyHandler(Handler *handlerPtr) override
Set final handler.
static CTaskState RunConcurrent(std::function< bool(const CGeneralTaskPromise &)> func, QThread::Priority priority=QThread::Priority::InheritPriority)
Run concurrent some code in new thread.
Status GetStatus() const override
Get current state of operation.
std::list< std::function< CTaskState()> > Sequence
Represent state of asynchronous operation.
Definition CTaskState.h:28
Interface for some operation promise.
Status
Status of promise.
This namespace containes interfaces and implementation of data processing concepts.

© Witold Gantzke and Kirill Lepskiy