sigx++  2.0.1
tunnel_context.h
Go to the documentation of this file.
1 #ifndef _SIGX_TUNNEL_CONTEXT_H_
2 #define _SIGX_TUNNEL_CONTEXT_H_
3 
4 /*
5  * Copyright 2007 Klaus Triendl
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21 */
22 
23 #include <memory> // std::auto_ptr
24 #include <sigc++/type_traits.h>
25 #include <sigc++/adaptors/bound_argument.h>
26 #include <sigx/fwddecl.h>
27 #include <sigx/types.h>
28 #include <sigx/internal_types.h>
29 #include <sigx/dispatcher.h>
31 
32 
33 namespace sigx
34 {
35 
44 template<sync_type I_sync, typename T_return, typename T_unary_functor>
46 
47 
50 template<sync_type I_sync, typename T_return, typename T_adaptor>
51 tunnel_context<I_sync, T_return, T_adaptor>* make_new_tunnel_context(const shared_dispatchable& _A_disp, const tunnel_validity_tracker& _A_validity_tracker, const T_adaptor& _A_func)
52 {
53  return new tunnel_context<I_sync, T_return, T_adaptor>(_A_disp, _A_validity_tracker, _A_func);
54 }
55 
56 
66 template<typename T_return, typename T_unary_functor>
67 struct tunnel_context<ASYNC, T_return, T_unary_functor>: public tunnel_context_base
68 {
70  typedef T_return result_type;
71 
72  tunnel_context(const shared_dispatchable& _A_disp, const tunnel_validity_tracker& _A_validity_tracker, typename sigc::type_trait<T_unary_functor>::take _A_func):
73  tunnel_context_base(_A_disp, _A_validity_tracker),
74  m_boundmessage(_A_func)
75  {}
76 
81  {
83  return result_type();
84  }
85 
86  void invoke()
87  {
88  // async tunnels must delete themselves after dispatching
89  const std::auto_ptr<this_type> autodelete_this(this);
90 
91  // call functor in the context of the server thread, disregard return value
92  m_boundmessage();
93  }
94 
95 private:
96  T_unary_functor m_boundmessage;
97 };
98 
99 
111 template<typename T_return, typename T_unary_functor>
112 struct tunnel_context<SYNC, T_return, T_unary_functor>: public sync_tunnel_context_base
113 {
115  typedef T_return result_type;
116 
117  tunnel_context(const shared_dispatchable& _A_disp, const tunnel_validity_tracker& _A_validity_tracker, typename sigc::type_trait<T_unary_functor>::take _A_func):
118  sync_tunnel_context_base(_A_disp, _A_validity_tracker),
119  m_boundmessage(_A_func),
120  m_bound_result(result_type())
121  {}
122 
126  T_return tunnel()
127  {
128  const std::auto_ptr<this_type> autodelete_this(this);
129 
130  Glib::Mutex::Lock lock(m_mutex);
131  // rather call tunnel_context_base::dispatch_me() than
132  // sync_tunnel_context_base::dispatch_me() because we want to ensure
133  // that the result is returned while we still hold the lock
135  // synchronize with other end of the tunnel
136  m_cond.wait(m_mutex);
137 
138  return m_bound_result.invoke();
139  }
140 
141  void invoke()
142  {
143  Glib::Mutex::Lock lock(m_mutex);
144  // save result
145  m_bound_result = m_boundmessage();
146 
147  // tell the one end of the tunnel that we are done
148  m_cond.signal();
149  }
150 
151 private:
152  T_unary_functor m_boundmessage;
153  sigc::bound_argument<typename sigc::type_trait<T_return>::type> m_bound_result;
154 };
155 
156 
168 template<typename T_unary_functor>
169 struct tunnel_context<SYNC, void, T_unary_functor>: public sync_tunnel_context_base
170 {
172  typedef void result_type;
173 
174  tunnel_context(const shared_dispatchable& _A_disp, const tunnel_validity_tracker& _A_validity_tracker, typename sigc::type_trait<T_unary_functor>::take _A_func):
175  sync_tunnel_context_base(_A_disp, _A_validity_tracker),
176  m_boundmessage(_A_func)
177  {}
178 
182  void tunnel()
183  {
184  const std::auto_ptr<this_type> autodelete_this(this);
185 
187  }
188 
189  void invoke()
190  {
191  Glib::Mutex::Lock lock(m_mutex);
192  m_boundmessage();
193 
194  // tell the one end of the tunnel that we are done
195  m_cond.signal();
196  }
197 
198 private:
199  T_unary_functor m_boundmessage;
200 };
201 
202 
203 } // namespace sigx
204 
205 
206 #endif // file guard