OpenWalnut  1.5.0dev
WDeferredCallEvent.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WDEFERREDCALLEVENT_H
26 #define WDEFERREDCALLEVENT_H
27 
28 #include <boost/function.hpp>
29 
30 #include <QtCore/QEvent>
31 
32 #include "core/common/WConditionOneShot.h"
33 
34 #include "WEventTypes.h"
35 
36 /**
37  * A Qt event to call a function from within the GUI thread.
38  */
40 {
41 public:
42  /**
43  * Constructor.
44  *
45  * \param notify specify your own condition to wait for. This is needed since the QApplication doc tells us that ownership of an event is
46  * handed over to QT and that it is not save to use the event after posting it. This means we cannot utilize an internal condition in the
47  * event as it might be deleted already when calling wait() on it. Do not specify this variable to get a fire-and-forget call.
48  */
51  m_callCondition( notify )
52  {
53  }
54 
55  /**
56  * Destructor
57  */
59  {
60  }
61 
62  /**
63  * Constant which saves the number used to distinguish this event from other
64  * custom events.
65  */
66  static const QEvent::Type CUSTOM_TYPE = static_cast< QEvent::Type >( WQT_DEFERREDCALL );
67 
68  /**
69  * Call the function.
70  */
71  void call()
72  {
73  callImpl();
74  if( m_callCondition )
75  {
76  m_callCondition->notify();
77  }
78  }
79 
80  /**
81  * Get the condition that notifies about the finished execution of the specified function.
82  *
83  * \return the condition. Might be NULL if none was specified.
84  */
86  {
87  return m_callCondition;
88  }
89 protected:
90  /**
91  * Fired whenever the function was called.
92  */
94 
95  /**
96  * Call the functor.
97  */
98  virtual void callImpl() = 0;
99 private:
100 };
101 
102 /**
103  * Derived WDeferredCallEvent allowing result values in calls.
104  *
105  * \tparam Result return type
106  */
107 template< typename Result >
109 {
110 public:
111  /**
112  * Constructor.
113  *
114  * \param function the function to call
115  * \param result a pointer to store the result in. Can be NULL if you are not interested in the result. You need to alloc and free it. The
116  * reason for using an external pointer is that Qt doc tells us that you loose ownership on QEvents when using QCoreApplication::postEvent.
117  * The Event will be deleted after being processed, thus we cannot ensure safe access to members after posting the event.
118  * \param notify specify your own condition to wait for. This is needed since the QApplication doc tells us that ownership of an event is
119  * handed over to QT and that it is not save to use the event after posting it. This means we cannot utilize an internal condition in the
120  * event as it might be deleted already when calling wait() on it. Do not specify this variable to get a fire-and-forget call.
121  */
122  WDeferredCallResultEvent( boost::function< Result( void ) > function, Result* result, WCondition::SPtr notify = WCondition::SPtr() ):
123  WDeferredCallEventBase( notify ),
124  m_result( result ),
125  m_function( function )
126  {
127  }
128 
129  /**
130  * Destructor
131  */
133  {
134  // nothing to do
135  }
136 
137 protected:
138  /**
139  * Call the function.
140  */
141  virtual void callImpl()
142  {
143  if( m_result )
144  {
145  *m_result = m_function();
146  }
147  else
148  {
149  m_function();
150  }
151  }
152 private:
153  /**
154  * The result.
155  */
156  Result* m_result;
157 
158  /**
159  * the title of the widget to create
160  */
161  boost::function< Result( void ) > m_function;
162 };
163 
164 /**
165  * Derived WDeferredCallEvent allowing result values in calls. Specialization for VOID
166  */
167 template<>
169 {
170 public:
171  /**
172  * Constructor.
173  *
174  * \param function the function to call
175  * \param notify specify your own condition to wait for. This is needed since the QApplication doc tells us that ownership of an event is
176  * handed over to QT and that it is not save to use the event after posting it. This means we cannot utilize an internal condition in the
177  * event as it might be deleted already when calling wait() on it. Do not specify this variable to get a fire-and-forget call.
178  */
179  WDeferredCallResultEvent( boost::function< void( void ) > function, WCondition::SPtr notify = WCondition::SPtr() ):
180  WDeferredCallEventBase( notify ),
181  m_function( function )
182  {
183  }
184 
185  /**
186  * Destructor
187  */
189  {
190  // nothing to do
191  }
192 
193 protected:
194  /**
195  * Call the function.
196  */
197  virtual void callImpl()
198  {
199  m_function();
200  }
201 private:
202  /**
203  * the title of the widget to create
204  */
205  boost::function< void( void ) > m_function;
206 };
207 
208 /**
209  * Default deferred call without return type
210  */
212 
213 #endif // WDEFERREDCALLEVENT_H
std::shared_ptr< WCondition > SPtr
Shared pointer type for WCondition.
Definition: WCondition.h:48
A Qt event to call a function from within the GUI thread.
void call()
Call the function.
WDeferredCallEventBase(WCondition::SPtr notify=WCondition::SPtr())
Constructor.
WConditionOneShot::SPtr getDoneCondition() const
Get the condition that notifies about the finished execution of the specified function.
WCondition::SPtr m_callCondition
Fired whenever the function was called.
virtual void callImpl()=0
Call the functor.
static const QEvent::Type CUSTOM_TYPE
Constant which saves the number used to distinguish this event from other custom events.
virtual ~WDeferredCallEventBase()
Destructor.
Derived WDeferredCallEvent allowing result values in calls.
virtual ~WDeferredCallResultEvent()
Destructor.
boost::function< void(void) > m_function
the title of the widget to create
virtual void callImpl()
Call the function.
WDeferredCallResultEvent(boost::function< void(void) > function, WCondition::SPtr notify=WCondition::SPtr())
Constructor.
Derived WDeferredCallEvent allowing result values in calls.
boost::function< Result(void) > m_function
the title of the widget to create
virtual ~WDeferredCallResultEvent()
Destructor.
virtual void callImpl()
Call the function.
WDeferredCallResultEvent(boost::function< Result(void) > function, Result *result, WCondition::SPtr notify=WCondition::SPtr())
Constructor.
Result * m_result
The result.