libqi-api  2.4.3.28
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
executioncontext.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_EXECUTION_CONTEXT_HPP_
8 #define _QI_EXECUTION_CONTEXT_HPP_
9 
10 #include <boost/function.hpp>
11 #include <qi/clock.hpp>
12 #include <qi/api.hpp>
13 
14 namespace qi
15 {
16 
17 template <typename T>
18 class Future;
19 
21 {
22 public:
23  virtual ~ExecutionContext() {}
24 
26  virtual void post(const boost::function<void()>& callback) = 0;
28  virtual qi::Future<void> async(const boost::function<void()>& callback,
31  virtual qi::Future<void> async(const boost::function<void()>& callback,
32  qi::Duration delay = qi::Duration(0)) = 0;
34  template <typename R>
35  typename boost::disable_if<boost::is_same<R, void>,
36  qi::Future<R> >::type
37  async(const boost::function<R()>& callback,
38  qi::Duration delay = qi::Duration(0));
40  template <typename R>
41  typename boost::disable_if<boost::is_same<R, void>,
42  qi::Future<R> >::type
43  async(const boost::function<R()>& callback, qi::SteadyClockTimePoint tp);
44 
46  virtual bool isInThisContext() = 0;
47 };
48 
49 }
50 
51 #include <qi/detail/future_fwd.hpp>
52 
53 namespace qi
54 {
55 
56 namespace detail
57 {
58 
59 template <typename T>
60 class DelayedPromise: public Promise<T>
61 {
62 public:
64  void setup(boost::function<void (qi::Promise<T>)> cancelCallback, FutureCallbackType async = FutureCallbackType_Async)
65  {
66  Promise<T>::setup(cancelCallback, async);
67  }
68 };
69 
70 template<typename R> void call_and_set(qi::Promise<R> p, boost::function<R()> f)
71 {
72  try
73  {
74  p.setValue(f());
75  }
76  catch (const std::exception& e)
77  {
78  p.setError(e.what());
79  }
80  catch(...)
81  {
82  p.setError("unknown exception");
83  }
84 }
85 template<typename R> void check_canceled(qi::Future<void> f, qi::Promise<R> p)
86 {
87  if (f.wait() == FutureState_Canceled)
88  p.setCanceled();
89  // Nothing to do for other states.
90 }
91 
92 }
93 
94 template <typename R>
95 typename boost::disable_if<boost::is_same<R, void>,
96  qi::Future<R> >::type
97  ExecutionContext::async(const boost::function<R()>& callback,
98  qi::Duration delay)
99 {
101  qi::Future<void> f = async(boost::function<void()>(boost::bind(
102  detail::call_and_set<R>, promise, callback)),
103  delay);
104  promise.setup(
105  boost::bind(&detail::futureCancelAdapter<void>,
106  boost::weak_ptr<detail::FutureBaseTyped<void> >(f.impl())),
108  f.connect(boost::bind(&detail::check_canceled<R>, _1, promise));
109  return promise.future();
110 }
111 
112 template <typename R>
113 typename boost::disable_if<boost::is_same<R, void>,
114  qi::Future<R> >::type
115  ExecutionContext::async(const boost::function<R()>& callback,
117 {
119  qi::Future<void> f = async(boost::function<void()>(boost::bind(
120  detail::call_and_set<R>, promise, callback)),
121  tp);
122  promise.setup(
123  boost::bind(&detail::futureCancelAdapter<void>,
124  boost::weak_ptr<detail::FutureBaseTyped<void> >(f.impl())),
126  f.connect(boost::bind(&detail::check_canceled<R>, _1, promise));
127  return promise.future();
128 }
129 
130 }
131 
132 #endif
void setup(boost::function< void(qi::Promise< T >)> cancelCallback, FutureCallbackType async=FutureCallbackType_Async)
The future has been canceled.
Definition: future_fwd.hpp:78
#define QI_API
Definition: api.hpp:34
boost::function< RF > bind(const AF &fun,...)
boost::chrono::duration< int64_t, boost::nano > Duration
Convenience typedefs.
Definition: clock.hpp:18
void setup(boost::function< void(qi::Promise< T >)> cancelCallback, FutureCallbackType async=FutureCallbackType_Async)
Definition: future_fwd.hpp:744
virtual qi::Future< void > async(const boost::function< void()> &callback, qi::SteadyClockTimePoint tp)=0
call a callback asynchronously to be executed on tp
void setError(const std::string &msg)
Definition: future_fwd.hpp:689
dll import/export and compiler message
void connect(const AF &fun, FutureCallbackType type=FutureCallbackType_Async)
Definition: future_fwd.hpp:376
Future< T > future() const
Get a future linked to this promise. Can be called multiple times.
Definition: future_fwd.hpp:708
void check_canceled(qi::Future< void > f, qi::Promise< R > p)
Future< R > async(boost::function< R()> callback, uint64_t usDelay=0)
Definition: eventloop.hpp:176
void call_and_set(qi::Promise< R > p, boost::function< R()> f)
void setCanceled()
Definition: future_fwd.hpp:696
FutureState wait(int msecs=FutureTimeout_Infinite) const
Definition: future_fwd.hpp:212
FutureCallbackType
Definition: future_fwd.hpp:83
void setValue(const ValueType &value)
Definition: future_fwd.hpp:682
SteadyClock::time_point SteadyClockTimePoint
Steady clock time point.
Definition: clock.hpp:203
boost::shared_ptr< detail::FutureBaseTyped< T > > impl()
Definition: future_fwd.hpp:419