1#ifndef MSTL_CORE_ASYNC_PROMISE_HPP__
2#define MSTL_CORE_ASYNC_PROMISE_HPP__
31template <
typename Res>
33 static_assert(!is_array_v<Res>,
"result type must not be an array");
34 static_assert(!is_function_v<Res>,
"result type must not be a function");
35 static_assert(is_destructible_v<Res>,
"result type must be destructible");
46 template <
typename T,
typename U>
47 friend struct _INNER __future_base::state_base::setter;
55 _INNER __future_base::state_base::check(future_ptr);
74 : future_ptr(
_MSTL move(other.future_ptr)),
96 if (
static_cast<bool>(future_ptr) && !future_ptr.unique()) {
97 future_ptr->break_promise(
_MSTL move(storage));
106 future_ptr.swap(other.future_ptr);
107 storage.swap(other.storage);
134 state().set_result(state_type::create_setter(
exception,
this));
144 state().set_delayed_result(state_type::create_setter(
this,
_MSTL forward<Res>(value)), future_ptr);
153 state().set_delayed_result(state_type::create_setter(
exception,
this), future_ptr);
161template <
typename Res>
172 template <
typename T,
typename U>
173 friend struct _INNER __future_base::state_base::setter;
176 _INNER __future_base::state_base::check(future_ptr);
192 : future_ptr(
_MSTL move(other.future_ptr)),
210 if (
static_cast<bool>(future_ptr) && !future_ptr.unique()) {
211 future_ptr->break_promise(
_MSTL move(storage));
219 future_ptr.swap(other.future_ptr);
220 storage.swap(other.storage);
237 state().set_result(state_type::create_setter(
this, value));
246 state().set_result(state_type::create_setter(
exception,
this));
255 state().set_delayed_result(state_type::create_setter(
this, value), future_ptr);
264 state().set_delayed_result(state_type::create_setter(
exception,
this), future_ptr);
282 template <
typename T,
typename U>
283 friend struct _INNER __future_base::state_base::setter;
286 _INNER __future_base::state_base::check(future_ptr);
302 : future_ptr(
_MSTL move(other.future_ptr)),
320 if (
static_cast<bool>(future_ptr) && !future_ptr.unique()) {
321 future_ptr->break_promise(
_MSTL move(storage));
329 future_ptr.swap(other.future_ptr);
330 storage.swap(other.storage);
346 state().set_result(state_type::create_setter(
this));
355 state().set_result(state_type::create_setter(
exception,
this));
363 state().set_delayed_result(state_type::create_setter(
this), future_ptr);
372 state().set_delayed_result(state_type::create_setter(
exception,
this), future_ptr);
386template <
typename PtrT,
typename Func,
typename>
387struct __future_base::task_setter {
396 PtrT operator ()() const noexcept {
398 (*result_ptr)->set((*function_ptr)());
411template <
typename PtrT,
typename Func>
412struct __future_base::task_setter<PtrT, Func, void> {
416 PtrT operator ()() const noexcept {
433template <
typename Res,
typename... Args>
434class __future_base::task_state_base<Res(Args...)> :
public __future_base::state_base {
436 using result_type = Res;
437 using PtrType = __future_base::Ptr<basic_result<Res>>;
439 PtrType result_storage;
446 template <
typename Alloc>
447 task_state_base(
const Alloc& alloc)
448 : result_storage(allocate_result<Res>(alloc)) {}
455 virtual void run(Args&&... args) = 0;
463 virtual void run_delayed(Args&&... args, weak_ptr<state_base> self) = 0;
470 virtual shared_ptr<task_state_base> reset() = 0;
482template <
typename Func,
typename Alloc,
typename Res,
typename... Args>
483class __future_base::task_state<Func, Alloc, Res(Args...)> final
484 :
public __future_base::task_state_base<Res(Args...)> {
492 template <
typename Func2>
493 task_state(Func2&& func,
const Alloc& alloc)
494 : task_state_base<Res(Args...)>(alloc)
498 void run(Args&&... args)
override {
499 auto bound_func = [&]() -> Res {
502 state_base::set_result(
503 __future_base::create_task_setter(this->result_storage, bound_func));
506 void run_delayed(Args&&... args, weak_ptr<state_base> self)
override {
507 auto bound_function = [&]() -> Res {
510 state_base::set_delayed_result(
511 __future_base::create_task_setter(this->result_storage, bound_function),
_MSTL move(self));
514 shared_ptr<task_state_base<Res(Args...)>>
522 struct Impl : Alloc {
525 template <
typename Func2>
526 Impl(Func2&& func,
const Alloc& alloc)
527 : Alloc(alloc), function_ptr(
_MSTL forward<Func2>(func)) {}
540template <
typename Sign,
typename Func,
typename Alloc = _MSTL allocator<
int>>
542create_task_state(Func&& func,
const Alloc& alloc = Alloc()) {
543 using State = __future_base::task_state<decay_t<Func>, Alloc, Sign>;
553template <
typename Func,
typename Alloc,
typename Res,
typename... Args>
554shared_ptr<__future_base::task_state_base<Res(Args...)>>
555__future_base::task_state<Func, Alloc, Res(Args...)>::reset() {
556 return _INNER create_task_state<Res(Args...)>(
_MSTL move(impl.function_ptr),
static_cast<Alloc&
>(impl));
void set_value_at_thread_exit(Res &value)
在线程退出时设置结果引用
_INNER __future_base::basic_result< Res & > result_type
结果类型
promise(promise &&other) noexcept
移动构造函数
promise(const promise &)=delete
禁止拷贝构造
future< Res & > get_future()
获取关联的future对象
void set_exception(exception_ptr exception)
设置异常
_INNER __future_base::Ptr< result_type > ptr_type
结果指针类型
void swap(promise &other) noexcept
交换两个promise对象
void set_value(Res &value)
设置结果引用
void set_exception_at_thread_exit(exception_ptr exception)
在线程退出时设置异常
_INNER __future_base::state_base state_type
状态类型
promise(promise &&other) noexcept
移动构造函数
MSTL_NODISCARD future< void > get_future() const
获取关联的future对象
void set_exception_at_thread_exit(exception_ptr exception)
在线程退出时设置异常
_INNER __future_base::basic_result< void > result_type
结果类型
promise(const promise &)=delete
禁止拷贝构造
void set_value_at_thread_exit()
在线程退出时设置void结果
_INNER __future_base::state_base state_type
状态类型
_INNER __future_base::Ptr< result_type > ptr_type
结果指针类型
void set_exception(exception_ptr exception)
设置异常
void swap(promise &other) noexcept
交换两个promise对象
promise & operator=(promise &&other) noexcept
移动赋值运算符
void set_value(Res &&value)
设置结果值
_INNER __future_base::Ptr< result_type > ptr_type
结果指针类型
void swap(promise &other) noexcept
交换两个promise对象
_INNER __future_base::basic_result< Res > result_type
结果类型
promise(promise &&other) noexcept
移动构造函数
future< Res > get_future()
获取关联的future对象
_INNER __future_base::state_base state_type
状态类型
promise(const promise &)=delete
禁止拷贝构造
void set_exception(exception_ptr exception)
设置异常
void set_value_at_thread_exit(Res &&value)
在线程退出时设置结果值
void set_exception_at_thread_exit(exception_ptr exception)
在线程退出时设置异常
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
exception_ptr MSTL_API current_exception() noexcept
获取当前异常
MSTL_CONSTEXPR14 enable_if_t< is_invocable_r< Res, Callable, Args... >::value, Res > invoke_r(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
带返回类型检查的统一调用接口
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
enable_if_t<!is_unbounded_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > make_shared(Args &&... args)
融合分配创建共享指针
enable_if_t<!is_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > allocate_shared(Alloc &alloc, Args &&... args)
使用分配器创建共享指针
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素