libqi-api  2.8.7.4
 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 
34  PeriodicTask();
35 
37  ~PeriodicTask();
38 
40 
45  template <typename T>
46  auto setCallback(T&& cb) -> typename std::enable_if<detail::IsAsyncBind<typename std::decay<T>::type>::value>::type
47  {
48  static_assert(sizeof(T) && false,
49  "Don't use PeriodicTask::setCallback(qi::bind(...)) but setCallback(...) directly");
50  }
51  template <typename T>
52  auto setCallback(T&& cb) -> typename std::enable_if<!detail::IsAsyncBind<typename std::decay<T>::type>::value>::type
53  {
54  _setCallback(std::forward<T>(cb));
55  }
56  template <typename AF, typename ARG0, typename... ARGS>
57  inline void setCallback(AF&& callable, ARG0&& arg0, ARGS&&... args)
58  {
59  _connectMaybeActor(arg0);
60  _setCallback(boost::bind(std::forward<AF>(callable), std::forward<ARG0>(arg0), std::forward<ARGS>(args)...));
61  }
62 
69  void setStrand(qi::Strand* strand);
70 
74  void setUsPeriod(qi::int64_t usPeriod);
75 
84  void setPeriod(qi::Duration period);
85 
94  void start(bool immediate = true);
95 
101  void trigger();
102 
110  void stop();
111 
116  void asyncStop();
117 
122  void compensateCallbackTime(bool compensate);
123 
126  void setName(const std::string& name);
127 
129  bool isRunning() const;
130 
137  bool isStopping() const;
138 
139  private:
140  boost::shared_ptr<PeriodicTaskPrivate> _p;
141 
142  template <typename ARG0>
143  inline typename boost::enable_if<
144  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
145  void>::type
146  _connectMaybeActor(const ARG0& arg0)
147  {
148  setStrand(detail::Unwrap<ARG0>::unwrap(arg0)->strand());
149  }
150  template <typename ARG0>
151  inline typename boost::disable_if<
152  boost::is_base_of<Actor, typename detail::Unwrap<ARG0>::type>,
153  void>::type
154  _connectMaybeActor(const ARG0& arg0)
155  {
156  setStrand(0);
157  }
158 
159  void _setCallback(const Callback& cb);
160  };
161 
162 }
163 #endif
int64_t int64_t
Definition: types.hpp:61
#define QI_API
Definition: api.hpp:33
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:308
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.