9#if defined(__cplusplus) && __cplusplus >= 201103L
18# include <type_traits>
31using promise = ::std::promise<zth::type<T>>;
34using future = ::std::future<zth::type<T>>;
65 template <
class Rep,
class Period>
67 wait_for(std::chrono::duration<Rep, Period>
const& timeout_duration)
const
70 return m_future->
block(timeout_duration) ? std::future_status::ready
71 : std::future_status::timeout;
74 template <
class Clock,
class Duration>
76 wait_until(
const std::chrono::time_point<Clock, Duration>& timeout_time)
const
79 return m_future->
block(timeout_time) ? std::future_status::ready
80 : std::future_status::timeout;
93 zth_throw(std::future_error(std::future_errc::no_state));
113 *
this = std::move(other);
119 this->
m_future = std::move(other.m_future);
126 std::shared_future<zth::type<T>>
share()
const
128 auto f = std::shared_future<zth::type<T>>(std::move(this->
m_future));
145 : m_future{std::move(
future)}
150 *
this = std::move(other);
156 this->m_future = std::move(other.m_future);
165 std::swap(m_future, other.m_future);
173 zth_throw(std::future_error(std::future_errc::future_already_retrieved));
179 template <
typename U>
183 m_future->set(std::forward<U>(value));
195 m_future->set(std::move(exception));
202 zth_throw(std::future_error(std::future_errc::no_state));
209 if(m_future->valid())
210 zth_throw(std::future_error(std::future_errc::promise_already_satisfied));
215 bool m_got_future =
false;
226 using value_type = T;
229 future() noexcept = default;
232 future(
zth::SharedPointer<typename base::Future_type> f) noexcept
238 return base::value();
246 using value_type = T&;
249 future() noexcept = default;
252 future(
zth::SharedPointer<typename base::Future_type> f) noexcept
258 return *base::value();
266 using value_type = void;
269 future() noexcept = default;
272 future(
zth::SharedPointer<typename base::Future_type> f) noexcept
286 using value_type = T;
291 return base::value();
299 using value_type = T&;
304 return *base::value();
312 using value_type = void;
325 using value_type = T;
332 : base{std::move(future)}
339 using value_type = T&;
346 : base{std::move(future)}
359 : base{std::move(future)}
375# if __cplusplus < 201703L
376template <
typename Func,
typename... Args>
377using invoke_result =
typename std::result_of<Func(Args...)>::type;
379template <
typename Func,
typename... Args>
384template <
typename Func,
typename... Args>
390template <
typename Func,
typename... Args>
394 auto& fib =
zth::fiber(f)(std::forward<Args>(args)...);
399 return fib.withFuture();
401 zth_throw(std::invalid_argument(
"policy"));
407template <
typename Func,
typename... Args>
411 return zth::async(policy, std::forward<Func>(f), std::forward<Args>(args)...);
std_future_base(std_future_base &&other) noexcept
std_future_base() noexcept=default
std_future_base & operator=(std_future_base &&other)
std_future_base & operator=(std_future_base const &)=delete
std::shared_future< zth::type< T > > share() const
std_future_base(std_future_base const &)=delete
std_promise_base(SharedPointer< Future_type > future) noexcept
void check_not_satisfied() const
std::future< zth::type< T > > get_future()
std_promise_base & operator=(std_promise_base &&other) noexcept
std_promise_base(std_promise_base const &)=delete
void swap(std_promise_base &other) noexcept
void set_exception(std::exception_ptr exception)
std_promise_base(std_promise_base &&other) noexcept
void set_value(U &&value)
std_promise_base & operator=(std_promise_base const &)=delete
zth::SharedPointer< zth::Future< T > > m_future
bool valid() const noexcept
std::future_status wait_until(const std::chrono::time_point< Clock, Duration > &timeout_time) const
std::future_status wait_for(std::chrono::duration< Rep, Period > const &timeout_duration) const
std_shared_future_base() noexcept=default
fiber_type< F >::factory fiber(F f, char const *name=nullptr)
Create a new fiber.
#define ZTH_CLASS_NEW_DELETE(T)
Define new/delete operators for a class, which are allocator-aware.
zth::future< zth::impl::invoke_result< Func, Args... > > async(zth::launch policy, Func &&f, Args &&... args)
typename std::invoke_result_t< Func, Args... > invoke_result
zth::future< zth::impl::invoke_result< Func, Args... > > async(Func &&f, Args &&... args)
::std::future< zth::type< T > > future
::std::shared_future< zth::type< T > > shared_future
::std::promise< zth::type< T > > promise