libqi-api  2.1.4.13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventloop.hxx
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2013 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6 
7 #ifndef _QI_DETAILS_EVENTLOOP_HXX_
8 #define _QI_DETAILS_EVENTLOOP_HXX_
9 
10 #include <qi/future.hpp>
11 
12 namespace qi
13 {
14 
15  namespace detail
16  {
17  template <typename T>
18  class DelayedPromise: public Promise<T>
19  {
20  public:
21  DelayedPromise() {}
22  void setup(boost::function<void (qi::Promise<T>)> cancelCallback, FutureCallbackType async = FutureCallbackType_Async)
23  {
24  Promise<T>::setup(cancelCallback, async);
25  }
26  };
27 
28  template<typename R> void call_and_set(qi::Promise<R> p, boost::function<R()> f)
29  {
30  try
31  {
32  p.setValue(f());
33  }
34  catch (const std::exception& e)
35  {
36  p.setError(e.what());
37  }
38  catch(...)
39  {
40  p.setError("unknown exception");
41  }
42  }
43  template<typename R> void check_canceled(qi::Future<void> f, qi::Promise<R> p)
44  {
45  if (f.wait() == FutureState_Canceled)
46  p.setCanceled();
47  // Nothing to do for other states.
48  }
49  }
50  template<typename R> void nullConverter(void*, R&) {}
51  template<typename R> Future<R> EventLoop::async(boost::function<R()> callback, uint64_t usDelay)
52  {
53  detail::DelayedPromise<R> promise;
54  qi::Future<void> f = async((boost::function<void()>)boost::bind(detail::call_and_set<R>, promise, callback), usDelay);
55  promise.setup(boost::bind(&detail::futureCancelAdapter<void>,
56  boost::weak_ptr<detail::FutureBaseTyped<void> >(f.impl())), FutureCallbackType_Sync);
57  f.connect(boost::bind(&detail::check_canceled<R>,_1, promise));
58  return promise.future();
59  }
60 
61 }
62 
63 
64 #endif
uint64_t uint64_t
Cross-platform unsigned integer of length 64 bits (8 bytes).
Definition: types.hpp:38
void setCanceled()
Definition: future.hpp:483
FutureState wait(int msecs=FutureTimeout_Infinite) const
Definition: future.hpp:188
Operation pending.
Definition: future.hpp:64
void call_and_set(qi::Promise< R > p, boost::function< R()> f)
Definition: eventloop.hxx:28
void connect(const AF &fun, FutureCallbackType type=FutureCallbackType_Async)
Definition: future.hpp:271
FutureCallbackType
Definition: future.hpp:69
void check_canceled(qi::Future< void > f, qi::Promise< R > p)
Definition: eventloop.hxx:43
boost::shared_ptr< detail::FutureBaseTyped< T > > impl()
Definition: future.hpp:302
void setError(const std::string &msg)
Definition: future.hpp:476
void nullConverter(void *, R &)
Definition: eventloop.hxx:50
boost::function< RF > bind(const AF &fun,...)
void setValue(const ValueType &value)
Definition: future.hpp:469
void setup(boost::function< void(qi::Promise< T >)> cancelCallback, FutureCallbackType async=FutureCallbackType_Async)
Definition: future.hpp:503
Future< R > async(boost::function< R()> callback, uint64_t usDelay=0)
Definition: eventloop.hxx:51