7 #ifndef _QI_FUTURE_HPP_
8 #define _QI_FUTURE_HPP_
13 #include <qi/config.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/make_shared.hpp>
18 #include <boost/function.hpp>
19 #include <boost/bind.hpp>
20 #include <boost/thread/recursive_mutex.hpp>
23 # pragma warning( push )
24 # pragma warning( disable: 4251 )
25 # pragma warning( disable: 4275 ) //std::runtime_error: no dll interface
47 template <
typename T>
class Future;
52 template <
typename T>
class FutureBaseTyped;
56 boost::weak_ptr<detail::FutureBaseTyped<FT> > wf);
99 : std::runtime_error(stateToString(es) + str)
105 std::string stateToString(
const ExceptionState &es);
111 ExceptionState _state;
128 template <
typename T>
136 :
_p(boost::make_shared<detail::FutureBaseTyped<T> >())
146 return _p.get() == other._p.get();
155 bool operator < (const Future<T>& b)
const
157 return _p.get() < b._p.get();
259 return _p->isCancelable();
270 template<
typename AF>
274 _p->connect(*
this, fun, type);
283 template<
typename FUNCTYPE,
typename ARG0>
284 void connect(FUNCTYPE fun, ARG0 tracked, ...,
287 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
288 template<typename AF, typename ARG0 comma ATYPEDECL> \
289 inline void connect(const AF& fun, const ARG0& arg0 comma ADECL, \
290 FutureCallbackType type = FutureCallbackType_Async) \
292 _p->connect(*this, ::qi::bind<void(Future<T>)>(fun, arg0 comma AUSE), \
302 boost::shared_ptr<detail::FutureBaseTyped<T> >
impl() {
return _p;}
303 Future(boost::shared_ptr<detail::FutureBaseTyped<T> > p) :
310 boost::shared_ptr< detail::FutureBaseTyped<T> >
_p;
314 template<
typename FT,
typename PT>
316 template<
typename FT,
typename PT,
typename CONV>
319 template<
typename FT>
321 boost::weak_ptr<detail::FutureBaseTyped<FT> > wf);
386 bool operator < (const FutureSync<T>& b)
const
388 return _future._p.get() < b._future._p.get();
412 template<
typename FUNCTYPE,
typename ARG0>
413 void connect(FUNCTYPE fun, ARG0 tracked, ...);
415 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
416 template<typename AF, typename ARG0 comma ATYPEDECL> \
417 inline void connect(const AF& fun, const ARG0& arg0 comma ADECL) \
420 connect(::qi::bind<void(FutureSync<T>)>(fun, arg0 comma AUSE)); \
441 template <
typename T>
452 _f._p->reportStart();
453 _f._p->_async = async;
463 setup(cancelCallback, async);
470 _f._p->setValue(
_f, value);
477 _f._p->setError(
_f, msg);
484 _f._p->setCanceled(
_f);
505 this->
_f._p->reportStart();
506 this->
_f._p->setOnCancel(cancelCallback);
507 this->
_f._p->_async = async;
510 template<
typename>
friend class ::qi::detail::FutureBaseTyped;
513 template<
typename FT,
typename PT>
515 template<
typename FT,
typename PT,
typename CONV>
538 fut.
connect(boost::bind<void>(&FutureBarrier::onFutureFinish,
this));
556 void onFutureFinish() {
564 if (*(this->
_count) == 0) {
570 template <
typename T>
574 template <
typename T>
575 void waitForAll(std::vector< Future<T> >& vect);
578 template <
typename T>
582 template<
typename FT,
typename PT>
593 template<
typename FT,
typename PT>
594 void adaptFuture(
const Future<FT>& f, Promise<PT>& p);
597 template<
typename FT,
typename PT,
typename CONV>
598 void adaptFuture(
const Future<FT>& f, Promise<PT>& p, CONV converter);
602 # pragma warning( pop )
607 #endif // _QI_FUTURE_HPP_
Future< T >::ValueType ValueType
This class helps waiting on multiple futures at the same point.
Promise(boost::function< void(qi::Promise< T >)> cancelCallback, FutureCallbackType async=FutureCallbackType_Async)
FutureSync(const Future< T > &b)
bool hasValue(int msecs=FutureTimeout_Infinite) const
const ValueType & value(int msecs=FutureTimeout_Infinite) const
Return the value associated to a Future.
void operator()(const FT &vIn, PT &vOut)
Future< T >::Connection Connection
FutureType< T >::type ValueType
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)
bool isRunning() const
isRunning
void connect(const Connection &s)
bool isFinished() const
isFinished
bool hasError(int msecs=FutureTimeout_Infinite) const
hasError
Future< T > & operator=(const Future< T > &b)
bool isCancelable() const
FutureType< T >::typecast ValueTypeCast
FutureState wait(int msecs=FutureTimeout_Infinite) const
bool operator==(const Future< T > &other)
Promise(FutureCallbackType async=FutureCallbackType_Async)
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
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.
FutureSync< T > & operator=(const Future< T > &b)
void futureCancelAdapter(boost::weak_ptr< FutureBaseTyped< FT > > wf)
bool isCancelable() const
FutureSync(const FutureSync< T > &b)
bool hasValue(int msecs=FutureTimeout_Infinite) const
hasValue
FutureBarrier(FutureCallbackType async=FutureCallbackType_Async)
FutureBarrier constructor taking no argument.
void connect(const AF &fun, FutureCallbackType type=FutureCallbackType_Async)
Future is not tied to a promise.
Specialize this struct to provide conversion between future values.
virtual ~FutureException()
FutureHasNoValue typecast
void adaptFuture(const Future< FT > &f, Promise< PT > &p)
Future< T >::ValueTypeCast ValueTypeCast
Future(const Future< T > &b)
std::vector< Future< T > > _futures
The operation is finished with an error.
virtual ~FutureUserException()
void _connect(const boost::function< void()> &s)
ExceptionState state() const
boost::function< void(Future< std::vector< qi::Future< T > > >) > Connection
const std::string & error(int msecs=FutureTimeout_Infinite) const
error
FutureSync< T > & operator=(const FutureSync< T > &b)
boost::shared_ptr< detail::FutureBaseTyped< T > > impl()
FutureState wait(int msecs=FutureTimeout_Infinite) const
dll import/export and compiler message
void setError(const std::string &msg)
bool addFuture(qi::Future< T > fut)
Adds the future to the barrier.
Promise< std::vector< Future< T > > > _promise
boost::function< RF > bind(const AF &fun,...)
Future(boost::shared_ptr< detail::FutureBaseTyped< T > > p)
bool isCanceled() const
isCanceled
bool hasError(int msecs=FutureTimeout_Infinite) const
void _connect(const boost::function< void()> &s)
const std::string & error(int msecs=FutureTimeout_Infinite) const
friend void adaptFuture(const Future< FT > &f, Promise< PT > &p)
FutureException(const ExceptionState &es, const std::string &str=std::string())
boost::shared_ptr< detail::FutureBaseTyped< T > > _p
void setValue(const ValueType &value)
void waitForAll(std::vector< Future< T > > &vect)
Helper function to wait on a vector of futures.
friend void adaptFuture(const Future< FT > &f, Promise< PT > &p)
FutureType< T >::type ValueType
void setup(boost::function< void(qi::Promise< T >)> cancelCallback, FutureCallbackType async=FutureCallbackType_Async)
const ValueType & value(int msecs=FutureTimeout_Infinite) const
FutureUserException(const std::string &str=std::string())