NCBI C++ ToolKit
app_job_impl.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OBJUTILS___APP_JOB_IMPL__HPP
2 #define GUI_OBJUTILS___APP_JOB_IMPL__HPP
3 
4 /* $Id: app_job_impl.hpp 47374 2023-02-23 00:42:16Z evgeniev $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  *
33  */
34 
35 /** @addtogroup GUI_UTILS
36 *
37 * @{
38 */
39 
40 #include <gui/utils/app_job.hpp>
41 #include <gui/utils/locks.hpp>
42 
43 #include <corelib/ncbiobj.hpp>
44 
47 #include <gui/utils/locker.hpp>
48 
50 
51 ///////////////////////////////////////////////////////////////////////////////
52 /// CAppJobProgress
53 /// Default implementation for IAppJobProgress - the class encapsulates a text
54 /// message and a numeric value indicating the progress of the task normalized
55 /// in [0.0, 1.0] range (0.0 - just started, 1.0 - 100% done).
56 ///
57 /// MT safety: class is sync-ed for concurrent access from multiple thereads.
58 ///
60  public CObject,
61  public IAppJobProgress
62 {
63 public:
65  CAppJobProgress(float done, const string& text);
66 
67  CAppJobProgress(const CAppJobProgress& progress);
68  CAppJobProgress& operator=(const CAppJobProgress& progress);
69 
70  void SetNormDone(float done);
71  void SetText(const string& text);
72 
73  /// @name IAppJobProgress implementation
74  /// @{
75  virtual float GetNormDone() const override;
76  virtual string GetText() const override;
77  /// @}
78 
79 private:
80  /// copy constructor, assignment implementation
81  void x_CopyFrom(const CAppJobProgress& progress);
82 
83 protected:
84  float m_Done;
85  string m_Text;
86  mutable CGuiRWLock m_Lock; ///< MT sync.lock
87 };
88 
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 /// CAppJobError
92 /// Default implementation for IAppJobError - encapsulates a text error message.
93 
95  public CObject,
96  public IAppJobError
97 {
98 public:
99  CAppJobError( const string& text, bool user_level = false );
100 
101  /// @name IAppJobError implementation
102  /// @{
103  virtual string GetText() const override;
104 
105  virtual bool IsUserLevel() const override;
106  /// @}
107 protected:
108  string m_Text;
110 };
111 
112 
113 ///////////////////////////////////////////////////////////////////////////////
114 /// CAppJobTextResult
115 /// Default implementation for result object - encapsulates a text error message.
116 
118  public CObject
119 {
120 public:
121  CAppJobTextResult(const string& text);
122 
123  virtual string GetText() const;
124 
125 protected:
126  string m_Text;
127 };
128 
129 ///////////////////////////////////////////////////////////////////////////////
130 /// Base class to build jobs with cancel functionality
131 ///
132 
133 class CJobCancelable : public CObject, public IAppJob
134 {
135  class CCanceled : public CObject, public CCanceledImpl
136  {
137  };
138 
139 public:
141 
142  void SetDataLocker(ILocker* locker) { m_DataLocker.reset(locker); }
143 
144  /// @name IAppJob implementation
145  /// @{
146  virtual void RequestCancel() override { return m_Canceled->RequestCancel(); }
147  virtual bool IsCanceled() const override { return m_Canceled->IsCanceled(); }
148  /// @}
149 
150 protected:
152 
154 
155 private:
157 
158  unique_ptr<ILocker> m_DataLocker;
159 };
160 
161 ///////////////////////////////////////////////////////////////////////////////
162 /// CAppJob - default implementation of IAppJob that could be used as a base
163 /// class.
165 {
166 public:
167  CAppJob(const string& descr = "AppJob");
168 
169  /// @name IAppJob implementation
170  /// @{
171 
172  /// implement Run() in derived classes, make sure that exceptions are
173  /// handled and m_StopRequested flag is respected.
174  virtual CConstIRef<IAppJobProgress> GetProgress() override;
175  virtual CRef<CObject> GetResult() override;
176  virtual CConstIRef<IAppJobError> GetError() override;
177  virtual string GetDescr() const override;
178  /// @}
179 
180 
181 protected:
182  virtual void x_SetStatusText(const string& text);
183  virtual void x_ResetState();
184 
185 protected:
186  mutable CFastMutex m_Mutex; /// mutex to sync our internals
187 
188  /// UI-friendly Job descriptor
189  string m_Descr;
190 
191  /// Current status of the Job
192  string m_Status;
193 
195 };
196 
198 {
199 public:
200  CAppJobExecuteUnit(IExecuteUnit& executeUnit, const string& descr)
201  : CAppJob(descr), m_ExecuteUnit(&executeUnit) {}
202 
203  virtual EJobState Run() override
204  {
205  return m_ExecuteUnit->Execute(*x_GetICanceled()) ? eCompleted : eFailed;
206  }
207 
208 private:
210 };
211 
213 
214 /* @} */
215 
216 #endif // GUI_OBJUTILS___APP_JOB_IMPL__HPP
CAppJobError Default implementation for IAppJobError - encapsulates a text error message.
CAppJobProgress Default implementation for IAppJobProgress - the class encapsulates a text message an...
CAppJobTextResult Default implementation for result object - encapsulates a text error message.
CAppJob - default implementation of IAppJob that could be used as a base class.
CFastMutex –.
Definition: ncbimtx.hpp:667
Base class to build jobs with cancel functionality.
CObject –.
Definition: ncbiobj.hpp:180
CRWLock –.
Definition: ncbimtx.hpp:953
CRef –.
Definition: ncbiobj.hpp:618
IAppJobError.
Definition: app_job.hpp:65
IAppJobProgress.
Definition: app_job.hpp:50
IAppJob.
Definition: app_job.hpp:82
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
virtual CConstIRef< IAppJobProgress > GetProgress()=0
return progress object, the function shall be synchronized internally.
void SetDataLocker(ILocker *locker)
ICanceled * x_GetICanceled()
CRef< CAppJobError > m_Error
virtual EJobState Run() override
Function that does all the useful work, called by the Engine.
CGuiRWLock m_Lock
MT sync.lock.
string m_Descr
mutex to sync our internals
virtual string GetText() const =0
returns a text string describing current state
virtual CConstIRef< IAppJobError > GetError()=0
Returns IAppJobError object describing internal error that caused the Job to fail.
virtual bool IsCanceled() const override
virtual CRef< CObject > GetResult()=0
Returns the Job Result.
CAppJobExecuteUnit(IExecuteUnit &executeUnit, const string &descr)
CFastMutex m_Mutex
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
virtual string GetDescr() const =0
Returns a human readable description of the Job (optional)
unique_ptr< ILocker > m_DataLocker
string m_Status
Current status of the Job.
CRef< CCanceled > m_Canceled
CIRef< IExecuteUnit > m_ExecuteUnit
virtual void RequestCancel() override
RequestCancel() is called to notify the Job that it shall exit Run() function ASAP.
CLockerGuard x_GetGuard()
virtual float GetNormDone() const =0
return a number indicating the progress(0.0 - just started, 1.0 - done)
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:482
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIUTILS_EXPORT
Definition: gui_export.h:518
#define NCBI_GUIOBJUTILS_EXPORT
Definition: gui_export.h:512
GUI Core entry point to compile time overloaded sync.primitives (like Locks, Mutex,...
static void text(MDB_val *v)
Definition: mdb_dump.c:62
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
done
Definition: token1.c:1
Modified on Fri Sep 20 14:58:11 2024 by modify_doxy.py rev. 669887