15#if defined(__cplusplus) && __cplusplus >= 201103L
24# include <type_traits>
37using promise = ::std::promise<zth::type<T>>;
40using future = ::std::future<zth::type<T>>;
72# define ZTH_FUTURE_CTORS(future_class_type) \
73 using Future_type = typename base::Future_type; \
75 future_class_type() noexcept = default; \
77 future_class_type(zth::SharedPointer<Future_type> const& f) noexcept \
81 future_class_type(zth::SharedReference<Future_type> const& f) noexcept \
101# define ZTH_FUTURE_MOVE(future_class_type) \
102 future_class_type(future_class_type&& other) noexcept = default; \
104 future_class_type(zth::SharedPointer<Future_type>&& f) noexcept \
105 : base{std::move(f)} \
108 future_class_type(zth::SharedReference<Future_type>&& f) noexcept \
109 : base{std::move(f)} \
112 future_class_type& operator=(future_class_type&& other) noexcept = default; \
114 future_class_type& operator=(zth::SharedPointer<Future_type>&& other) noexcept \
116 this->base::operator=(std::move(other)); \
120 future_class_type& operator=(zth::SharedReference<Future_type>&& other) noexcept \
122 this->base::operator=(std::move(other)); \
140# define ZTH_FUTURE_COPY(future_class_type) \
141 future_class_type(future_class_type const& other) noexcept = default; \
143 future_class_type& operator=(future_class_type const& other) noexcept = default; \
145 future_class_type& operator=(zth::SharedPointer<Future_type> const& other) noexcept \
147 this->base::operator=(other); \
151 future_class_type& operator=(zth::SharedReference<Future_type> const& other) noexcept \
153 this->base::operator=(other); \
168 template <
class Rep,
class Period>
170 wait_for(std::chrono::duration<Rep, Period>
const& timeout_duration)
const
173 return m_future->block(timeout_duration) ? std::future_status::ready
174 : std::future_status::timeout;
177 template <
class Clock,
class Duration>
179 wait_until(
const std::chrono::time_point<Clock, Duration>& timeout_time)
const
182 return m_future->block(timeout_time) ? std::future_status::ready
183 : std::future_status::timeout;
207 zth_throw(std::future_error(std::future_errc::no_state));
227 std::shared_future<zth::type<T>>
share() const noexcept
229 auto f = std::shared_future<zth::type<T>>(std::move(this->
m_future));
246 : m_future{std::move(
future)}
254 this->m_future = std::move(other.m_future);
263 std::swap(m_future, other.m_future);
271 zth_throw(std::future_error(std::future_errc::future_already_retrieved));
277 template <
typename U>
281 m_future->set(std::forward<U>(value));
300 zth_throw(std::future_error(std::future_errc::no_state));
307 if(m_future->valid())
308 zth_throw(std::future_error(std::future_errc::promise_already_satisfied));
313 bool m_got_future =
false;
325 using value_type =
typename base::value_type;
330 future(future
const&) =
delete;
331 future& operator=(future
const&) =
delete;
333 value_type
const& get()
335 return *base::value();
343 using value_type = T&;
349 future(future
const&) =
delete;
350 future& operator=(future
const&) =
delete;
354 return **base::value();
363 using value_type = void;
368 future(future
const&) =
delete;
369 future& operator=(future
const&) =
delete;
381 using value_type = T;
388 value_type
const& get()
390 return *base::value();
398 using value_type = T&;
407 return **base::value();
415 using value_type = void;
432 using value_type = T;
439 : base{std::move(future)}
446 using value_type = T&;
453 : base{std::move(future)}
466 : base{std::move(future)}
472# undef ZTH_FUTURE_CTORS
473# undef ZTH_FUTURE_MOVE
474# undef ZTH_FUTURE_COPY
495# if __cplusplus < 201703L
496template <
typename Func,
typename... Args>
497using invoke_result =
typename std::result_of<Func(Args...)>::type;
499template <
typename Func,
typename... Args>
504template <
typename Func,
typename... Args>
510template <
typename Func,
typename... Args>
514 auto fib =
zth::fiber(std::forward<Func>(f), std::forward<Args>(args)...);
521 zth_throw(std::invalid_argument(
"policy"));
527template <
typename Func,
typename... Args>
531 return zth::async(policy, std::forward<Func>(f), std::forward<Args>(args)...);
constexpr type * get() const noexcept
typename base::value_type value_type
std::shared_future< zth::type< T > > share() const noexcept
std_future_base & operator=(std_future_base const &)=delete
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=default
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_shared_future_base & operator=(std_shared_future_base &&other) noexcept=default
std_shared_future_base(SharedReference< Future_type > const &future) noexcept
std_shared_future_base & operator=(SharedPointer< Future_type > const &other) noexcept
std_shared_future_base(SharedPointer< Future_type > &&future) 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
std_shared_future_base & operator=(SharedReference< Future_type > &&other) noexcept
Future_type & value() const
std_shared_future_base & operator=(SharedPointer< Future_type > &&other) noexcept
std_shared_future_base(SharedReference< Future_type > &&future) noexcept
std_shared_future_base & operator=(std_shared_future_base const &other) noexcept=default
std_shared_future_base & operator=(SharedReference< Future_type > const &other) noexcept
#define ZTH_FUTURE_CTORS(future_class_type)
#define ZTH_FUTURE_COPY(future_class_type)
#define ZTH_FUTURE_MOVE(future_class_type)
fiber_type< F >::fiber fiber(F &&f, Args &&... args)
Create and start a new fiber.
launch
Launch policies for std::async to start fibers instead of threads.
@ awaitable
Start fiber, returning a future to await.
@ detached
Start fiber, without a future to await.
#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
Forces the fiber to have a future that outlives the fiber.
Base exception class for Zth exceptions.