Lunchbox  1.17.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 <boost/noncopyable.hpp> // base class
22 #include <lunchbox/refPtr.h> // used inline
23 #include <lunchbox/referenced.h> // base class
24 #include <stdexcept>
25 
26 namespace lunchbox
27 {
28 class FutureTimeout : public std::runtime_error
29 {
30 public:
32  : std::runtime_error("")
33  {
34  }
35 };
36 
38 template <class T>
39 class FutureImpl : public Referenced, public boost::noncopyable
40 {
41 public:
43  virtual ~FutureImpl() {}
52  virtual T wait(const uint32_t timeout = LB_TIMEOUT_INDEFINITE) = 0;
53 
57  virtual bool isReady() const = 0;
58 };
59 
61 template <class T>
62 class Future
63 {
64 private:
65  typedef void (Future<T>::*bool_t)() const;
66  void bool_true() const {}
67 public:
69 
71  explicit Future(Impl impl)
72  : impl_(impl)
73  {
74  }
75 
77  ~Future() {}
85  T wait(const uint32_t timeout_ = LB_TIMEOUT_INDEFINITE)
86  {
87  return impl_->wait(timeout_);
88  }
89 
93  bool isReady() const { return impl_->isReady(); }
97  operator bool_t() { return wait() ? &Future<T>::bool_true : 0; }
99  bool operator!() { return !wait(); }
101  bool operator==(const T& rhs) { return wait() == rhs; }
103  bool operator!=(const T& rhs) { return wait() != rhs; }
105  bool operator<(const T& rhs) { return wait() < rhs; }
107  bool operator>(const T& rhs) { return wait() > rhs; }
109  bool operator<=(const T& rhs) { return wait() <= rhs; }
111  bool operator>=(const T& rhs) { return wait() >= rhs; }
113 
114 protected:
115  Impl impl_;
116 };
117 
119 
121 template <>
122 class Future<void>
123 {
124 private:
125  typedef void (Future<void>::*bool_t)() const;
126  void bool_true() const {}
127 public:
129 
131  explicit Future(Impl impl)
132  : impl_(impl)
133  {
134  }
135 
137  ~Future() {}
145  void wait(const uint32_t timeout_ = LB_TIMEOUT_INDEFINITE)
146  {
147  impl_->wait(timeout_);
148  }
149 
153  bool isReady() const { return impl_->isReady(); }
154 protected:
155  Impl impl_;
156 };
157 }
158 #endif // LUNCHBOX_FUTURE_H
~Future()
Destruct the future.
Definition: future.h:77
~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:145
bool operator!=(const T &rhs)
Definition: future.h:103
bool isReady() const
Definition: future.h:93
#define LB_TIMEOUT_INDEFINITE
Constant defining &#39;wait forever&#39; in methods with wait parameters.
Definition: types.h:80
RefPtr< FutureImpl< T > > Impl
The wait implementation.
Definition: future.h:68
Future template specialization for void.
Definition: future.h:122
Future(Impl impl)
Construct a new future.
Definition: future.h:131
RefPtr< FutureImpl< void > > Impl
The wait implementation.
Definition: future.h:128
virtual ~FutureImpl()
Destruct the future.
Definition: future.h:43
T wait(const uint32_t timeout_=LB_TIMEOUT_INDEFINITE)
Wait for the promise to be fullfilled.
Definition: future.h:85
bool operator>(const T &rhs)
Definition: future.h:107
bool operator==(const T &rhs)
Definition: future.h:101
bool operator<=(const T &rhs)
Definition: future.h:109
Base class to implement the wait method fulfilling the future.
Definition: future.h:39
A future represents a asynchronous operation.
Definition: future.h:62
Base class for referenced objects.
Definition: referenced.h:45
Future(Impl impl)
Construct a new future.
Definition: future.h:71
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
bool isReady() const
Definition: future.h:153
A smart reference pointer, aka boost::intrusive_ptr.
Definition: refPtr.h:40
bool operator!()
Definition: future.h:99
bool operator<(const T &rhs)
Definition: future.h:105
Future< bool > f_bool_t
A boolean future.
Definition: future.h:118
bool operator>=(const T &rhs)
Definition: future.h:111