1#ifndef NEFORCE_CORE_ASYNC_PROMISE_HPP__
2#define NEFORCE_CORE_ASYNC_PROMISE_HPP__
12NEFORCE_BEGIN_NAMESPACE__
36template <
typename Res>
45 using ptr_type = inner::__future_base::Ptr<result_type>;
51 template <
typename T,
typename U>
52 friend struct inner::__future_base::state_base::setter;
60 inner::__future_base::state_base::check(future_ptr);
79 future_ptr(_NEFORCE
move(other.future_ptr)),
80 storage(_NEFORCE
move(other.storage)) {}
101 if (
static_cast<bool>(future_ptr) && !future_ptr.unique()) {
102 future_ptr->break_promise(_NEFORCE
move(storage));
111 future_ptr.swap(other.future_ptr);
112 storage.swap(other.storage);
143 state().set_delayed_result(state_type::create_setter(
this, _NEFORCE
forward<Res>(value)), future_ptr);
152 state().set_delayed_result(state_type::create_setter(
exception,
this), future_ptr);
160template <
typename Res>
165 using ptr_type = inner::__future_base::Ptr<result_type>;
171 template <
typename T,
typename U>
172 friend struct inner::__future_base::state_base::setter;
175 inner::__future_base::state_base::check(future_ptr);
191 future_ptr(_NEFORCE
move(other.future_ptr)),
192 storage(_NEFORCE
move(other.storage)) {}
209 if (
static_cast<bool>(future_ptr) && !future_ptr.unique()) {
210 future_ptr->break_promise(_NEFORCE
move(storage));
218 future_ptr.swap(other.future_ptr);
219 storage.swap(other.storage);
233 void set_value(Res& value) { state().set_result(state_type::create_setter(
this, value)); }
248 state().set_delayed_result(state_type::create_setter(
this, value), future_ptr);
257 state().set_delayed_result(state_type::create_setter(
exception,
this), future_ptr);
269 using ptr_type = inner::__future_base::Ptr<result_type>;
275 template <
typename T,
typename U>
276 friend struct inner::__future_base::state_base::setter;
279 inner::__future_base::state_base::check(future_ptr);
295 future_ptr(_NEFORCE
move(other.future_ptr)),
296 storage(_NEFORCE
move(other.storage)) {}
313 if (
static_cast<bool>(future_ptr) && !future_ptr.unique()) {
314 future_ptr->break_promise(_NEFORCE
move(storage));
322 future_ptr.swap(other.future_ptr);
323 storage.swap(other.storage);
336 void set_value() { state().set_result(state_type::create_setter(
this)); }
357 state().set_delayed_result(state_type::create_setter(
exception,
this), future_ptr);
371template <
typename PtrT,
typename Func,
typename>
372struct __future_base::task_setter {
381 PtrT operator()() const noexcept {
383 (*result_ptr)->set((*function_ptr)());
387 return _NEFORCE
move(*result_ptr);
396template <
typename PtrT,
typename Func>
397struct __future_base::task_setter<PtrT, Func, void> {
401 PtrT operator()() const noexcept {
407 return _NEFORCE
move(*result_ptr);
418template <
typename Res,
typename... Args>
419class __future_base::task_state_base<Res(Args...)> :
public __future_base::state_base {
421 using result_type = Res;
422 using PtrType = __future_base::Ptr<basic_result<Res>>;
424 PtrType result_storage;
431 template <
typename Alloc>
432 task_state_base(
const Alloc& alloc) :
433 result_storage(allocate_result<Res>(alloc)) {}
440 virtual void run(Args&&... args) = 0;
448 virtual void run_delayed(Args&&... args, weak_ptr<state_base> self) = 0;
455 virtual shared_ptr<task_state_base> reset() = 0;
467template <
typename Func,
typename Alloc,
typename Res,
typename... Args>
468class __future_base::task_state<Func, Alloc, Res(Args...)> final :
public __future_base::task_state_base<Res(Args...)> {
476 template <
typename Func2>
477 task_state(Func2&& func,
const Alloc& alloc) :
478 task_state_base<Res(Args...)>(alloc),
479 impl(_NEFORCE
forward<Func2>(func), alloc) {}
482 void run(Args&&... args)
override {
483 auto bound_func = [&]() -> Res {
486 state_base::set_result(__future_base::create_task_setter(this->result_storage, bound_func));
489 void run_delayed(Args&&... args, weak_ptr<state_base> self)
override {
490 auto bound_function = [&]() -> Res {
493 state_base::set_delayed_result(__future_base::create_task_setter(this->result_storage, bound_function),
494 _NEFORCE
move(self));
497 shared_ptr<task_state_base<Res(Args...)>> reset()
override;
504 struct Impl : Alloc {
507 template <
typename Func2>
508 Impl(Func2&& func,
const Alloc& alloc) :
510 function_ptr(_NEFORCE
forward<Func2>(func)) {}
523template <
typename Sign,
typename Func,
typename Alloc = _NEFORCE allocator<
int>>
525 using State = __future_base::task_state<decay_t<Func>, Alloc, Sign>;
535template <
typename Func,
typename Alloc,
typename Res,
typename... Args>
536shared_ptr<__future_base::task_state_base<Res(Args...)>> __future_base::task_state<Func, Alloc, Res(Args...)>::reset() {
537 return inner::create_task_state<Res(Args...)>(_NEFORCE
move(impl.function_ptr),
static_cast<Alloc&
>(impl));
547NEFORCE_END_NAMESPACE__
void set_value_at_thread_exit(Res &value)
在线程退出时设置结果引用
promise & operator=(promise &&other) noexcept
移动赋值运算符
promise(promise &&other) noexcept
移动构造函数
promise(const promise &)=delete
禁止拷贝构造
future< Res & > get_future()
获取关联的future对象
inner::__future_base::Ptr< result_type > ptr_type
结果指针类型
void set_exception(exception_ptr exception)
设置异常
inner::__future_base::basic_result< Res & > result_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 & operator=(const promise &)=delete
禁止拷贝赋值
promise & operator=(const promise &)=delete
禁止拷贝赋值
promise & operator=(promise &&other) noexcept
移动赋值运算符
promise(promise &&other) noexcept
移动构造函数
void set_exception_at_thread_exit(exception_ptr exception)
在线程退出时设置异常
NEFORCE_NODISCARD future< void > get_future() const
获取关联的future对象
promise(const promise &)=delete
禁止拷贝构造
inner::__future_base::basic_result< void > result_type
结果类型
void set_value_at_thread_exit()
在线程退出时设置void结果
inner::__future_base::state_base state_type
状态类型
void set_exception(exception_ptr exception)
设置异常
void swap(promise &other) noexcept
交换两个promise对象
inner::__future_base::Ptr< result_type > ptr_type
结果指针类型
promise & operator=(promise &&other) noexcept
移动赋值运算符
inner::__future_base::Ptr< result_type > ptr_type
结果指针类型
void set_value(Res &&value)
设置结果值
void swap(promise &other) noexcept
交换两个promise对象
promise & operator=(const promise &)=delete
禁止拷贝赋值
inner::__future_base::basic_result< Res > result_type
结果类型
promise(promise &&other) noexcept
移动构造函数
inner::__future_base::state_base state_type
状态类型
future< Res > get_future()
获取关联的future对象
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)
在线程退出时设置异常
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_INLINE17 constexpr bool is_array_v
is_array的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_function_v
is_function的便捷变量模板
exception_ptr NEFORCE_API current_exception() noexcept
获取当前异常
NEFORCE_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)
带返回类型检查的统一调用接口
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) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
NEFORCE_INLINE17 constexpr bool is_destructible_v
is_destructible的便捷变量模板