YSTest: thunk.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

thunk.hpp
浏览该文件的文档.
1 /*
2 © 2014-2016 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_thunk_hpp_
29 #define YB_INC_ystdex_thunk_hpp_ 1
30
31 #include "type_traits.hpp" // for decay_t, std::forward, exclude_self_t,
32 // enable_if_t, is_same, remove_reference_t, enable_if_convertible_t,
33 // std::move, result_of_t;
34 #include "functional.hpp" // for is_reference_wrapper, unwrap_reference_t,
35 // ystdex::invoke;
36
37 namespace ystdex
38 {
39
40 namespace details
41 {
42
44 template<typename _type>
45 struct thunk_call_proxy
46 {
47 _type arg;
48
49 _type
50 operator()() const
51 {
52 return arg;
53 }
54 };
55
57 template<typename _tRet, typename _fCallable>
58 struct thunk_caller
59 {
61 static_assert(is_decayed<_fCallable>(), "Invalid type found.");
62
64 using caller_type = _fCallable;
65 using return_type = _tRet;
66 using value_type = decay_t<unwrap_reference_t<_tRet>>;
67
69 caller_type caller;
70
71 thunk_caller(caller_type f)
72 : caller(f)
73 {}
75 thunk_caller(const value_type& arg)
76 : caller([arg]{
77 return std::forward<return_type>(arg);
78 })
79 {}
81 template<typename _type, yimpl(typename = exclude_self_t<thunk_caller,
82 _type>, typename = enable_if_t<is_reference_wrapper<_type>::value
83 && is_same<remove_reference_t<unwrap_reference_t<_type>>,
84 value_type>::value>)>
85 thunk_caller(_type arg)
86 : caller([arg]() ynoexcept(std::is_nothrow_constructible<return_type,
87 _type>::value) -> return_type{
88 return arg;
89 })
90 {}
91 thunk_caller(const thunk_caller&) = default;
92 thunk_caller(thunk_caller&&) = default;
93
94 thunk_caller&
95 operator=(const thunk_caller&) = default;
96 thunk_caller&
97 operator=(thunk_caller&&) = default;
98 };
99
100 } // namespace details;
101
102
108 template<typename _tRet, typename _fCallable
109 = std::function<remove_reference_t<unwrap_reference_t<_tRet>>()>>
110 class thunk : private details::thunk_caller<_tRet, decay_t<_fCallable>>
111 {
112 private:
113 using base = details::thunk_caller<_tRet, decay_t<_fCallable>>;
114
115 public:
117 using typename base::caller_type;
118 using typename base::return_type;
119 using typename base::value_type;
120
121 using base::caller;
122
123 thunk(const value_type& arg)
124 : base(arg)
125 {}
127 template<typename _type>
128 thunk(lref<_type> arg)
129 : base(lref<value_type>(arg.get()))
130 {}
132 template<typename _fCaller, yimpl(
133 typename = exclude_self_t<thunk, _fCaller>, typename
134 = enable_if_convertible_t<_fCaller&&, caller_type>)>
135 thunk(_fCaller&& f)
136 : base(std::move(caller_type(f)))
137 {}
138 thunk(const thunk&) = default;
139 thunk(thunk&&) = default;
140
141 thunk&
142 operator=(const thunk&) = default;
143 thunk&
144 operator=(thunk&&) = default;
145
147 return_type
148 operator()() const
149 {
150 return ystdex::invoke(caller);
151 }
152
154 operator return_type() const
155 {
156 return operator();
157 }
158 };
159
166 template<typename _fCallable>
167 thunk<result_of_t<_fCallable()>, decay_t<_fCallable>>
168 make_thunk(_fCallable&& f)
169 {
170 return thunk<result_of_t<_fCallable()>, decay_t<_fCallable>>(yforward(f));
171 }
173 template<typename _type>
174 yimpl(enable_if_t<sizeof(result_of_t<_type()>) != 0,
175 details::thunk_call_proxy<_type>>)
176 make_thunk(const _type& obj)
177 {
178 return ystdex::make_thunk(details::thunk_call_proxy<_type>{obj});
179 }
181
182 } // namespace ystdex;
183
184 #endif
185
ystdex::details::thunk_call_proxy
Definition: thunk.hpp:45
ystdex::details::thunk_caller::thunk_caller
thunk_caller(caller_type f)
Definition: thunk.hpp:71
ystdex::details::thunk_call_proxy::arg
_type arg
Definition: thunk.hpp:47
ystdex::cpp2014::result_of_t
typename result_of< _type >::type result_of_t
Definition: type_traits.hpp:355
CHRLib::f
f(d, s)
ystdex::thunk::caller_type
_fCallable caller_type
Definition: thunk.hpp:64
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
ystdex::details::thunk_call_proxy::operator()
_type operator()() const
Definition: thunk.hpp:50
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:343
ystdex::details::thunk_caller< _tRet, decay_t< _fCallable > >::caller_type
decay_t< _fCallable > caller_type
Definition: thunk.hpp:64
ystdex::details::thunk_caller
Definition: thunk.hpp:58
ystdex::is_reference_wrapper
判断模板参数指定的类型是否。
Definition: ref.hpp:161
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
ystdex::details::thunk_caller::thunk_caller
thunk_caller(_type arg)
Definition: thunk.hpp:85
ystdex::details::thunk_caller::caller
caller_type caller
Definition: thunk.hpp:69
ystdex::details::thunk_caller< _tRet, decay_t< _fCallable > >::return_type
_tRet return_type
Definition: thunk.hpp:65
type_traits.hpp
ISO C++ 类型特征扩展。
ystdex::thunk::value_type
decay_t< unwrap_reference_t< _tRet >> value_type
Definition: thunk.hpp:66
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::thunk::operator()
return_type operator()() const
Definition: thunk.hpp:148
ystdex::details::thunk_caller::thunk_caller
thunk_caller(const value_type &arg)
Definition: thunk.hpp:75
functional.hpp
函数和可调用对象。
ystdex::thunk::make_thunk
thunk< result_of_t< _fCallable()>, decay_t< _fCallable > > make_thunk(_fCallable &&f)
构造延迟调用对象。
Definition: thunk.hpp:168
ystdex::is_decayed
判断指定类型是否已退化。
Definition: type_traits.hpp:906
ystdex::thunk::thunk
thunk(lref< _type > arg)
Definition: thunk.hpp:128
ystdex::thunk::thunk
thunk(_fCaller &&f)
Definition: thunk.hpp:135
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:748
ystdex::cpp2014::remove_reference_t
typename remove_reference< _type >::type remove_reference_t
Definition: type_traits.hpp:299
std
Definition: memory.hpp:1183
ystdex::thunk
包装惰性求值的过程。
Definition: thunk.hpp:110
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::thunk::return_type
_tRet return_type
Definition: thunk.hpp:65
ystdex::invoke
result_of_t< _fCallable &&(_tParams &&...)> invoke(_fCallable &&f, _tParams &&...args)
调用可调用对象。
Definition: functional.hpp:339
ystdex::enable_if_convertible_t
enable_if_t< is_convertible< _tFrom, _tTo >::value, _type > enable_if_convertible_t
Definition: type_traits.hpp:1052
ystdex::thunk::thunk
thunk(const value_type &arg)
Definition: thunk.hpp:123
ystdex::unwrap_reference_t
_t< unwrap_reference< _type >> unwrap_reference_t
Definition: ref.hpp:191
ystdex::details::thunk_caller< _tRet, decay_t< _fCallable > >::value_type
decay_t< unwrap_reference_t< _tRet >> value_type
Definition: thunk.hpp:66
ystdex::details::thunk_caller::operator=
thunk_caller & operator=(const thunk_caller &)=default
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:746
ystdex::details::get
get())
Definition: cast.hpp:102