7 #ifndef _QI_DETAILS_FUTURE_HXX_
8 #define _QI_DETAILS_FUTURE_HXX_
12 #include <boost/bind.hpp>
21 class FutureBasePrivate;
29 bool isRunning()
const;
30 bool isFinished()
const;
31 bool isCanceled()
const;
32 bool hasError(
int msecs)
const;
33 bool hasValue(
int msecs)
const;
34 const std::string &error(
int msecs)
const;
40 void reportError(
const std::string &message);
41 void reportCanceled();
42 boost::recursive_mutex& mutex();
46 FutureBasePrivate *_p;
52 class FutureBaseTyped :
public FutureBase {
54 typedef typename FutureType<T>::type ValueType;
61 bool isCancelable()
const
72 _onCancel(Promise<T>(future));
75 void setOnCancel(boost::function<
void (Promise<T>)> onCancel)
83 for(
unsigned i = 0; i<_onResult.size(); ++i)
91 }
catch(
const std::exception& e) {
92 qiLogError(
"qi.future") <<
"Exception caught in future callback "
96 <<
"Unknown exception caught in future callback";
107 boost::recursive_mutex::scoped_lock lock(mutex());
113 callCbNotify(future);
124 boost::recursive_mutex::scoped_lock lock(mutex());
129 callCbNotify(future);
132 void setError(
qi::Future<T>& future,
const std::string &message)
134 boost::recursive_mutex::scoped_lock lock(mutex());
138 reportError(message);
139 callCbNotify(future);
143 boost::recursive_mutex::scoped_lock lock(mutex());
148 callCbNotify(future);
158 boost::recursive_mutex::scoped_lock lock(mutex());
159 _onResult.push_back(s);
160 ready = isFinished();
170 }
catch(const ::qi::PointerLockException&)
176 const ValueType &value(
int msecs)
const {
188 friend class Promise<T>;
189 typedef std::vector<boost::function<void (qi::Future<T>)> > Callbacks;
192 boost::function<void (Promise<T>)> _onCancel;
196 template <
typename T>
200 if (!prom.future().isFinished() && !fut.
hasError())
214 if (!prom.future().isFinished())
220 prom.setValue(makeFutureError<T>(
"No future returned successfully."));
230 template <
typename T>
237 template <
typename T>
239 typename std::vector< Future<T> >::iterator it;
242 for (it = vect.begin(); it != vect.end(); ++it) {
248 template <
typename T>
250 typename std::vector< Future<T> >::iterator it;
253 count->
swap((
int)vect.size());
254 for (it = vect.begin(); it != vect.end(); ++it) {
255 it->connect(boost::bind<void>(&detail::waitForFirstHelper<T>, prom, *it, count));
262 template<
typename FT,
typename PT,
typename CONV>
274 catch (
const std::exception& e)
276 p.
setError(std::string(
"futureAdapter conversion error: ") + e.what());
283 template<
typename FT>
286 if (boost::shared_ptr<FutureBaseTyped<FT> > f = wf.lock())
299 template <
typename T>
307 template <
typename T>
315 template<
typename FT,
typename PT>
320 boost::weak_ptr<detail::FutureBaseTyped<FT> >(f.
_p)));
325 template<
typename FT,
typename PT,
typename CONV>
330 boost::weak_ptr<detail::FutureBaseTyped<FT> >(f.
_p)));
331 const_cast<Future<FT>&
>(f).connect(
boost::bind(detail::futureAdapter<FT, PT, CONV>, _1, p, converter));
335 #endif // _QI_DETAILS_FUTURE_HXX_
This class helps waiting on multiple futures at the same point.
void futureAdapter(Future< FT > f, Promise< PT > p, CONV converter)
const ValueType & value(int msecs=FutureTimeout_Infinite) const
Return the value associated to a Future.
void operator()(void *in, void *out)
Future< std::vector< Future< T > > > future()
Gets the future result for the barrier.
The future has been canceled.
qi::Future< T > makeFutureError(const std::string &error, FutureCallbackType async)
void post(const boost::function< void()> &callback, uint64_t usDelay=0)
Similar to async() but without cancelation or notification.
bool hasError(int msecs=FutureTimeout_Infinite) const
hasError
bool isCancelable() const
Future< T > future() const
Get a future linked to this promise. Can be called multiple times.
qi::FutureSync< qi::Future< T > > waitForFirst(std::vector< Future< T > > &vect)
Helper function to wait for the first valid future.
void operator()(void *in, const T &out)
void futureCancelAdapter(boost::weak_ptr< FutureBaseTyped< FT > > wf)
Future is not tied to a promise.
EventLoop * getEventLoop()
Return the global eventloop, created on demand on first call.
Specialize this struct to provide conversion between future values.
void waitForFirstHelper(qi::Promise< qi::Future< T > > &prom, qi::Future< T > &fut, qi::Atomic< int > *count)
void adaptFuture(const Future< FT > &f, Promise< PT > &p)
#define qiLogError(...)
Log in error mode.
const std::string & error(int msecs=FutureTimeout_Infinite) const
error
void setError(const std::string &msg)
bool addFuture(qi::Future< T > fut)
Adds the future to the barrier.
boost::function< RF > bind(const AF &fun,...)
bool isCanceled() const
isCanceled
boost::shared_ptr< detail::FutureBaseTyped< T > > _p
void waitForAll(std::vector< Future< T > > &vect)
Helper function to wait on a vector of futures.
void operator()(const T &in, void *out)
void setup(boost::function< void(qi::Promise< T >)> cancelCallback, FutureCallbackType async=FutureCallbackType_Async)