YSTest: functor.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

functor.hpp
浏览该文件的文档.
1 /*
2 © 2010-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
30 #ifndef YB_INC_ystdex_functor_hpp_
31 #define YB_INC_ystdex_functor_hpp_ 1
32
33 #include "ref.hpp" // for enable_if_t, is_detected, ystdex::constfn_addressof,
34 // not_, or_, is_reference_wrapper, and_, std::greater, std::less,
35 // std::greater_equal, std::less_equal, addrof_t, indirect_t;
36 #include <string> // for std::char_traits;
37 #include <algorithm> // for std::lexicographical_compare;
38
39 #if YB_IMPL_MSCPP >= 1900
40
45 # ifndef __cpp_lib_robust_nonmodifying_seq_ops
46 # define __cpp_lib_robust_nonmodifying_seq_ops 201304
47 # endif
48 #endif
49
50 namespace ystdex
51 {
52
54
55 namespace details
56 {
57
59 template<class _type, typename = void>
60 using mem_is_transparent_t = typename _type::is_transparent;
61
62 } // namespace details;
63
65
66
71 template<typename _type, typename _tDummy = void>
72 struct has_mem_is_transparent
73 : is_detected<details::mem_is_transparent_t, _type, _tDummy>
74 {};
75
84 template<typename _fComp, typename _tDummy, typename _type = void>
85 using enable_if_transparent_t
86 = enable_if_t<has_mem_is_transparent<_fComp, _tDummy>::value, _type>;
88
89
101 template<typename _type = void>
102 struct id
103 {
104 yconstfn _type
105 operator()(const _type& x) const ynothrow
106 {
107 return x;
108 }
109 };
110
111 template<>
112 struct id<void>
113 {
114 template<typename _type>
115 yconstfn _type
116 operator()(_type&& x) const ynothrow
117 {
118 return yforward(x);
119 }
120 };
122
123
129 template<typename _type = void>
130 struct addressof_op
131 {
132 yconstfn _type*
133 operator()(_type& x) const ynothrow
134 {
135 return ystdex::constfn_addressof(x);
136 }
137 };
138
139 template<>
140 struct addressof_op<void>
141 {
142 template<typename _type>
143 yconstfn _type*
144 operator()(_type& x) const ynothrow
145 {
146 return ystdex::constfn_addressof(x);
147 }
148 };
150
151
157 template<typename _type = void>
158 struct mem_get
159 {
160 auto
161 operator()(const _type& x) const -> decltype(x.get())
162 {
163 return yforward(x.get());
164 }
165 };
166
167 template<>
168 struct mem_get<void>
169 {
170 template<typename _type>
171 auto
172 operator()(_type&& x) const ynoexcept_spec(std::declval<_type&&>().get())
173 -> decltype(x.get())
174 {
175 return yforward(x.get());
176 }
177 };
179
185 struct is_equal
186 {
188 template<typename _type1, typename _type2>
189 yconstfn yimpl(enable_if_t)<not_<or_<is_reference_wrapper<_type1>,
190 is_reference_wrapper<_type2>>>::value, bool>
191 operator()(const _type1& x, const _type2& y) const ynoexcept_spec(x == y)
192 {
193 return x == y;
194 }
196
197 template<typename _type1, typename _type2>
198 yconstfn yimpl(enable_if_t)<and_<is_reference_wrapper<_type1>,
199 not_<is_reference_wrapper<_type2>>>::value, bool>
200 operator()(const _type1& x, const _type2& y) const ynothrow
201 {
202 return std::addressof(x.get()) == std::addressof(y);
203 }
204 template<typename _type1, typename _type2>
205 yconstfn yimpl(enable_if_t)<and_<not_<is_reference_wrapper<_type1>>,
206 is_reference_wrapper<_type2>>::value, bool>
207 operator()(const _type1& x, const _type2& y) const ynothrow
208 {
209 return std::addressof(x) == std::addressof(y.get());
210 }
211 template<typename _type1, typename _type2>
212 yconstfn yimpl(enable_if_t)<and_<is_reference_wrapper<_type1>,
213 is_reference_wrapper<_type2>>::value, bool>
214 operator()(const _type1& x, const _type2& y) const ynothrow
215 {
216 return std::addressof(x.get()) == std::addressof(y.get());
217 }
219 };
220
222 template<typename _type>
223 struct ref_eq
224 {
225 yconstfn bool
226 operator()(const _type& _x, const _type& _y) const
227 {
228 return &_x == &_y;
229 }
230 };
231
232 // NOTE: There is 'constexpr' in WG21 N3936; but it is in ISO/IEC 14882:2014.
233 #define YB_Impl_Functor_Ops_Primary(_n, _tRet, _using_stmt, _expr, ...) \
234 template<typename _type = void> \
235 struct _n \
236 { \
237 using first_argument_type = _type; \
238 _using_stmt \
239 using result_type = _tRet; \
240 \
241 yconstfn _tRet \
242 operator()(__VA_ARGS__) const \
243 { \
244 return _expr; \
245 } \
246 };
247
248 #define YB_Impl_Functor_Ops_Spec(_n, _tparams, _params, _expr) \
249 template<> \
250 struct _n<void> \
251 { \
252 using is_transparent = yimpl(void); \
253 \
254 template<_tparams> \
255 yconstfn auto \
256 operator()(_params) const -> decltype(_expr) \
257 { \
258 return _expr; \
259 } \
260 };
261
262 #define YB_Impl_Functor_Ops_Spec_Ptr(_n) \
263 template<typename _type> \
264 struct _n<_type*> \
265 { \
266 using first_argument_type = _type*; \
267 using second_argument_type = _type*; \
268 using result_type = bool; \
269 \
270 yconstfn bool \
271 operator()(_type* x, _type* y) const \
272 { \
273 return std::_n<_type*>()(x, y); \
274 } \
275 };
276
277 #define YB_Impl_Functor_Ops_using(_tRet) using second_argument_type = _tRet;
278
279 #define YB_Impl_Functor_Ops1(_n, _op, _tRet) \
280 YB_Impl_Functor_Ops_Primary(_n, _tRet, , _op x, const _type& x) \
281 \
282 YB_Impl_Functor_Ops_Spec(_n, typename _type, _type&& x, _op yforward(x))
283
284 #define YB_Impl_Functor_Ops2(_n, _op, _tRet) \
285 YB_Impl_Functor_Ops_Primary(_n, _tRet, \
286 YB_Impl_Functor_Ops_using(_tRet), x _op y, const _type& x, \
287 const _type& y) \
288 \
289 YB_Impl_Functor_Ops_Spec(_n, typename _type1 YPP_Comma typename \
290 _type2, _type1&& x YPP_Comma _type2&& y, yforward(x) _op yforward(y))
291
292 #define YB_Impl_Functor_Binary(_n, _op) \
293 YB_Impl_Functor_Ops2(_n, _op, _type)
294
295 #define YB_Impl_Functor_Unary(_n, _op) \
296 YB_Impl_Functor_Ops1(_n, _op, _type)
297
299 inline namespace cpp2014
300 {
301
302 #if __cpp_lib_transparent_operators >= 201210 || __cplusplus >= 201402L
303 using std::plus;
304 using std::minus;
305 using std::multiplies;
306 using std::divides;
307 using std::modulus;
308 using std::negate;
309
310 using std::equal_to;
311 using std::not_equal_to;
312 using std::greater;
314 using std::less;
315 using std::greater_equal;
316 using std::less_equal;
317
318 using std::logical_and;
319 using std::logical_or;
320 using std::logical_not;
321
322 using std::bit_and;
323 using std::bit_xor;
324 using std::bit_not;
325 #else
326
331 YB_Impl_Functor_Binary(plus, +)
333
334 YB_Impl_Functor_Binary(minus, -)
336
337 YB_Impl_Functor_Binary(multiplies, *)
339
340
344 YB_Impl_Functor_Binary(divides, /)
345
347 YB_Impl_Functor_Binary(modulus, %)
348
353 YB_Impl_Functor_Unary(negate, -)
354
355 #define YB_Impl_Functor_bool(_n, _op) \
356 YB_Impl_Functor_Ops2(_n, _op, bool)
357
358 #define YB_Impl_Functor_bool_Ordered(_n, _op) \
359 YB_Impl_Functor_bool(_n, _op) \
360 \
361 YB_Impl_Functor_Ops_Spec_Ptr(_n)
362
364 YB_Impl_Functor_bool(equal_to, ==)
365
366 YB_Impl_Functor_bool(not_equal_to, !=)
368
370 YB_Impl_Functor_bool_Ordered(greater, >)
371
373 YB_Impl_Functor_bool_Ordered(less, <)
374
376 YB_Impl_Functor_bool_Ordered(greater_equal, >=)
377
379 YB_Impl_Functor_bool_Ordered(less_equal, <=)
380
382
383 YB_Impl_Functor_bool(logical_and, &&)
385
387 YB_Impl_Functor_bool(logical_or, ||)
388
390 YB_Impl_Functor_Ops1(logical_not, !, bool)
391
393 YB_Impl_Functor_Binary(bit_and, &)
394
396 YB_Impl_Functor_Binary(bit_or, |)
397
399 YB_Impl_Functor_Binary(bit_xor, ^)
400
402 // NOTE: Available in %std since ISO C++14.
403 YB_Impl_Functor_Unary(bit_not, ~)
405
406 #endif
407
408 } // inline namespace cpp2014;
409
415 YB_Impl_Functor_Ops1(addrof, &, addrof_t<const _type&>)
417
418 YB_Impl_Functor_Ops1(indirect, *, indirect_t<const _type&>)
421
422 #undef YB_Impl_Functor_bool_Ordered
423 #undef YB_Impl_Functor_bool
424 #undef YB_Impl_Functor_Unary
425 #undef YB_Impl_Functor_Binary
426 #undef YB_Impl_Functor_Ops2
427 #undef YB_Impl_Functor_Ops1
428 #undef YB_Impl_Functor_Ops_using
429 #undef YB_Impl_Functor_Ops_Spec
430 #undef YB_Impl_Functor_Ops_Primary
431
432
434
435 template<bool, typename _tScalar>
436 struct xcrease_t
437 {
439 inline _tScalar&
440 operator()(_tScalar& _x) const
441 {
442 return ++_x;
443 }
444 };
445 template<typename _tScalar>
446 struct xcrease_t<false, _tScalar>
447 {
449 inline _tScalar&
450 operator()(_tScalar& _x) const
451 {
452 return --_x;
453 }
454 };
456
462 template<bool, typename _tScalar1, typename _tScalar2>
463 struct delta_assignment
464 {
465 yconstfn _tScalar1&
466 operator()(_tScalar1& x, _tScalar2 y) const
467 {
468 return x += y;
469 }
470 };
471 template<typename _tScalar1, typename _tScalar2>
472 struct delta_assignment<false, _tScalar1, _tScalar2>
473 {
474 yconstfn _tScalar1&
475 operator()(_tScalar1& x, _tScalar2 y) const
476 {
477 return x -= y;
478 }
479 };
481
483 template<bool _bIsPositive, typename _tScalar>
484 yconstfn _tScalar&
485 xcrease(_tScalar& _x)
486 {
487 return xcrease_t<_bIsPositive, _tScalar>()(_x);
488 }
489
494 template<bool _bIsPositive, typename _tScalar1, typename _tScalar2>
495 yconstfn _tScalar1&
496 delta_assign(_tScalar1& _x, _tScalar2& _y)
497 {
498 return delta_assignment<_bIsPositive, _tScalar1, _tScalar2>()(_x, _y);
499 }
501
502 } // namespace ystdex;
503
504 #endif
505
YSLib::Drawing::GBinaryGroup::get
yconstfn _type & get(GBinaryGroup< _type > &val)
取分量。
Definition: YGDIBase.h:242
ystdex::mem_get::operator()
auto operator()(const _type &x) const -> decltype(x.get())
Definition: functor.hpp:161
ystdex::indirect
一元 * 操作。
Definition: functor.hpp:419
ystdex::xcrease_t
选择自增/自减运算仿函数。
Definition: functor.hpp:436
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
ystdex::details::mem_is_transparent_t
typename _type::is_transparent mem_is_transparent_t
Definition: functor.hpp:60
ystdex::delta_assign
_tScalar1 & delta_assign(_tScalar1 &_x, _tScalar2 &_y)
选择加法/减法复合赋值运算。
Definition: functor.hpp:496
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::is_equal::operator()
enable_if_t< and_< not_< is_reference_wrapper< _type1 > >, is_reference_wrapper< _type2 > >::value, bool > operator()(const _type1 &x, const _type2 &y) const
Definition: functor.hpp:207
ystdex::is_detected
typename details::detector< nonesuch, void, _gOp, _tParams... >::value_t is_detected
Definition: type_traits.hpp:667
ystdex::delta_assignment
选择加法/减法复合赋值运算仿函数。
Definition: functor.hpp:463
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:350
ystdex::id< void >::operator()
_type operator()(_type &&x) const
Definition: functor.hpp:116
ystdex::cpp2014::greater
大于关系仿函数。
Definition: functor.hpp:370
ystdex::indirect_t
decltype(*std::declval< _type >()) indirect_t
Definition: type_traits.hpp:978
ystdex::cpp2014::greater_equal
大于等于关系仿函数。
Definition: functor.hpp:376
ystdex::cpp2014::bit_or
位或仿函数。
Definition: functor.hpp:396
ystdex::xcrease_t< false, _tScalar >::operator()
_tScalar & operator()(_tScalar &_x) const
Definition: functor.hpp:450
ystdex::cpp2014::bit_xor
位异或仿函数。
Definition: functor.hpp:399
ystdex::is_equal
相等关系仿函数。
Definition: functor.hpp:185
ystdex::delta_assignment::operator()
_tScalar1 & operator()(_tScalar1 &x, _tScalar2 y) const
Definition: functor.hpp:466
ystdex::cpp2014::equal_to
等于关系仿函数。
Definition: functor.hpp:364
ystdex::id::operator()
_type operator()(const _type &x) const
Definition: functor.hpp:105
ystdex::cpp2014::negate
取反仿函数。
Definition: functor.hpp:353
ystdex::addressof_op< void >::operator()
_type * operator()(_type &x) const
Definition: functor.hpp:144
ref.hpp
引用包装。
ystdex::cpp2014::not_equal_to
不等于关系仿函数。
Definition: functor.hpp:367
YB_Impl_Functor_Unary
#define YB_Impl_Functor_Unary(_n, _op)
Definition: functor.hpp:295
ystdex::delta_assignment< false, _tScalar1, _tScalar2 >::operator()
_tScalar1 & operator()(_tScalar1 &x, _tScalar2 y) const
Definition: functor.hpp:475
ystdex::addressof_op::operator()
_type * operator()(_type &x) const
Definition: functor.hpp:133
YB_Impl_Functor_bool
#define YB_Impl_Functor_bool(_n, _op)
Definition: functor.hpp:355
YB_Impl_Functor_Binary
#define YB_Impl_Functor_Binary(_n, _op)
Definition: functor.hpp:292
ystdex::cpp2014::less
小于关系仿函数。
Definition: functor.hpp:373
YB_Impl_Functor_bool_Ordered
#define YB_Impl_Functor_bool_Ordered(_n, _op)
Definition: functor.hpp:358
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
ynoexcept_spec
#define ynoexcept_spec(...)
表达式 noexcept 异常规范。
Definition: ydef.h:768
ystdex::constfn_addressof
enable_if_t<!has_overloaded_addressof< _type >::value, _type * > constfn_addressof(_type &r)
尝试对非重载 operator& 提供 constexpr 的 std::addressof 替代。
Definition: addressof.hpp:78
ystdex::enable_if_transparent_t
enable_if_t< has_mem_is_transparent< _fComp, _tDummy >::value, _type > enable_if_transparent_t
移除不满足包含 is_transparent 成员类型比较器的重载。
Definition: functor.hpp:86
ystdex::xcrease_t::operator()
_tScalar & operator()(_tScalar &_x) const
Definition: functor.hpp:440
std
Definition: memory.hpp:1100
ystdex::id
恒等仿函数。
Definition: functor.hpp:102
ystdex::cpp2014::bit_not
位取反仿函数。
Definition: functor.hpp:403
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::xcrease
_tScalar & xcrease(_tScalar &_x)
选择自增/自减运算。
Definition: functor.hpp:485
ystdex::cpp2014::less_equal
小于等于关系仿函数。
Definition: functor.hpp:379
ystdex::mem_get
成员 get 操作。
Definition: functor.hpp:158
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
ystdex::cpp2014::bit_and
位与仿函数。
Definition: functor.hpp:393
ystdex::cpp2014::logical_or
逻辑或仿函数。
Definition: functor.hpp:387
ystdex::ref_eq
引用相等关系仿函数。
Definition: functor.hpp:223
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
ystdex::mem_get< void >::operator()
auto operator()(_type &&x) const -> decltype(x.get())
Definition: functor.hpp:172
ystdex::cpp2014::modulus
模运算仿函数。
Definition: functor.hpp:347
YB_Impl_Functor_Ops1
#define YB_Impl_Functor_Ops1(_n, _op, _tRet)
Definition: functor.hpp:279
ystdex::is_equal::operator()
enable_if_t< and_< is_reference_wrapper< _type1 >, is_reference_wrapper< _type2 > >::value, bool > operator()(const _type1 &x, const _type2 &y) const
Definition: functor.hpp:214
ystdex::addrof
一元 & 操作。
Definition: functor.hpp:416
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::cpp2014::logical_not
逻辑非仿函数。
Definition: functor.hpp:390
ystdex::is_equal::operator()
enable_if_t< not_< or_< is_reference_wrapper< _type1 >, is_reference_wrapper< _type2 > > >::value, bool > operator()(const _type1 &x, const _type2 &y) const
Definition: functor.hpp:191
ystdex::ref_eq::operator()
bool operator()(const _type &_x, const _type &_y) const
Definition: functor.hpp:226
ystdex::addressof_op
std::addressof 仿函数。
Definition: functor.hpp:130
ystdex::is_equal::operator()
enable_if_t< and_< is_reference_wrapper< _type1 >, not_< is_reference_wrapper< _type2 > > >::value, bool > operator()(const _type1 &x, const _type2 &y) const
Definition: functor.hpp:200
ystdex::and_
逻辑操作元函数。
Definition: type_traits.hpp:394
ystdex::has_mem_is_transparent
判断 _type 是否包含 is_transparent 成员类型。
Definition: functor.hpp:72
ystdex::cpp2014::logical_and
逻辑与仿函数。
Definition: functor.hpp:384
ystdex::is_reference_wrapper
判断模板参数指定的类型是否。
Definition: ref.hpp:150
ystdex::not_
Definition: type_traits.hpp:430
ystdex::addrof_t
decltype(&std::declval< _type >()) addrof_t
Definition: type_traits.hpp:975
ystdex::cpp2014::divides
除法仿函数。
Definition: functor.hpp:344