Async 1.8.0
AsyncThreadSigCSignal.h
Go to the documentation of this file.
1
32#ifndef ASYNC_THREAD_SIGC_SIGNAL_INCLUDED
33#define ASYNC_THREAD_SIGC_SIGNAL_INCLUDED
34
35
36/****************************************************************************
37 *
38 * System Includes
39 *
40 ****************************************************************************/
41
42#include <sigc++/sigc++.h>
43
44
45/****************************************************************************
46 *
47 * Project Includes
48 *
49 ****************************************************************************/
50
51#include <AsyncApplication.h>
52#include <AsyncMutex.h>
53#include <AsyncTaskRunner.h>
54
55
56/****************************************************************************
57 *
58 * Local Includes
59 *
60 ****************************************************************************/
61
62
63
64/****************************************************************************
65 *
66 * Forward declarations
67 *
68 ****************************************************************************/
69
70
71
72/****************************************************************************
73 *
74 * Namespace
75 *
76 ****************************************************************************/
77
78namespace Async
79{
80
81
82/****************************************************************************
83 *
84 * Forward declarations of classes inside of the declared namespace
85 *
86 ****************************************************************************/
87
88
89
90/****************************************************************************
91 *
92 * Defines & typedefs
93 *
94 ****************************************************************************/
95
96
97
98/****************************************************************************
99 *
100 * Exported Global Variables
101 *
102 ****************************************************************************/
103
104
105
106/****************************************************************************
107 *
108 * Class definitions
109 *
110 ****************************************************************************/
111
139template <typename T_ret, typename... Args>
141{
142 public:
143 typedef T_ret result_type;
144 typedef sigc::slot<T_ret, Args...> slot_type;
145 typedef typename sigc::slot_list<slot_type> slot_list_type;
146 typedef typename sigc::signal<T_ret, Args...> signal_type;
147 typedef typename signal_type::iterator iterator;
148 typedef typename signal_type::const_iterator const_iterator;
149
151
161 ThreadSigCSignal(Mode mode=NON_SYNCHRONOUS) : m_mode(mode) {}
162
167
172 {
173 m_runner.cancel();
174 std::lock_guard<Async::Mutex> lk(m_mu);
175 m_sig.clear();
176 }
177
182
189 {
190 return m_sig.connect(slt);
191 }
192
203 template <typename... T>
204 result_type emit(T&&... args)
205 {
206 return handleSignal(std::forward<T>(args)...);
207 }
208
221 template <typename... T>
223 {
224 return handleReverseSignal(std::forward<T>(args)...);
225 }
226
237 template <typename... T>
239 {
240 return handleSignal(std::forward<T>(args)...);
241 }
242
248 {
249 return sigc::mem_fun(*this, &ThreadSigCSignal::handleSignal<Args...>);
250 }
251
257 {
258 return m_sig.slots();
259 }
260
265 const slot_list_type slots(void) const
266 {
267 return m_sig.slots();
268 }
269
270 private:
271 sigc::signal<T_ret, Args...> m_sig;
272 const Mode m_mode;
273 Async::Mutex m_mu;
274 Async::TaskRunner m_runner;
275
276 template <typename... T>
277 result_type handleSignal(T&&... args)
278 {
279 if (m_mode == SYNCHRONOUS)
280 {
281 std::lock_guard<Async::Mutex> lk(m_mu);
282 return m_sig.emit(std::forward<T>(args)...);
283 }
284 else
285 {
286 m_runner(&signal_type::emit, &m_sig, std::forward<T>(args)...);
287 }
288 return result_type();
289 }
290
291 template <typename... T>
292 result_type handleReverseSignal(T&&... args)
293 {
294 if (m_mode == SYNCHRONOUS)
295 {
296 std::lock_guard<Async::Mutex> lk(m_mu);
297 return m_sig.emit_reverse(std::forward<T>(args)...);
298 }
299 else
300 {
301 m_runner(&signal_type::emit_reverse, &m_sig, std::forward<T>(args)...);
302 }
303 return result_type();
304 }
305}; /* class ThreadSigCSignal */
306
307
308} /* namespace */
309
310#endif /* ASYNC_THREAD_SIGC_SIGNAL_INCLUDED */
311
312/*
313 * This file has not been truncated
314 */
The core class for writing asyncronous applications.
A mutex for synchronizing threads with the main Async thread.
Run tasks from the Async main thread.
Synchronize execution from a thread with the main Async thread.
Definition AsyncMutex.h:143
Run tasks from the Async main thread with automatic cleanup.
void cancel(void)
Cancel all queued tasks for this object.
A threadsafe SigC signal.
result_type emit_reverse(T &&... args)
Queue the emission of this signal in reverse order.
ThreadSigCSignal(Mode mode=NON_SYNCHRONOUS)
Constructor.
sigc::signal< T_ret, Args... > signal_type
signal_type::iterator iterator
ThreadSigCSignal & operator=(const ThreadSigCSignal &)=delete
The assignment operator is deleted.
ThreadSigCSignal(const ThreadSigCSignal &)=delete
The copy constructor is deleted.
result_type operator()(T &&... args)
Queue the emission of this signal.
sigc::slot< T_ret, Args... > slot_type
const slot_list_type slots(void) const
Return a list of the connected slots.
signal_type::const_iterator const_iterator
sigc::slot_list< slot_type > slot_list_type
result_type emit(T &&... args)
Queue the emission of this signal.
slot_type make_slot(void)
Return a slot that emits this signal.
slot_list_type slots(void)
Return a list of the connected slots.
iterator connect(const slot_type &slt)
Connect this signal to the given slot.
Namespace for the asynchronous programming classes.