- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
futurebarrier.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2015 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6
7 #ifndef QI_DETAIL_FUTURE_BARRIER_HXX_
8 #define QI_DETAIL_FUTURE_BARRIER_HXX_
9
10 namespace qi
11 {
12
13 namespace detail
14 {
15
16 template<typename T>
17 class FutureBarrierPrivate {
18 public:
20 FutureBarrierPrivate(FutureCallbackType async = FutureCallbackType_Async)
21 : _closed(0)
22 , _count(0)
23 , _futures()
24 , _promise(async)
25 {}
26
27 void onFutureFinish() {
28 if (--(this->_count) == 0 && this->_closed.load()) {
29 if (!_set.swap(true))
30 this->_promise.setValue(this->_futures);
31 }
32 }
33
34 void cancelAll() {
35 QI_ASSERT(_closed.load());
36 for (typename std::vector< Future<T> >::iterator it = this->_futures.begin();
37 it != this->_futures.end();
38 ++it)
39 it->cancel();
40 }
41
42 Atomic<bool> _closed;
43 Atomic<bool> _set;
44 Atomic<int> _count;
45 std::vector< Future<T> > _futures;
46 Promise< std::vector< Future<T> > > _promise;
47
48 };
49
50 }
51
163 template<typename T>
164 class FutureBarrier : boost::noncopyable {
165 public:
167 FutureBarrier(FutureCallbackType async = FutureCallbackType_Async)
168 : _p(boost::make_shared<detail::FutureBarrierPrivate<T> >(async))
169 {
170 _p->_promise.setOnCancel(qi::bindWithFallback(
171 boost::function<void()>(),
172 &detail::FutureBarrierPrivate<T>::cancelAll,
173 boost::weak_ptr<detail::FutureBarrierPrivate<T> >(_p)));
174 }
175
188 void addFuture(qi::Future<T> fut) {
189 // Can't add future from closed qi::FutureBarrier.
190 if (_p->_closed.load())
191 throw std::runtime_error("Adding future to closed barrier");
192
193 ++(_p->_count);
194 fut.connect(boost::bind<void>(&detail::FutureBarrierPrivate<T>::onFutureFinish, _p));
195 _p->_futures.push_back(fut);
196 }
197
209 Future< std::vector< Future<T> > > future() {
210 this->close();
211 return _p->_promise.future();
212 }
213
214 protected:
215 boost::shared_ptr<detail::FutureBarrierPrivate<T> > _p;
216
217 private:
218 void close() {
219 _p->_closed = true;
220 if (_p->_count.load() == 0) {
221 if (!_p->_set.swap(true))
222 _p->_promise.setValue(_p->_futures);
223 }
224 }
225 };
226
238 template <typename T>
239 qi::FutureSync<std::vector<Future<T> > > waitForAll(std::vector<Future<T> >& vect) {
240 typename std::vector< Future<T> >::iterator it;
241 qi::FutureBarrier<T> barrier;
242
243 for (it = vect.begin(); it != vect.end(); ++it) {
244 barrier.addFuture(*it);
245 }
246 return barrier.future();
247 }
248
260 template <typename T>
261 qi::FutureSync< qi::Future<T> > waitForFirst(std::vector< Future<T> >& vect) {
262 typename std::vector< Future<T> >::iterator it;
263 qi::Promise< qi::Future<T> > prom;
264 qi::Atomic<int>* count = new qi::Atomic<int>();
265 count->swap((int)vect.size());
266 for (it = vect.begin(); it != vect.end(); ++it) {
267 it->connect(boost::bind<void>(&detail::waitForFirstHelper<T>, prom, *it, count));
268 }
269 return prom.future();
270 }
271
272 }
273
274 #endif
qi::detail::FutureBarrierPrivate::_closed
Atomic< bool > _closed
Definition: futurebarrier.hpp:42
qi::FutureBarrier::FutureBarrier
FutureBarrier(FutureCallbackType async=FutureCallbackType_Async)
FutureBarrier constructor taking no argument.
Definition: futurebarrier.hpp:167
qi::Atomic::swap
T swap(T value)
Definition: atomic.hpp:210
qi::Future::connect
void connect(const AF &fun, FutureCallbackType type=FutureCallbackType_Auto)
Definition: future_fwd.hpp:545
qi::Future
Definition: executioncontext.hpp:20
qi::FutureBarrier::addFuture
void addFuture(qi::Future< T > fut)
Adds the future to the barrier.
Definition: futurebarrier.hpp:188
qi::detail::FutureBarrierPrivate::_set
Atomic< bool > _set
Definition: futurebarrier.hpp:43
qi::detail::FutureBarrierPrivate::onFutureFinish
void onFutureFinish()
Definition: futurebarrier.hpp:27
qi::FutureBarrier
This class helps waiting for multiple futures at the same point.
Definition: futurebarrier.hpp:164
qi::detail::FutureBarrierPrivate::cancelAll
void cancelAll()
Definition: futurebarrier.hpp:34
qi::bindWithFallback
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bindWithFallback(boost::function< void()> onFail, AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:291
QI_ASSERT
#define QI_ASSERT(expr__)
Definition: assert.hpp:27
qi::FutureBarrier::future
Future< std::vector< Future< T > > > future()
Gets the future result for the barrier.
Definition: futurebarrier.hpp:209
qi::detail::FutureBarrierPrivate::_count
Atomic< int > _count
Definition: futurebarrier.hpp:44
qi::waitForAll
qi::FutureSync< std::vector< Future< T > > > waitForAll(std::vector< Future< T > > &vect)
Helper function that creates a future waiting on a vector of futures.
Definition: futurebarrier.hpp:239
qi::detail::FutureBarrierPrivate::_futures
std::vector< Future< T > > _futures
Definition: futurebarrier.hpp:45
qi::Atomic< bool >
qi::Promise::future
Future< T > future() const
Get a future linked to this promise. Can be called multiple times.
Definition: future_fwd.hpp:881
qi::async
auto async(F &&callback) -> decltype(asyncDelay(std::forward< F >(callback), qi::Duration(0)))
Definition: async.hpp:53
qi::FutureCallbackType_Async
Definition: future_fwd.hpp:106
qi::FutureSync
Definition: future_fwd.hpp:70
qi::detail::FutureBarrierPrivate::_promise
Promise< std::vector< Future< T > > > _promise
Definition: futurebarrier.hpp:46
qi::FutureCallbackType
FutureCallbackType
Definition: future_fwd.hpp:104
qi::detail::FutureBarrierPrivate::FutureBarrierPrivate
FutureBarrierPrivate(FutureCallbackType async=FutureCallbackType_Async)
FutureBarrier constructor taking no argument.
Definition: futurebarrier.hpp:20
qi::FutureBarrier::_p
boost::shared_ptr< detail::FutureBarrierPrivate< T > > _p
Definition: futurebarrier.hpp:215
qi::detail::FutureBarrierPrivate
Definition: futurebarrier.hpp:17
qi::Promise
Definition: future_fwd.hpp:71
qi::waitForFirst
qi::FutureSync< qi::Future< T > > waitForFirst(std::vector< Future< T > > &vect)
Helper function to wait for the first valid future.
Definition: futurebarrier.hpp:261
qi::Atomic::load
T load() const
Definition: atomic.hpp:221