MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
promise.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_PROMISE_HPP__
2#define MSTL_CORE_ASYNC_PROMISE_HPP__
3
10
14
20
31template <typename Res>
32class promise {
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");
36
37public:
38 using state_type = _INNER __future_base::state_base;
39 using result_type = _INNER __future_base::basic_result<Res>;
40 using ptr_type = _INNER __future_base::Ptr<result_type>;
41
42private:
43 shared_ptr<state_type> future_ptr;
44 ptr_type storage;
45
46 template <typename T, typename U>
47 friend struct _INNER __future_base::state_base::setter;
48
54 MSTL_NODISCARD state_type& state() const {
55 _INNER __future_base::state_base::check(future_ptr);
56 return *future_ptr;
57 }
58
59public:
66 : future_ptr(_MSTL make_shared<state_type>()),
67 storage(new result_type()) {}
68
73 promise(promise&& other) noexcept
74 : future_ptr(_MSTL move(other.future_ptr)),
75 storage(_MSTL move(other.storage)) {}
76
82 promise& operator =(promise&& other) noexcept {
83 promise(_MSTL move(other)).swap(*this);
84 return *this;
85 }
86
87 promise(const promise&) = delete;
88 promise& operator =(const promise&) = delete;
89
96 if (static_cast<bool>(future_ptr) && !future_ptr.unique()) {
97 future_ptr->break_promise(_MSTL move(storage));
98 }
99 }
100
105 void swap(promise& other) noexcept {
106 future_ptr.swap(other.future_ptr);
107 storage.swap(other.storage);
108 }
109
116 return future<Res>(future_ptr);
117 }
118
124 void set_value(Res&& value) {
125 state().set_result(state_type::create_setter(this, _MSTL forward<Res>(value)));
126 }
127
134 state().set_result(state_type::create_setter(exception, this));
135 }
136
143 void set_value_at_thread_exit(Res&& value) {
144 state().set_delayed_result(state_type::create_setter(this, _MSTL forward<Res>(value)), future_ptr);
145 }
146
153 state().set_delayed_result(state_type::create_setter(exception, this), future_ptr);
154 }
155};
156
161template <typename Res>
162class promise<Res&> {
163public:
164 using state_type = _INNER __future_base::state_base;
165 using result_type = _INNER __future_base::basic_result<Res&>;
166 using ptr_type = _INNER __future_base::Ptr<result_type>;
167
168private:
169 shared_ptr<state_type> future_ptr;
170 ptr_type storage;
171
172 template <typename T, typename U>
173 friend struct _INNER __future_base::state_base::setter;
174
175 MSTL_NODISCARD state_type& state() const {
176 _INNER __future_base::state_base::check(future_ptr);
177 return *future_ptr;
178 }
179
180public:
185 : future_ptr(_MSTL make_shared<state_type>()),
186 storage(new result_type()) {}
187
191 promise(promise&& other) noexcept
192 : future_ptr(_MSTL move(other.future_ptr)),
193 storage(_MSTL move(other.storage)) {}
194
198 promise& operator =(promise&& other) noexcept {
199 promise(_MSTL move(other)).swap(*this);
200 return *this;
201 }
202
203 promise(const promise&) = delete;
204 promise& operator =(const promise&) = delete;
205
210 if (static_cast<bool>(future_ptr) && !future_ptr.unique()) {
211 future_ptr->break_promise(_MSTL move(storage));
212 }
213 }
214
218 void swap(promise& other) noexcept {
219 future_ptr.swap(other.future_ptr);
220 storage.swap(other.storage);
221 }
222
228 return future<Res&>(future_ptr);
229 }
230
236 void set_value(Res& value) {
237 state().set_result(state_type::create_setter(this, value));
238 }
239
246 state().set_result(state_type::create_setter(exception, this));
247 }
248
254 void set_value_at_thread_exit(Res& value) {
255 state().set_delayed_result(state_type::create_setter(this, value), future_ptr);
256 }
257
264 state().set_delayed_result(state_type::create_setter(exception, this), future_ptr);
265 }
266};
267
271template <>
272class promise<void> {
273public:
274 using state_type = _INNER __future_base::state_base;
275 using result_type = _INNER __future_base::basic_result<void>;
276 using ptr_type = _INNER __future_base::Ptr<result_type>;
277
278private:
279 shared_ptr<state_type> future_ptr;
280 ptr_type storage;
281
282 template <typename T, typename U>
283 friend struct _INNER __future_base::state_base::setter;
284
285 MSTL_NODISCARD state_type& state() const {
286 _INNER __future_base::state_base::check(future_ptr);
287 return *future_ptr;
288 }
289
290public:
295 : future_ptr(_MSTL make_shared<state_type>()),
296 storage(new result_type()) {}
297
301 promise(promise&& other) noexcept
302 : future_ptr(_MSTL move(other.future_ptr)),
303 storage(_MSTL move(other.storage)) {}
304
308 promise& operator =(promise&& other) noexcept {
309 promise(_MSTL move(other)).swap(*this);
310 return *this;
311 }
312
313 promise(const promise&) = delete;
314 promise& operator =(const promise&) = delete;
315
320 if (static_cast<bool>(future_ptr) && !future_ptr.unique()) {
321 future_ptr->break_promise(_MSTL move(storage));
322 }
323 }
324
328 void swap(promise& other) noexcept {
329 future_ptr.swap(other.future_ptr);
330 storage.swap(other.storage);
331 }
332
337 MSTL_NODISCARD future<void> get_future() const {
338 return future<void>(future_ptr);
339 }
340
345 void set_value() {
346 state().set_result(state_type::create_setter(this));
347 }
348
355 state().set_result(state_type::create_setter(exception, this));
356 }
357
363 state().set_delayed_result(state_type::create_setter(this), future_ptr);
364 }
365
372 state().set_delayed_result(state_type::create_setter(exception, this), future_ptr);
373 }
374};
375
378
386template <typename PtrT, typename Func, typename>
387struct __future_base::task_setter {
388 PtrT* result_ptr;
389 Func* function_ptr;
390
396 PtrT operator ()() const noexcept {
397 try {
398 (*result_ptr)->set((*function_ptr)());
399 } catch (...) {
400 (*result_ptr)->error_ptr = _MSTL current_exception();
401 }
402 return _MSTL move(*result_ptr);
403 }
404};
405
411template <typename PtrT, typename Func>
412struct __future_base::task_setter<PtrT, Func, void> {
413 PtrT* result_ptr;
414 Func* function_ptr;
415
416 PtrT operator ()() const noexcept {
417 try {
418 (*function_ptr)();
419 } catch (...) {
420 (*result_ptr)->error_ptr = current_exception();
421 }
422 return _MSTL move(*result_ptr);
423 }
424};
425
433template <typename Res, typename... Args>
434class __future_base::task_state_base<Res(Args...)> : public __future_base::state_base {
435public:
436 using result_type = Res;
437 using PtrType = __future_base::Ptr<basic_result<Res>>;
438
439 PtrType result_storage;
440
446 template <typename Alloc>
447 task_state_base(const Alloc& alloc)
448 : result_storage(allocate_result<Res>(alloc)) {}
449
455 virtual void run(Args&&... args) = 0;
456
463 virtual void run_delayed(Args&&... args, weak_ptr<state_base> self) = 0;
464
470 virtual shared_ptr<task_state_base> reset() = 0;
471};
472
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...)> {
485public:
492 template <typename Func2>
493 task_state(Func2&& func, const Alloc& alloc)
494 : task_state_base<Res(Args...)>(alloc)
495 , impl(_MSTL forward<Func2>(func), alloc) {}
496
497private:
498 void run(Args&&... args) override {
499 auto bound_func = [&]() -> Res {
500 return _MSTL invoke_r<Res>(impl.function_ptr, _MSTL forward<Args>(args)...);
501 };
502 state_base::set_result(
503 __future_base::create_task_setter(this->result_storage, bound_func));
504 }
505
506 void run_delayed(Args&&... args, weak_ptr<state_base> self) override {
507 auto bound_function = [&]() -> Res {
508 return _MSTL invoke_r<Res>(impl.function_ptr, _MSTL forward<Args>(args)...);
509 };
510 state_base::set_delayed_result(
511 __future_base::create_task_setter(this->result_storage, bound_function), _MSTL move(self));
512 }
513
514 shared_ptr<task_state_base<Res(Args...)>>
515 reset() override;
516
522 struct Impl : Alloc {
523 Func function_ptr;
524
525 template <typename Func2>
526 Impl(Func2&& func, const Alloc& alloc)
527 : Alloc(alloc), function_ptr(_MSTL forward<Func2>(func)) {}
528 } impl;
529};
530
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>;
544 return _MSTL allocate_shared<State>(alloc, _MSTL forward<Func>(func), alloc);
545}
546
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));
557}
558
561 // Async
563
565#endif // MSTL_CORE_ASYNC_PROMISE_HPP__
独占future类模板
promise()
默认构造函数
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)
在线程退出时设置异常
~promise()
析构函数
_INNER __future_base::state_base state_type
状态类型
promise(promise &&other) noexcept
移动构造函数
MSTL_NODISCARD future< void > get_future() const
获取关联的future对象
void set_value()
设置void结果
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
状态类型
promise()
默认构造函数
_INNER __future_base::Ptr< result_type > ptr_type
结果指针类型
void set_exception(exception_ptr exception)
设置异常
void swap(promise &other) noexcept
交换两个promise对象
~promise()
析构函数
promise & operator=(promise &&other) noexcept
移动赋值运算符
void set_value(Res &&value)
设置结果值
_INNER __future_base::Ptr< result_type > ptr_type
结果指针类型
~promise()
析构函数
void swap(promise &other) noexcept
交换两个promise对象
_INNER __future_base::basic_result< Res > result_type
结果类型
promise()
默认构造函数
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异步结果消费者
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)
移动范围元素
MSTL标准分配器
异常基类