libqi-api  2.4.3.28
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
strand.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_STRAND_HPP_
8 #define _QI_STRAND_HPP_
9 
11 #include <boost/shared_ptr.hpp>
12 #include <boost/function.hpp>
13 #include <boost/noncopyable.hpp>
14 #include <boost/type_traits/function_traits.hpp>
15 
16 namespace qi
17 {
18 
19 class StrandPrivate;
20 
31 class QI_API Strand : public ExecutionContext, private boost::noncopyable
32 {
33 public:
35  Strand();
37  Strand(qi::ExecutionContext& executionContext);
42  ~Strand();
43 
44  void post(const boost::function<void()>& callback);
45 
46  qi::Future<void> async(const boost::function<void()>& cb,
48  qi::Future<void> async(const boost::function<void()>& cb,
49  qi::Duration delay = qi::Duration(0));
50 
51  bool isInThisContext();
52 
53 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
54  template <typename T, typename F, typename ARG0 comma ATYPEDECL> \
55  boost::function<T> schedulerFor( \
56  const F& func, const ARG0& arg0 comma ADECL, \
57  const boost::function<void()>& fallbackCb = boost::function<void()>()) \
58  { \
59  boost::function<T> funcbind = qi::bind<T>(func, arg0 comma AUSE); \
60  return qi::trackWithFallback( \
61  fallbackCb, \
62  SchedulerHelper<boost::function_traits<T>::arity, T>::_scheduler( \
63  funcbind, this), \
64  arg0); \
65  }
67 #undef genCall
68 
69 private:
70  boost::shared_ptr<StrandPrivate> _p;
71 
72  template <int N, typename T>
73  struct SchedulerHelper;
74 #define typedefi(z, n, _) \
75  typedef typename boost::function_traits<T>::BOOST_PP_CAT( \
76  BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type) BOOST_PP_CAT(P, n);
77 #define placeholders(z, n, __) , BOOST_PP_CAT(_, BOOST_PP_INC(n))
78 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
79  template <typename T> \
80  struct SchedulerHelper<n, T> \
81  { \
82  BOOST_PP_REPEAT(n, typedefi, _); \
83  typedef typename boost::function_traits<T>::result_type R; \
84  static boost::function<T> _scheduler(const boost::function<T>& f, \
85  Strand* strand) \
86  { \
87  return qi::bind<T>(&_asyncCall, strand, \
88  f BOOST_PP_REPEAT(n, placeholders, _)); \
89  } \
90  static qi::Future<R> _asyncCall(Strand* strand, \
91  const boost::function<T>& func comma \
92  ADECL) \
93  { \
94  /* use qi::bind again since first arg may be a Trackable */ \
95  return ((qi::ExecutionContext*)strand) \
96  ->async(qi::bind<R()>(func comma AUSE)); \
97  } \
98  };
100 #undef genCall
101 #undef placeholders
102 #undef typedefi
103 };
104 
105 }
106 
107 #endif // _QI_STRAND_HPP_
#define QI_API
Definition: api.hpp:34
boost::chrono::duration< int64_t, boost::nano > Duration
Convenience typedefs.
Definition: clock.hpp:18
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
Definition: strand.hpp:78
Future< R > async(boost::function< R()> callback, uint64_t usDelay=0)
Definition: eventloop.hpp:176
SteadyClock::time_point SteadyClockTimePoint
Steady clock time point.
Definition: clock.hpp:203
#define QI_GEN(f)
Definition: preproc.hpp:476