Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
future.h
1 
2 /* Copyright (c) 2013-2014, 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 
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 template<> class Future< void >
125 {
126 private:
127  typedef void (Future< void >::*bool_t)() const;
128  void bool_true() const {}
129 
130 public:
132 
134  explicit Future( Impl impl ) : impl_( impl ){}
135 
138 
146  void wait( const uint32_t timeout_ = LB_TIMEOUT_INDEFINITE )
147  {
148  impl_->wait( timeout_ );
149  }
150 
154  bool isReady() const { return impl_->isReady(); }
155 
156 protected:
157  Impl impl_;
158 };
159 
160 }
161 #endif //LUNCHBOX_FUTURE_H
RefPtr< FutureImpl< void > > Impl
The wait implementation.
Definition: future.h:131
virtual bool isReady() const =0
~Future()
Destruct the future.
Definition: future.h:73
~Future()
Destruct the future.
Definition: future.h:137
void wait(const uint32_t timeout_=LB_TIMEOUT_INDEFINITE)
Wait for the promise to be fullfilled.
Definition: future.h:146
bool operator!=(const T &rhs)
Definition: future.h:104
bool isReady() const
Definition: future.h:90
#define LB_TIMEOUT_INDEFINITE
Constant defining 'wait forever' in methods with wait parameters.
Definition: types.h:106
Future template specialization for void.
Definition: future.h:124
Future(Impl impl)
Construct a new future.
Definition: future.h:134
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
bool operator>(const T &rhs)
Definition: future.h:110
RefPtr< FutureImpl< T > > Impl
The wait implementation.
Definition: future.h:67
virtual T wait(const uint32_t timeout=LB_TIMEOUT_INDEFINITE)=0
Wait for the promise to be fullfilled.
bool operator==(const T &rhs)
Definition: future.h:101
bool operator<=(const T &rhs)
Definition: future.h:113
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
bool isReady() const
Definition: future.h:154
A smart reference pointer, aka boost::intrusive_ptr.
Definition: refPtr.h:39
bool operator!()
Definition: future.h:98
bool operator<(const T &rhs)
Definition: future.h:107
bool operator>=(const T &rhs)
Definition: future.h:116