1 #ifndef DASH__FUTURE_H__INCLUDED 2 #define DASH__FUTURE_H__INCLUDED 9 #include <dash/Exception.h> 10 #include <dash/internal/Logging.h> 19 template<
typename ResultT>
44 test_func_t _test_func;
46 destroy_func_t _destroy_func;
54 template<
typename ResultT_>
55 friend std::ostream & operator<<(
66 Future() noexcept(std::is_nothrow_constructible<ResultT>::value) =
default;
71 Future(ResultT result) noexcept(std::is_nothrow_move_constructible<ResultT>::value)
72 : _value(
std::move(result)),
82 : _get_func(std::move(get_func))
94 Future(get_func_t get_func, test_func_t test_func) noexcept
95 : _get_func(std::move(get_func))
96 , _test_func(std::move(test_func))
111 get_func_t get_func, test_func_t test_func, destroy_func_t destroy_func) noexcept
112 : _get_func(std::move(get_func))
113 , _test_func(std::move(test_func))
114 , _destroy_func(std::move(destroy_func))
121 Future(
const self_t& other) =
delete;
127 std::is_nothrow_move_assignable<Future>::value)
129 *
this = std::move(other);
145 self_t &
operator=(
const self_t& other) =
delete;
151 std::is_nothrow_move_assignable<ResultT>::value)
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;
167 DASH_LOG_TRACE_VAR(
"Future.wait()", _ready);
172 DASH_LOG_ERROR(
"Future.wait()",
"No function");
175 "Future not initialized with function");
177 _value = _get_func();
179 DASH_LOG_TRACE_VAR(
"Future.wait >", _ready);
193 _ready = _test_func(&_value);
194 }
else if (_get_func) {
195 _value = _get_func();
200 "Future not initialized with function");
211 DASH_LOG_TRACE_VAR(
"Future.get()", _ready);
213 DASH_LOG_TRACE_VAR(
"Future.get >", _value);
224 return (_ready || _get_func !=
nullptr);
257 get_func_t _get_func;
259 test_func_t _test_func;
261 destroy_func_t _destroy_func;
267 template<
typename ResultT_>
268 friend std::ostream & operator<<(
281 #if defined(__clang__) && (__clang_major__ < 6) 293 : _get_func(std::move(get_func))
304 Future(get_func_t get_func, test_func_t test_func) noexcept
305 : _get_func(std::move(get_func))
306 , _test_func(std::move(test_func))
321 test_func_t test_func,
322 destroy_func_t destroy_func) noexcept
323 : _get_func(std::move(get_func)),
324 _test_func(std::move(test_func)),
325 _destroy_func(std::move(destroy_func))
331 Future(
const self_t& other) =
delete;
338 *
this = std::move(other);
354 self_t&
operator=(
const self_t& other) =
delete;
361 _get_func = std::move(other._get_func);
362 _test_func = std::move(other._test_func);
363 _destroy_func = std::move(other._destroy_func);
364 _ready = other._ready;
374 DASH_LOG_TRACE_VAR(
"Future.wait()", _ready);
379 DASH_LOG_ERROR(
"Future.wait()",
"No function");
382 "Future not initialized with function");
386 DASH_LOG_TRACE_VAR(
"Future.wait >", _ready);
400 _ready = _test_func();
401 }
else if (_get_func) {
407 "Future not initialized with function");
418 DASH_LOG_TRACE_VAR(
"Future.get()", _ready);
420 DASH_LOG_TRACE(
"Future.get >");
429 return (_get_func !=
nullptr);
434 template<
typename ResultT>
435 std::ostream & operator<<(
439 std::ostringstream ss;
440 ss <<
"dash::Future<" <<
typeid(ResultT).name() <<
">(";
447 return operator<<(os, ss.str());
452 #endif // DASH__FUTURE_H__INCLUDED void wait()
Wait for the value to become available.
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 result val...
std::function< void(void)> destroy_func_t
Callback function called upon destruction.
This class is a simple memory pool which holds allocates elements of size ValueType.
Future(self_t &&other) noexcept(std::is_nothrow_move_assignable< Future >::value)
Move constructor.
Implementation of a future used to wait for an operation to complete and access the value returned by...
Future(get_func_t get_func) noexcept
Create a future using a function that returns the value.
Future(self_t &&other) noexcept
Move constructor.
std::function< bool(ResultT *)> test_func_t
Callback function to test for the availability of the result value.
std::function< void(void)> destroy_func_t
Callback function called upon destruction.
void wait()
Wait for the value to become available.
self_t & operator=(self_t &&other) noexcept(std::is_nothrow_move_assignable< ResultT >::value)
Move assignment.
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 retu...
Future(get_func_t get_func, test_func_t test_func, destroy_func_t destroy_func) noexcept
Create a future using a function to wait for completion and a function to test for completion...
Overload of dash::Future for operations returning void.
self_t & operator=(self_t &&other) noexcept
Move assignment.
Future(get_func_t get_func) noexcept
Create a future using a function that returns the value.
Future(ResultT result) noexcept(std::is_nothrow_move_constructible< ResultT >::value)
Create a future from an already available value.
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 retu...
Future() noexcept(std::is_nothrow_constructible< ResultT >::value)=default
Default constructor, creates a future that invalid.
std::function< void(void)> get_func_t
Callback function to wait for completion.
bool test()
Test whether the operation has completed.
bool valid() noexcept
Check whether the future is valid, i.e., a function to wait for completion has been provided...
bool valid() noexcept
Check whether the future is valid, i.e., whether either a value or a function to access the valid has...
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
bool test()
Test whether the value is available.
self_t & operator=(const self_t &other)=delete
Copy assignment is not permited.
std::function< bool(void)> test_func_t
Callback function to test for completion.
std::function< ResultT(void)> get_func_t
Callback function returning the result value.