YSTest: thunk.hpp 源文件

YSTest PreAlpha_b700_20160611 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, enable_if_convertible_t, std::move, result_of_t;
33 #include "functional.hpp" // for is_reference_wrapper, unwrap_reference_t,
34 // ystdex::invoke;
35
36 namespace ystdex
37 {
38
39 namespace details
40 {
41
43 template<typename _type>
44 struct thunk_call_proxy
45 {
46 _type arg;
47
48 _type
49 operator()() const
50 {
51 return arg;
52 }
53 };
54
56 template<typename _tRet, typename _fCallable>
57 struct thunk_caller
58 {
60 static_assert(is_decayed<_fCallable>(), "Invalid type found.");
61
63 using caller_type = _fCallable;
64 using return_type = _tRet;
65 using value_type = decay_t<unwrap_reference_t<_tRet>>;
66
68 caller_type caller;
69
70 thunk_caller(caller_type f)
71 : caller(f)
72 {}
74 thunk_caller(const value_type& arg)
75 : caller([arg]{
76 return std::forward<return_type>(arg);
77 })
78 {}
80 template<typename _type, yimpl(typename = exclude_self_t<thunk_caller,
81 _type>, typename = enable_if_t<is_reference_wrapper<_type>::value
82 && is_same<unwrap_reference_t<_type>, value_type>::value>)>
83 thunk_caller(_type arg)
84 : caller([arg]() ynoexcept(std::is_nothrow_constructible<return_type,
85 _type>::value) -> return_type{
86 return arg;
87 })
88 {}
89 thunk_caller(const thunk_caller&) = default;
90 thunk_caller(thunk_caller&&) = default;
91
92 thunk_caller&
93 operator=(const thunk_caller&) = default;
94 thunk_caller&
95 operator=(thunk_caller&&) = default;
96 };
97
98 } // namespace details;
99
100
106 template<typename _tRet,
107 typename _fCallable = std::function<unwrap_reference_t<_tRet>()>>
108 class thunk : private details::thunk_caller<_tRet, decay_t<_fCallable>>
109 {
110 private:
111 using base = details::thunk_caller<_tRet, decay_t<_fCallable>>;
112
113 public:
115 using typename base::caller_type;
116 using typename base::return_type;
117 using typename base::value_type;
118
119 using base::caller;
120
121 thunk(const value_type& arg)
122 : base(arg)
123 {}
125 template<typename _type>
126 thunk(lref<_type> arg)
127 : base(lref<value_type>(arg.get()))
128 {}
130 template<typename _fCaller, yimpl(
131 typename = exclude_self_t<thunk, _fCaller>, typename
132 = enable_if_convertible_t<_fCaller&&, caller_type>)>
133 thunk(_fCaller&& f)
134 : base(std::move(caller_type(f)))
135 {}
136 thunk(const thunk&) = default;
137 thunk(thunk&&) = default;
138
139 thunk&
140 operator=(const thunk&) = default;
141 thunk&
142 operator=(thunk&&) = default;
143
145 return_type
146 operator()() const
147 {
148 return ystdex::invoke(caller);
149 }
150
152 operator return_type() const
153 {
154 return operator();
155 }
156 };
157
164 template<typename _fCallable>
165 thunk<result_of_t<_fCallable()>, decay_t<_fCallable>>
166 make_thunk(_fCallable&& f)
167 {
168 return thunk<result_of_t<_fCallable()>, decay_t<_fCallable>>(yforward(f));
169 }
171 template<typename _type>
172 yimpl(enable_if_t<sizeof(result_of_t<_type()>) != 0,
173 details::thunk_call_proxy<_type>>)
174 make_thunk(const _type& obj)
175 {
176 return ystdex::make_thunk(details::thunk_call_proxy<_type>{obj});
177 }
179
180 } // namespace ystdex;
181
182 #endif
183
ystdex::details::thunk_call_proxy::operator()
_type operator()() const
Definition: thunk.hpp:49
ystdex::thunk::operator()
return_type operator()() const
Definition: thunk.hpp:146
ystdex::enable_if_convertible_t
enable_if_t< is_convertible< _tFrom, _tTo >::value, _type > enable_if_convertible_t
Definition: type_traits.hpp:993
ystdex::details::thunk_caller::thunk_caller
thunk_caller(const value_type &arg)
Definition: thunk.hpp:74
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:350
ystdex::invoke
result_of_t< _fCallable &&(_tParams &&...)> invoke(_fCallable &&f, _tParams &&...args)
调用可调用对象。
Definition: functional.hpp:337
ystdex::thunk::value_type
decay_t< unwrap_reference_t< _tRet >> value_type
Definition: thunk.hpp:65
ystdex::details::thunk_caller::operator=
thunk_caller & operator=(const thunk_caller &)=default
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:708
ystdex::is_decayed
判断指定类型是否已退化。
Definition: type_traits.hpp:866
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
ystdex::details::thunk_caller< _tRet, decay_t< _fCallable > >::value_type
decay_t< unwrap_reference_t< _tRet >> value_type
Definition: thunk.hpp:65
ystdex::thunk::make_thunk
thunk< result_of_t< _fCallable()>, decay_t< _fCallable > > make_thunk(_fCallable &&f)
构造延迟调用对象。
Definition: thunk.hpp:166
functional.hpp
函数和可调用对象。
std
Definition: memory.hpp:1100
ystdex::unwrap_reference_t
_t< unwrap_reference< _type >> unwrap_reference_t
Definition: ref.hpp:176
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::thunk::thunk
thunk(lref< _type > arg)
Definition: thunk.hpp:126
ystdex::details::thunk_caller::caller
caller_type caller
Definition: thunk.hpp:68
type_traits.hpp
ISO C++ 类型特征扩展。
ystdex::thunk::thunk
thunk(const value_type &arg)
Definition: thunk.hpp:121
ystdex::details::thunk_call_proxy
Definition: thunk.hpp:44
ystdex::thunk::thunk
thunk(_fCaller &&f)
Definition: thunk.hpp:133
ystdex::details::thunk_caller< _tRet, decay_t< _fCallable > >::return_type
_tRet return_type
Definition: thunk.hpp:64
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:720
ystdex::details::thunk_call_proxy::arg
_type arg
Definition: thunk.hpp:46
ystdex::details::thunk_caller::thunk_caller
thunk_caller(_type arg)
Definition: thunk.hpp:83
ystdex::details::thunk_caller
Definition: thunk.hpp:57
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::thunk::return_type
_tRet return_type
Definition: thunk.hpp:64
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
ystdex::details::thunk_caller< _tRet, decay_t< _fCallable > >::caller_type
decay_t< _fCallable > caller_type
Definition: thunk.hpp:63
ystdex::thunk
包装惰性求值的过程。
Definition: thunk.hpp:108
ystdex::cpp2014::result_of_t
typename result_of< _type >::type result_of_t
Definition: type_traits.hpp:362
ystdex::is_reference_wrapper
判断模板参数指定的类型是否。
Definition: ref.hpp:150
ystdex::thunk::caller_type
_fCallable caller_type
Definition: thunk.hpp:63
ystdex::details::thunk_caller::thunk_caller
thunk_caller(caller_type f)
Definition: thunk.hpp:70