DASH  0.3.0
dash::Future< ResultT > Class Template Reference

Implementation of a future used to wait for an operation to complete and access the value returned by that operation. More...

#include <Future.h>

Public Types

typedef Future< ResultT > self_t
 
typedef std::function< ResultT(void)> get_func_t
 Callback function returning the result value. More...
 
typedef std::function< bool(ResultT *)> test_func_t
 Callback function to test for the availability of the result value. More...
 
typedef std::function< void(void)> destroy_func_t
 Callback function called upon destruction. More...
 

Public Member Functions

 Future () noexcept(std::is_nothrow_constructible< ResultT >::value)=default
 Default constructor, creates a future that invalid. More...
 
 Future (ResultT result) noexcept(std::is_nothrow_move_constructible< ResultT >::value)
 Create a future from an already available value. More...
 
 Future (get_func_t get_func) noexcept
 Create a future using a function that returns the value. More...
 
 Future (get_func_t get_func, test_func_t test_func) noexcept
 Create a future using a function that returns the value and a function to test whether the value returned by get_func is ready. More...
 
 Future (get_func_t get_func, test_func_t test_func, destroy_func_t destroy_func) noexcept
 Create a future using a function that returns the value and a function to test whether the value returned by get_func is ready, plus a function to be called upon destruction of the future. More...
 
 Future (const self_t &other)=delete
 Copy construction is not permited. More...
 
 Future (self_t &&other) noexcept(std::is_nothrow_move_assignable< Future >::value)
 Move constructor. More...
 
 ~Future ()
 Destructor. More...
 
self_toperator= (const self_t &other)=delete
 Copy assignment is not permited. More...
 
self_toperator= (self_t &&other) noexcept(std::is_nothrow_move_assignable< ResultT >::value)
 Move assignment. More...
 
void wait ()
 Wait for the value to become available. More...
 
bool test ()
 Test whether the value is available. More...
 
ResultT get ()
 Return the value after making sure that the value is available. More...
 
bool valid () noexcept
 Check whether the future is valid, i.e., whether either a value or a function to access the valid has been provided. More...
 

Friends

template<typename ResultT_ >
std::ostream & operator<< (std::ostream &os, const Future< ResultT_ > &future)
 

Detailed Description

template<typename ResultT>
class dash::Future< ResultT >

Implementation of a future used to wait for an operation to complete and access the value returned by that operation.

Definition at line 20 of file Future.h.

Member Typedef Documentation

◆ destroy_func_t

template<typename ResultT>
typedef std::function<void (void)> dash::Future< ResultT >::destroy_func_t

Callback function called upon destruction.

Definition at line 38 of file Future.h.

◆ get_func_t

template<typename ResultT>
typedef std::function<ResultT (void)> dash::Future< ResultT >::get_func_t

Callback function returning the result value.

Definition at line 28 of file Future.h.

◆ test_func_t

template<typename ResultT>
typedef std::function<bool (ResultT*)> dash::Future< ResultT >::test_func_t

Callback function to test for the availability of the result value.

Definition at line 33 of file Future.h.

Constructor & Destructor Documentation

◆ Future() [1/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( ) const
defaultnoexcept

Default constructor, creates a future that invalid.

See also
dash::Future::valid

Referenced by dash::Future< ResultT >::Future(), and dash::Future< void >::Future().

◆ Future() [2/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( ResultT  result)
inlinenoexcept

Create a future from an already available value.

Definition at line 71 of file Future.h.

72  : _value(std::move(result)),
73  _ready(true)
74  { }

◆ Future() [3/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( get_func_t  get_func)
inlinenoexcept

Create a future using a function that returns the value.

Parameters
get_funcFunction returning the result value.

Definition at line 81 of file Future.h.

82  : _get_func(std::move(get_func))
83  {
84  }

◆ Future() [4/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( get_func_t  get_func,
test_func_t  test_func 
)
inlinenoexcept

Create a future using a function that returns the value and a function to test whether the value returned by get_func is ready.

Parameters
get_funcFunction returning the result value.
test_funcFunction returning true and assigning the result value to the pointer passed to it if the value is available.

Definition at line 94 of file Future.h.

95  : _get_func(std::move(get_func))
96  , _test_func(std::move(test_func))
97  {
98  }

◆ Future() [5/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( get_func_t  get_func,
test_func_t  test_func,
destroy_func_t  destroy_func 
)
inlinenoexcept

Create a future using a function that returns the value and a function to test whether the value returned by get_func is ready, plus a function to be called upon destruction of the future.

Parameters
get_funcFunction returning the result value.
test_funcFunction returning true and assigning the result value to the pointer passed to it if the value is available.
destroy_funcFunction called upon destruction of the future.

Definition at line 110 of file Future.h.

References dash::Future< ResultT >::Future().

112  : _get_func(std::move(get_func))
113  , _test_func(std::move(test_func))
114  , _destroy_func(std::move(destroy_func))
115  {
116  }

◆ Future() [6/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( const self_t other)
delete

Copy construction is not permited.

◆ Future() [7/7]

template<typename ResultT>
dash::Future< ResultT >::Future ( self_t &&  other)
inlinenoexcept

Move constructor.

Definition at line 126 of file Future.h.

128  {
129  *this = std::move(other);
130  }

◆ ~Future()

template<typename ResultT>
dash::Future< ResultT >::~Future ( )
inline

Destructor.

Calls the destroy_func passed to the constructor, if available.

Definition at line 136 of file Future.h.

References dash::Future< ResultT >::operator=().

136  {
137  if (_destroy_func) {
138  _destroy_func();
139  }
140  }

Member Function Documentation

◆ get()

template<typename ResultT>
ResultT dash::Future< ResultT >::get ( )
inline

Return the value after making sure that the value is available.

Definition at line 209 of file Future.h.

References dash::Future< ResultT >::wait().

210  {
211  DASH_LOG_TRACE_VAR("Future.get()", _ready);
212  wait();
213  DASH_LOG_TRACE_VAR("Future.get >", _value);
214  return _value;
215  }
void wait()
Wait for the value to become available.
Definition: Future.h:165

◆ operator=() [1/2]

template<typename ResultT>
self_t& dash::Future< ResultT >::operator= ( const self_t other)
delete

Copy assignment is not permited.

Referenced by dash::Future< ResultT >::~Future(), and dash::Future< void >::~Future().

◆ operator=() [2/2]

template<typename ResultT>
self_t& dash::Future< ResultT >::operator= ( self_t &&  other)
inlinenoexcept

Move assignment.

Definition at line 150 of file Future.h.

152  {
153  _get_func = std::move(other._get_func);
154  _test_func = std::move(other._test_func);
155  _destroy_func = std::move(other._destroy_func);
156  _value = std::move(other._value);
157  _ready = other._ready;
158  return *this;
159  }

◆ test()

template<typename ResultT>
bool dash::Future< ResultT >::test ( )
inline

Test whether the value is available.

It is safe to call get after this call returned true. This function will block if no test-function has been provided.

Returns
true if the value is available, false otherwise.

Definition at line 189 of file Future.h.

190  {
191  if (!_ready) {
192  if (_test_func) {
193  _ready = _test_func(&_value);
194  } else if (_get_func) {
195  _value = _get_func();
196  _ready = true;
197  } else {
198  DASH_THROW(
200  "Future not initialized with function");
201  }
202  }
203  return _ready;
204  }

◆ valid()

template<typename ResultT>
bool dash::Future< ResultT >::valid ( )
inlinenoexcept

Check whether the future is valid, i.e., whether either a value or a function to access the valid has been provided.

Definition at line 222 of file Future.h.

223  {
224  return (_ready || _get_func != nullptr);
225  }

◆ wait()

template<typename ResultT>
void dash::Future< ResultT >::wait ( )
inline

Wait for the value to become available.

It is safe to call get after this call returned.

Definition at line 165 of file Future.h.

Referenced by dash::Future< ResultT >::get(), dash::Future< void >::get(), and dash::summa().

166  {
167  DASH_LOG_TRACE_VAR("Future.wait()", _ready);
168  if (_ready) {
169  return;
170  }
171  if (!_get_func) {
172  DASH_LOG_ERROR("Future.wait()", "No function");
173  DASH_THROW(
175  "Future not initialized with function");
176  }
177  _value = _get_func();
178  _ready = true;
179  DASH_LOG_TRACE_VAR("Future.wait >", _ready);
180  }

The documentation for this class was generated from the following file: