YSTest: future.hpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

future.hpp
浏览该文件的文档.
1 /*
2 © 2015-2016, 2018, 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ystdex_future_hpp_
29 #define YB_INC_ystdex_future_hpp_ 1
30
31 #include "invoke.hpp" // for false_, true_, decay_t, invoke_result_t;
32 #include <future> // for std::future, std::packaged_task_t;
33 #include <libdefect/exception.h> // for std::exception_ptr;
34 #include <memory> // for std::shared_ptr;
35
36 namespace ystdex
37 {
38
40
41 template<typename>
42 struct is_future_type : false_
43 {};
44
45 template<typename _type>
46 struct is_future_type<std::future<_type>> : true_
47 {};
48
49
50 #if !__GLIBCXX__ || (defined(_GLIBCXX_HAS_GTHREADS) \
51 && defined(_GLIBCXX_USE_C99_STDINT_TR1) && (ATOMIC_INT_LOCK_FREE > 1))
52 template<typename _type>
53 std::future<decay_t<_type>>
54 make_ready_future(_type&& val)
55 {
56 using val_t = decay_t<_type>;
57 std::promise<val_t> pm;
58
59 pm.set_value(std::forward<val_t>(val));
60 return pm.get_future();
61 }
62 inline
63 std::future<void>
64 make_ready_future()
65 {
66 std::promise<void> pm;
67
68 pm.set_value();
69 return pm.get_future();
70 }
71 template<typename _type>
72 std::future<_type>
73 make_ready_future(std::exception_ptr p)
74 {
75 std::promise<_type> pm;
76
77 pm.set_exception(p);
78 return pm.get_future();
79 }
81 #endif
82
83
85 template<typename _fCallable, typename... _tParams>
86 using future_result_t = std::future<invoke_result_t<_fCallable, _tParams...>>;
87
89 template<typename _fCallable, typename... _tParams>
90 using packed_task_t
91 = std::packaged_task<invoke_result_t<_fCallable, _tParams...>()>;
92
93
95
96 template<typename _fCallable, typename... _tParams>
97 packed_task_t<_fCallable&&, _tParams&&...>
98 pack_task(_fCallable&& f, _tParams&&... args)
99 {
100 return packed_task_t<_fCallable&&, _tParams&&...>(
101 std::bind(yforward(f), yforward(args)...));
102 }
103
104 template<typename _fCallable, typename... _tParams>
105 std::shared_ptr<packed_task_t<_fCallable&&, _tParams&&...>>
106 pack_shared_task(_fCallable&& f, _tParams&&... args)
107 {
108 return std::make_shared<packed_task_t<_fCallable&&,
109 _tParams&&...>>(std::bind(yforward(f), yforward(args)...));
110 }
112
113 } // namespace ystdex;
114
115 #endif
116
invoke.hpp
可调用对象和调用包装接口。
ystdex::make_shared
std::shared_ptr< _type > make_shared(std::initializer_list< _tValue > il)
使用指定类型的初始化列表构造指定类型的 std::shared_ptr 对象。
Definition: memory.hpp:855
ystdex::make_ready_future
std::future< decay_t< _type > > make_ready_future(_type &&val)
Definition: future.hpp:54
exception.h
标准库实现 <exception> 修正。
CHRLib::f
f(d, s)
CHRLib::args
YB_ATTR_nodiscard _tParams && args
Definition: CharacterProcessing.h:649
ystdex::pack_shared_task
std::shared_ptr< packed_task_t< _fCallable &&, _tParams &&... > > pack_shared_task(_fCallable &&f, _tParams &&... args)
Definition: future.hpp:106
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: meta.hpp:196
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
ystdex::packed_task_t
std::packaged_task< invoke_result_t< _fCallable, _tParams... >()> packed_task_t
Definition: future.hpp:91
std
Definition: optional.h:982
ystdex::pack_task
packed_task_t< _fCallable &&, _tParams &&... > pack_task(_fCallable &&f, _tParams &&... args)
Definition: future.hpp:98
ystdex::cpp2017::invoke_result_t
_t< invoke_result< _fCallable, _tParams... > > invoke_result_t
Definition: invoke.hpp:393
ystdex::is_future_type
Definition: future.hpp:43
ystdex::true_
bool_< true > true_
Definition: integral_constant.hpp:109
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
platform::p
return p p
Definition: YCLib/FileSystem.h:209
ystdex::false_
bool_< false > false_
Definition: integral_constant.hpp:110
ystdex::future_result_t
std::future< invoke_result_t< _fCallable, _tParams... > > future_result_t
Definition: future.hpp:86