Lunchbox  1.15.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
future.h
1 
2 /* Copyright (c) 2013-2015, Stefan.Eilemann@epfl.ch
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_FUTURE_H
19 #define LUNCHBOX_FUTURE_H
20 
21 #include <lunchbox/refPtr.h> // used inline
22 #include <lunchbox/referenced.h> // base class
23 #include <boost/noncopyable.hpp> // base class
24 #include <stdexcept>
25 
26 namespace lunchbox
27 {
28 
29 class FutureTimeout : public std::runtime_error
30 {
31 public:
32  FutureTimeout() : std::runtime_error("") {}
33 };
34 
36 template< class T >
37 class FutureImpl : public Referenced, public boost::noncopyable
38 {
39 public:
41  virtual ~FutureImpl(){}
42 
51  virtual T wait( const uint32_t timeout = LB_TIMEOUT_INDEFINITE ) = 0;
52 
56  virtual bool isReady() const = 0;
57 };
58 
60 template< class T > class Future
61 {
62 private:
63  typedef void (Future< T >::*bool_t)() const;
64  void bool_true() const {}
65 
66 public:
68 
70  explicit Future( Impl impl ) : impl_( impl ){}
71 
73  ~Future() {}
74 
82  T wait( const uint32_t timeout_ = LB_TIMEOUT_INDEFINITE )
83  {
84  return impl_->wait( timeout_ );
85  }
86 
90  bool isReady() const { return impl_->isReady(); }
91 
95  operator bool_t() { return wait() ? &Future< T >::bool_true : 0; }
96 
98  bool operator ! () { return !wait(); }
99 
101  bool operator == ( const T& rhs ) { return wait() == rhs; }
102 
104  bool operator != ( const T& rhs ) { return wait() != rhs; }
105 
107  bool operator < ( const T& rhs ) { return wait() < rhs; }
108 
110  bool operator > ( const T& rhs ) { return wait() > rhs; }
111 
113  bool operator <= ( const T& rhs ) { return wait() <= rhs; }
114 
116  bool operator >= ( const T& rhs ) { return wait() >= rhs; }
118 
119 protected:
120  Impl impl_;
121 };
122 
124 
126 template<> class Future< void >
127 {
128 private:
129  typedef void (Future< void >::*bool_t)() const;
130  void bool_true() const {}
131 
132 public:
134 
136  explicit Future( Impl impl ) : impl_( impl ){}
137 
140 
148  void wait( const uint32_t timeout_ = LB_TIMEOUT_INDEFINITE )
149  {
150  impl_->wait( timeout_ );
151  }
152 
156  bool isReady() const { return impl_->isReady(); }
157 
158 protected:
159  Impl impl_;
160 };
161 }
162 #endif //LUNCHBOX_FUTURE_H
RefPtr< FutureImpl< void > > Impl
The wait implementation.
Definition: future.h:133
~Future()
Destruct the future.
Definition: future.h:73
~Future()
Destruct the future.
Definition: future.h:139
void wait(const uint32_t timeout_=LB_TIMEOUT_INDEFINITE)
Wait for the promise to be fullfilled.
Definition: future.h:148
bool isReady() const
Definition: future.h:90
#define LB_TIMEOUT_INDEFINITE
Constant defining &#39;wait forever&#39; in methods with wait parameters.
Definition: types.h:80
Future template specialization for void.
Definition: future.h:126
Future(Impl impl)
Construct a new future.
Definition: future.h:136
virtual ~FutureImpl()
Destruct the future.
Definition: future.h:41
T wait(const uint32_t timeout_=LB_TIMEOUT_INDEFINITE)
Wait for the promise to be fullfilled.
Definition: future.h:82
RefPtr< FutureImpl< T > > Impl
The wait implementation.
Definition: future.h:67
Base class to implement the wait method fulfilling the future.
Definition: future.h:37
A future represents a asynchronous operation.
Definition: future.h:60
Base class for referenced objects.
Definition: referenced.h:45
Future(Impl impl)
Construct a new future.
Definition: future.h:70
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
bool isReady() const
Definition: future.h:156
A smart reference pointer, aka boost::intrusive_ptr.
Definition: refPtr.h:39
Future< bool > f_bool_t
A boolean future.
Definition: future.h:123