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

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_WX___JOB_SYNC_CALL__HPP
2 #define GUI_WIDGETS_WX___JOB_SYNC_CALL__HPP
3 
4 /* $Id: async_call.hpp 47290 2022-12-20 20:37:03Z 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: Roman Katargin
30  *
31  * File Description:
32  *
33  */
34 
35 #include <gui/gui.hpp>
36 
37 #include <corelib/ncbiobj.hpp>
38 #include <corelib/ncbicntr.hpp>
39 
40 #include <util/icanceled.hpp>
41 
43 #include <gui/utils/job_future.hpp>
44 
45 // This header must (at least indirectly) precede any wxWidgets headers.
47 
48 #include <wx/string.h>
49 
50 
52 
53 class IAppJob;
54 
55 /// Class for GUI asyncronous execition.
56 ///
57 /// If async background call takes too much time GBench GUI automatically shows
58 /// a window to inform the user to wait
59 ///
61 {
62 public:
63  class Call : public ICanceled
64  {
65  public:
66  Call(const wxString& descr) : m_Descr(descr) {}
67  Call() : m_Descr(wxT("Accessing network...")) {}
68  virtual ~Call() {}
69 
70  wxString GetDescr() const { return m_Descr; }
71 
72  virtual void Execute() = 0;
73 
74  void RequestCancel() { m_Canceled.Set(1); }
75  bool IsCancalable() const { return (m_Cancelable.Get() != 0); }
76 
77  // ICanceled implementation
78  virtual bool IsCanceled() const {
79  m_Cancelable.Set(1);
80  return (m_Canceled.Get() != 0);
81  }
82 
83  private:
84  wxString m_Descr;
85 
88  };
89 
90  CAsyncCall(Call& call);
91  ~CAsyncCall();
92 
93  void Execute();
94  static void Execute(Call& call);
95 
96  static bool InsideAsyncCall();
97 
98 private:
100  wxString m_Descr;
101  int m_JobId;
102 };
103 
104 template<class _Fty, typename _Rty>
106 {
107 public:
108  CLocalAsyncCallBind(_Fty&& func, const wxString& msg)
109  : CAsyncCall::Call(msg), m_Func(func) {}
110  virtual void Execute() { m_Ret = m_Func(*this); }
111  _Rty GetResult() { return m_Ret; }
112 private:
113  _Fty& m_Func;
114  _Rty m_Ret;
115 };
116 
117 template<class _Fty>
118 class CLocalAsyncCallBind<_Fty, void> : public CObject, public CAsyncCall::Call
119 {
120 public:
121  CLocalAsyncCallBind(_Fty&& func, const wxString& msg)
122  : CAsyncCall::Call(msg), m_Func(func) {}
123  virtual void Execute() { m_Func(*this); }
124  void GetResult() { return; }
125 private:
126  _Fty& m_Func;
127 };
128 
129 template<class _Fty>
131  const wxString& msg = wxT("Accessing network..."))
132 {
133  typedef typename std::invoke_result<_Fty, ICanceled&>::type _Rty;
134  typedef CLocalAsyncCallBind<_Fty, _Rty> _Bty;
135  CRef<_Bty> f(new _Bty(std::forward<_Fty>(_Fnarg), msg));
136  CAsyncCall asyncCall(*f);
137  asyncCall.Execute();
138  return f->GetResult();
139 }
140 
141 NCBI_GUIWIDGETS_WX_EXPORT void GUI_AsyncJoinFuture(const job_future_base& future, const string& msg);
142 
143 NCBI_GUIWIDGETS_WX_EXPORT bool GUI_AsyncExecUnit(IExecuteUnit& exec_unit, const wxString& msg);
144 
146 
147 #endif // GUI_WIDGETS_WX___JOB_SYNC_CALL__HPP
bool GUI_AsyncExecUnit(IExecuteUnit &exec_unit, const wxString &msg)
Definition: async_call.cpp:53
void GUI_AsyncJoinFuture(const job_future_base &future, const string &msg)
Definition: async_call.cpp:58
std::invoke_result< _Fty, ICanceled & >::type GUI_AsyncExec(_Fty &&_Fnarg, const wxString &msg=wxT("Accessing network..."))
Definition: async_call.hpp:130
CAtomicCounter_WithAutoInit m_Cancelable
Definition: async_call.hpp:87
virtual ~Call()
Definition: async_call.hpp:68
virtual bool IsCanceled() const
Definition: async_call.hpp:78
void RequestCancel()
Definition: async_call.hpp:74
wxString GetDescr() const
Definition: async_call.hpp:70
CAtomicCounter_WithAutoInit m_Canceled
Definition: async_call.hpp:86
wxString m_Descr
Definition: async_call.hpp:84
virtual void Execute()=0
Call(const wxString &descr)
Definition: async_call.hpp:66
bool IsCancalable() const
Definition: async_call.hpp:75
Class for GUI asyncronous execition.
Definition: async_call.hpp:61
void Execute()
Definition: async_call.cpp:194
wxString m_Descr
Definition: async_call.hpp:100
Call & m_Call
Definition: async_call.hpp:99
CAtomicCounter_WithAutoInit –.
Definition: ncbicntr.hpp:120
CLocalAsyncCallBind(_Fty &&func, const wxString &msg)
Definition: async_call.hpp:121
CLocalAsyncCallBind(_Fty &&func, const wxString &msg)
Definition: async_call.hpp:108
virtual void Execute()
Definition: async_call.hpp:110
CObject –.
Definition: ncbiobj.hpp:180
CRef –.
Definition: ncbiobj.hpp:618
IAppJob.
Definition: app_job.hpp:82
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
Workaround for wxWidgets header errors in certain configurations; MUST be included (at least indirect...
static int type
Definition: getdata.c:31
#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_GUIWIDGETS_WX_EXPORT
Definition: gui_export.h:543
#define wxT(x)
Definition: muParser.cpp:41
string Execute(const string &cmmd, const vector< string > &args, const string &data=kEmptyStr)
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
Modified on Wed Feb 28 07:12:55 2024 by modify_doxy.py rev. 669887