libqi-api  2.4.3.28
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventloop.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2012 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6 
7 #ifndef _QI_EVENTLOOP_HPP_
8 # define _QI_EVENTLOOP_HPP_
9 
10 # ifdef _MSC_VER
11 # pragma warning( disable: 4503 ) // decorated name length
12 # endif
13 
14 # include <boost/shared_ptr.hpp>
15 # include <boost/function.hpp>
16 
17 # include <qi/types.hpp>
18 # include <qi/api.hpp>
19 # include <qi/clock.hpp>
21 
22 # ifdef _MSC_VER
23 # pragma warning( push )
24 # pragma warning( disable: 4251 )
25 # endif
26 
27 namespace boost {
28  namespace asio {
29  class io_service;
30 }}
31 
32 namespace qi
33 {
34  template<typename T> class Future;
35 
36  class EventLoopPrivate;
42  {
43  public:
50  EventLoop(const std::string& name = "eventloop");
51 
53  ~EventLoop();
58  bool isInThisContext();
63  void start(int nthreads = 0);
64 
66  void join();
68  void stop();
73  void setEmergencyCallback(boost::function<void()> cb);
74 
79  void setMaxThreads(unsigned int max);
80 
82  void *nativeHandle();
83 
85 
92  template<typename R>
93  Future<R> async(const boost::function<R()>& callback, uint64_t usDelay=0);
94  Future<void> async(const boost::function<void ()>& callback, uint64_t usDelay=0);
95  Future<void> async(const boost::function<void ()>& callback, qi::Duration delay);
96  Future<void> async(const boost::function<void ()>& callback, qi::SteadyClockTimePoint timepoint);
97 
98  template <typename R>
99  typename boost::enable_if_c<!boost::is_same<R, void>::value,
100  qi::Future<R> >::type
101  async(const boost::function<R()>& callback, qi::Duration delay)
102  {
103  return ExecutionContext::async<R>(callback, delay);
104  }
105  template <typename R>
106  typename boost::enable_if_c<!boost::is_same<R, void>::value,
107  qi::Future<R> >::type
108  async(const boost::function<R()>& callback,
110  {
111  return ExecutionContext::async<R>(callback, tp);
112  }
114 
120  void post(const boost::function<void ()>& callback, uint64_t usDelay);
121  void post(const boost::function<void ()>& callback, qi::Duration delay);
122  void post(const boost::function<void ()>& callback, qi::SteadyClockTimePoint timepoint);
123  void post(const boost::function<void ()>& callback)
124  {
125  return post(callback, 0);
126  }
127 
136  Future<void> monitorEventLoop(EventLoop* helper, uint64_t maxUsDelay);
137 
138  EventLoopPrivate *_p;
139  std::string _name;
140  };
141 
144 
145  namespace detail
146  {
147  template <typename R, typename ARG0>
148  typename boost::enable_if<
149  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
150  Future<R> >::type
151  asyncMaybeActor(const ARG0& arg0, const boost::function<R()>& cb,
152  qi::Duration delay);
153  template <typename R, typename ARG0>
154  typename boost::disable_if<
155  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
156  Future<R> >::type
157  asyncMaybeActor(const ARG0& arg0, const boost::function<R()>& cb,
158  qi::Duration delay);
159  template <typename R, typename ARG0>
160  typename boost::enable_if<
161  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
162  Future<R> >::type
163  asyncMaybeActor(const ARG0& arg0, const boost::function<R()>& cb,
164  qi::SteadyClockTimePoint timepoint);
165  template <typename R, typename ARG0>
166  typename boost::disable_if<
167  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
168  Future<R> >::type
169  asyncMaybeActor(const ARG0& arg0, const boost::function<R()>& cb,
170  qi::SteadyClockTimePoint timepoint);
171  }
172 
175  template<typename R>
176  inline Future<R> async(boost::function<R()> callback, uint64_t usDelay=0)
177  {
178  return qi::getEventLoop()->async(callback, usDelay);
179  }
180  template<typename R>
181  inline Future<R> async(boost::function<R()> callback, qi::Duration delay)
182  {
183  return qi::getEventLoop()->async(callback, delay);
184  }
185  template<typename R>
186  inline Future<R> async(boost::function<R()> callback, qi::SteadyClockTimePoint timepoint)
187  {
188  return qi::getEventLoop()->async(callback, timepoint);
189  }
190 
191 #ifdef DOXYGEN
192  template<typename R, typename Func, typename ArgTrack>
193  qi::Future<R> async(const Func& f, const ArgTrack& toTrack, ...);
194 #else
195 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
196  template<typename R, typename AF, typename ARG0 comma ATYPEDECL> \
197  inline typename boost::disable_if< \
198  boost::mpl::or_< \
199  boost::is_same<ARG0, std::string>, boost::is_same<ARG0, char*>, \
200  boost::is_same<ARG0, const char*>, boost::is_array<ARG0> >, \
201  Future<R> >::type async(const AF& fun, const ARG0& arg0 comma ADECL, \
202  qi::Duration delay = qi::Duration(0)) \
203  { \
204  return detail::asyncMaybeActor<R, ARG0>( \
205  arg0, qi::bind<R()>(fun, arg0 comma AUSE), delay); \
206  } \
207  template<typename R, typename AF, typename ARG0 comma ATYPEDECL> \
208  inline Future<R> async(const AF& fun, const ARG0& arg0 comma ADECL, \
209  qi::SteadyClockTimePoint timepoint) \
210  { \
211  return detail::asyncMaybeActor<R, ARG0>( \
212  arg0, qi::bind<R()>(fun, arg0 comma AUSE), timepoint); \
213  }
214  QI_GEN(genCall)
215 #undef genCall
216 #endif
217 
222  QI_API void startEventLoop(int nthread);
223 
228  QI_API boost::asio::io_service& getIoService();
229 
230  namespace detail {
231  /* when throw this thread will stop a thread of the eventloop
232  */
234  };
235  };
236 }
237 
238 # ifdef _MSC_VER
239 # pragma warning( pop )
240 # endif
241 
242 # include <qi/detail/eventloop.hxx>
243 #endif // _QI_EVENTLOOP_HPP_
#define QI_API
Definition: api.hpp:34
Class to handle eventloop. <includename>qi/eventloop.hpp</includename> .
Definition: eventloop.hpp:41
boost::chrono::duration< int64_t, boost::nano > Duration
Convenience typedefs.
Definition: clock.hpp:18
dll import/export and compiler message
EventLoopPrivate * _p
Definition: eventloop.hpp:138
void startEventLoop(int nthread)
Start the eventloop with nthread threads. No-op if already started.
Future< R > async(boost::function< R()> callback, uint64_t usDelay=0)
Definition: eventloop.hpp:176
boost::asio::io_service & getIoService()
Get the io_service used by the global event loop.
boost::enable_if< boost::is_base_of< Actor, typename detail::Unwrap< ARG0 >::type >, Future< R > >::type asyncMaybeActor(const ARG0 &arg0, const boost::function< R()> &cb, qi::Duration delay)
Definition: eventloop.hxx:34
void post(const boost::function< void()> &callback)
post a callback to be executed as soon as possible
Definition: eventloop.hpp:123
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: future_fwd.hpp:337
EventLoop * getEventLoop()
Return the global eventloop, created on demand on first call.
uint64_t uint64_t
Cross-platform unsigned integer of length 64 bits (8 bytes).
Definition: types.hpp:81
std::string _name
Definition: eventloop.hpp:139
Future< R > async(const boost::function< R()> &callback, uint64_t usDelay=0)
Call given function once after given delay in microseconds.
Definition: eventloop.hxx:21
boost::enable_if_c<!boost::is_same< R, void >::value, qi::Future< R > >::type async(const boost::function< R()> &callback, qi::SteadyClockTimePoint tp)
Definition: eventloop.hpp:108
SteadyClock::time_point SteadyClockTimePoint
Steady clock time point.
Definition: clock.hpp:203
boost::enable_if_c<!boost::is_same< R, void >::value, qi::Future< R > >::type async(const boost::function< R()> &callback, qi::Duration delay)
Definition: eventloop.hpp:101
#define QI_GEN(f)
Definition: preproc.hpp:476