libqi-api  release-2.5.3-2016-11-18
 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  using Callback = boost::function<void()>;
32  // internal
33  using ScheduleCallback = boost::function<qi::Future<void>(const Callback&, qi::Duration delay)>;
34 
36  PeriodicTask();
37 
39  ~PeriodicTask();
40 
42 
47  template <typename T>
48  auto setCallback(T&& cb) -> typename std::enable_if<detail::IsAsyncBind<typename std::decay<T>::type>::value>::type
49  {
50  static_assert(sizeof(T) && false,
51  "Don't use PeriodicTask::setCallback(qi::bind(...)) but setCallback(...) directly");
52  }
53  template <typename T>
54  auto setCallback(T&& cb) -> typename std::enable_if<!detail::IsAsyncBind<typename std::decay<T>::type>::value>::type
55  {
56  _setCallback(std::forward<T>(cb));
57  }
58  template <typename AF, typename ARG0, typename... ARGS>
59  inline void setCallback(AF&& callable, ARG0&& arg0, ARGS&&... args)
60  {
61  _connectMaybeActor(arg0);
62  _setCallback(boost::bind(std::forward<AF>(callable), std::forward<ARG0>(arg0), std::forward<ARGS>(args)...));
63  }
64 
71  void setStrand(qi::Strand* strand);
72 
76  void setUsPeriod(qi::int64_t usPeriod);
77 
86  void setPeriod(qi::Duration period);
87 
96  void start(bool immediate = true);
97 
104  void trigger();
105 
113  void stop();
114 
119  void asyncStop();
120 
125  void compensateCallbackTime(bool compensate);
126 
129  void setName(const std::string& name);
130 
132  bool isRunning() const;
133 
140  bool isStopping() const;
141 
142  private:
143  boost::shared_ptr<PeriodicTaskPrivate> _p;
144 
145  template <typename ARG0>
146  inline typename boost::enable_if<
147  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
148  void>::type
149  _connectMaybeActor(const ARG0& arg0)
150  {
151  setStrand(detail::Unwrap<ARG0>::unwrap(arg0)->strand());
152  }
153  template <typename ARG0>
154  inline typename boost::disable_if<
155  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
156  void>::type
157  _connectMaybeActor(const ARG0& arg0)
158  {
159  setStrand(0);
160  }
161 
162  void _setCallback(const Callback& cb);
163  };
164 
165 }
166 #endif
int64_t int64_t
Definition: types.hpp:61
#define QI_API
Definition: api.hpp:33
boost::function< qi::Future< void >(const Callback &, qi::Duration delay)> ScheduleCallback
Control a task executed periodically and asynchronously. <includename>qi/periodictask.hpp</includename> .
NanoSeconds Duration
Definition: clock.hpp:32
void setCallback(AF &&callable, ARG0 &&arg0, ARGS &&...args)
auto setCallback(T &&cb) -> typename std::enable_if<!detail::IsAsyncBind< typename std::decay< T >::type >::value >::type
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bind(AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:327
auto setCallback(T &&cb) -> typename std::enable_if< detail::IsAsyncBind< typename std::decay< T >::type >::value >::type
boost::function< void()> Callback
Callback is a boost::function.