NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
async_result.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_ASYNC_RESULT_HPP__
2#define NEFORCE_CORE_ASYNC_ASYNC_RESULT_HPP__
3
15
18#include "NeForce/core/exception/system_exception.hpp"
21NEFORCE_BEGIN_NAMESPACE__
22
28
30struct use_future_t {};
32struct detached_t {};
34struct deferred_t {};
35
39constexpr detached_t detached{};
41constexpr deferred_t deferred{};
42
52template <typename Token, typename Signature>
53struct async_result {
54 using handler_type = Token;
55 using return_type = void;
56
57 Token token_;
58
59 explicit async_result(Token& t) :
60 token_(move(t)) {}
61
62 handler_type get_handler() { return move(token_); }
63 void get() {}
64};
65
67NEFORCE_BEGIN_INNER__
68
69template <typename... Args>
70struct future_handler {
71 shared_ptr<promise<tuple<Args...>>> promise_;
72
73 void operator()(Args... args) { promise_->set_value(make_tuple(move(args)...)); }
74};
75
76template <>
77struct future_handler<error_code> {
78 shared_ptr<promise<void>> promise_;
79
80 void operator()(error_code ec) {
81 if (ec) {
82 promise_->set_exception(_NEFORCE make_exception_ptr(system_exception(ec)));
83 } else {
84 promise_->set_value();
85 }
86 }
87};
88
89template <>
90struct future_handler<error_code, size_t> {
91 shared_ptr<promise<size_t>> promise_;
92
93 void operator()(error_code ec, size_t n) {
94 if (ec) {
95 promise_->set_exception(_NEFORCE make_exception_ptr(system_exception(ec)));
96 } else {
97 promise_->set_value(n);
98 }
99 }
100};
101
102NEFORCE_END_INNER__
104
105template <>
106struct async_result<use_future_t, void(error_code)> {
107 using handler_type = inner::future_handler<error_code>;
108 using return_type = future<void>;
109 handler_type handler_;
110 explicit async_result(use_future_t /*unused*/) { handler_.promise_ = make_shared<promise<void>>(); }
111 handler_type get_handler() { return handler_; }
112 return_type get() { return handler_.promise_->get_future(); }
113};
114
115template <>
116struct async_result<use_future_t, void(error_code, size_t)> {
117 using handler_type = inner::future_handler<error_code, size_t>;
118 using return_type = future<size_t>;
119 handler_type handler_;
120 explicit async_result(use_future_t /*unused*/) { handler_.promise_ = make_shared<promise<size_t>>(); }
121 handler_type get_handler() { return handler_; }
122 return_type get() { return handler_.promise_->get_future(); }
123};
124
125template <typename... Args>
126struct async_result<detached_t, void(Args...)> {
127 struct detached_handler {
128 function<void(Args...)> inner_;
129
130 void operator()(Args... args) {
131 try {
132 inner_(args...);
133 // NOLINTNEXTLINE(bugprone-empty-catch)
134 } catch (...) {
135 // ignore
136 }
137 }
138 };
139
140 using handler_type = detached_handler;
141 using return_type = void;
142
143 explicit async_result(detached_t /*unused*/) {}
144 handler_type get_handler() {
145 return detached_handler{[](Args...) {}};
146 }
147 void get() {}
148};
149 // CompletionTokens
151
152NEFORCE_END_NAMESPACE__
153#endif // NEFORCE_CORE_ASYNC_ASYNC_RESULT_HPP__
异常指针实现
通用函数包装器
enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
constexpr deferred_t deferred
deferred 常量
constexpr detached_t detached
detached 常量
constexpr use_future_t use_future
use_future 常量
exception_ptr make_exception_ptr(Ex ex) noexcept
创建异常指针
uint64_t size_t
无符号大小类型
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
function< float(float)> function
缓动函数类型:输入归一化时间 [0,1],输出进度 [0,1]
异步结果生产者
共享智能指针实现
Token handler_type
handler 类型
Token token_
存储的令牌
延迟执行完成令牌
fire-and-forget 完成令牌
返回 future<T> 的完成令牌