libqi-api  2.4.3.28
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
periodictask.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 * Copyright (c) Aldebaran Robotics 2013 All Rights Reserved
4 */
5 #ifndef _QI_PERIODICTASK_HPP_
6 # define _QI_PERIODICTASK_HPP_
7 
8 # include <string>
9 
10 # include <boost/function.hpp>
11 # include <boost/utility.hpp>
12 
13 # include <qi/atomic.hpp>
14 # include <qi/future.hpp>
15 # include <qi/stats.hpp>
16 # include <qi/trackable.hpp>
17 # include <qi/actor.hpp>
18 # include <qi/clock.hpp>
19 
20 namespace qi
21 {
22  struct PeriodicTaskPrivate;
27  class QI_API PeriodicTask: public boost::noncopyable
28  {
29  public:
31  typedef boost::function<void()> Callback;
32  // internal
33  typedef boost::function<qi::Future<void>(
35 
37  PeriodicTask();
38 
40  ~PeriodicTask();
41 
43 
48  void setCallback(const Callback& cb);
49 #ifdef DOXYGEN
50  template <typename T, typename ARG0> PeriodicTask& setCallback(const T& callable, ARG0 tracked, ...);
51 #else
52 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
53  template <typename AF, typename ARG0 comma ATYPEDECL> \
54  inline void setCallback(const AF& fun, const ARG0& arg0 comma ADECL) \
55  { \
56  setCallback(boost::bind(fun, arg0 comma AUSE)); \
57  _connectMaybeActor<ARG0>(arg0); \
58  }
60 #undef genCall
61 #endif
62 
69  void setStrand(qi::Strand* strand);
70 
74  void setUsPeriod(qi::int64_t usPeriod);
75 
89  void setPeriod(qi::Duration period);
90 
99  void start(bool immediate = true);
100 
107  void trigger();
108 
116  void stop();
117 
122  void asyncStop();
123 
128  void compensateCallbackTime(bool compensate);
129 
132  void setName(const std::string& name);
133 
135  bool isRunning() const;
136 
143  bool isStopping() const;
144 
145  private:
146  boost::shared_ptr<PeriodicTaskPrivate> _p;
147 
148  template <typename ARG0>
149  inline typename boost::enable_if<
150  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
151  void>::type
152  _connectMaybeActor(const ARG0& arg0)
153  {
154  setStrand(detail::Unwrap<ARG0>::unwrap(arg0)->strand());
155  }
156  template <typename ARG0>
157  inline typename boost::disable_if<
158  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
159  void>::type
160  _connectMaybeActor(const ARG0& arg0)
161  {
162  setStrand(0);
163  }
164  };
165 
166 }
167 #endif
#define QI_API
Definition: api.hpp:34
boost::chrono::duration< int64_t, boost::nano > Duration
Convenience typedefs.
Definition: clock.hpp:18
Control a task executed periodically and asynchronously. <includename>qi/periodictask.hpp</includename> .
boost::function< void()> Callback
Callback is a boost::function.
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: future_fwd.hpp:337
boost::function< qi::Future< void > const Callback &, qi::Duration delay)> ScheduleCallback
int64_t int64_t
Cross-platform signed integer of length 64 bits (8 bytes).
Definition: types.hpp:76
#define QI_GEN(f)
Definition: preproc.hpp:476