YSTest: functional.hpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
functional.hpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2012 - 2013.
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_functional_hpp_
29 #define YB_INC_ystdex_functional_hpp_ 1
30
31 #include "utility.hpp" // for ../ydef.h, ystdex::decay_forward,
32 // ystdex::variadic_sequence, ystdex::make_natural_sequence;
33 #include <functional>
34 #include <tuple>
35 #include <string> // for std::char_traits;
36
37 namespace ystdex
38 {
39
44 template<typename... _tParams>
45 yconstfn size_t
46 sizeof_params(_tParams&&...)
47 {
48 return sizeof...(_tParams);
49 }
50
51
58 template<typename _fCallable>
59 inline void
60 seq_apply(_fCallable&&)
61 {}
62 template<typename _fCallable, typename _type, typename... _tParams>
63 inline void
64 seq_apply(_fCallable&& f, _type&& arg, _tParams&&... args)
65 {
66 (ystdex::decay_forward(yforward(f)))(ystdex::decay_forward(yforward(arg))),
67 ystdex::seq_apply(yforward(f), yforward(args)...);
68 }
70
71
77 template<typename _fCallable, typename... _tParams>
78 inline void
79 unseq_apply(_fCallable&& f, _tParams&&... args)
80 {
81 yunseq(((ystdex::decay_forward(yforward(f)))(
82 ystdex::decay_forward(yforward(args))), 0)...);
83 }
84
85
92 template<typename>
93 struct make_parameter_tuple;
94
95 template<typename _tRet, typename... _tParams>
96 struct make_parameter_tuple<_tRet(_tParams...)>
97 {
98 typedef std::tuple<_tParams...> type;
99 };
100
101 template<typename _tRet, typename... _tParams>
102 struct make_parameter_tuple<_tRet(*)(_tParams...)>
103 {
104 typedef std::tuple<_tParams...> type;
105 };
106
107 template<typename _tRet, typename... _tParams>
108 struct make_parameter_tuple<_tRet(&)(_tParams...)>
109 {
110 typedef std::tuple<_tParams...> type;
111 };
112
113 template<typename _tRet, class _tClass, typename... _tParams>
114 struct make_parameter_tuple<_tRet(_tClass::*)(_tParams...)>
115 {
116 typedef std::tuple<_tParams...> type;
117 };
118
119 // !\since build 358
120 template<typename _tRet, class _tClass, typename... _tParams>
121 struct make_parameter_tuple<_tRet(_tClass::*)(_tParams...) const>
122 {
123 typedef std::tuple<_tParams...> type;
124 };
125
126 // !\since build 358
127 template<typename _tRet, class _tClass, typename... _tParams>
128 struct make_parameter_tuple<_tRet(_tClass::*)(_tParams...) volatile>
129 {
130 typedef std::tuple<_tParams...> type;
131 };
132
133 // !\since build 358
134 template<typename _tRet, class _tClass, typename... _tParams>
135 struct make_parameter_tuple<_tRet(_tClass::*)(_tParams...) const volatile>
136 {
137 typedef std::tuple<_tParams...> type;
138 };
139
141
142
149 template<typename>
150 struct return_of;
151
152 template<typename _tResult, typename... _tParams>
153 struct return_of<_tResult(_tParams...)>
154 {
155 typedef _tResult type;
156 };
157
158 template<typename _tResult, typename... _tParams>
159 struct return_of<_tResult(*)(_tParams...)>
160 {
161 typedef _tResult type;
162 };
163
164 template<typename _tResult, typename... _tParams>
165 struct return_of<_tResult(&)(_tParams...)>
166 {
167 typedef _tResult type;
168 };
169
170 template<typename _tResult, class _tClass, typename... _tParams>
171 struct return_of<_tResult(_tClass::*)(_tParams...)>
172 {
173 typedef _tResult type;
174 };
175
176 // !\since build 358
177 template<typename _tResult, class _tClass, typename... _tParams>
178 struct return_of<_tResult(_tClass::*)(_tParams...) const>
179 {
180 typedef _tResult type;
181 };
182
183 // !\since build 358
184 template<typename _tResult, class _tClass, typename... _tParams>
185 struct return_of<_tResult(_tClass::*)(_tParams...) volatile>
186 {
187 typedef _tResult type;
188 };
189
190 // !\since build 358
191 template<typename _tResult, class _tClass, typename... _tParams>
192 struct return_of<_tResult(_tClass::*)(_tParams...) const volatile>
193 {
194 typedef _tResult type;
195 };
196
198
199
205 template<size_t _vIdx, typename _fCallable>
206 struct parameter_of
207 {
208 typedef typename std::tuple_element<_vIdx, typename
209 make_parameter_tuple<_fCallable>::type>::type type;
210 };
211
212
218 template<typename _fCallable>
219 struct paramlist_size : std::integral_constant<size_t, std::tuple_size<typename
220 make_parameter_tuple<_fCallable>::type>::value>
221 {};
222
223
230 template<typename _type>
231 struct wrapped_traits : public std::false_type
232 {
233 typedef _type type;
234 };
235
236 template<typename _tWrapped>
237 struct wrapped_traits<std::reference_wrapper<_tWrapped>> : public std::true_type
238 {
239 typedef _tWrapped type;
240 };
242
243
251 template<typename _type>
252 _type&
253 unref(_type&& x) ynothrow
254 {
255 return x;
256 }
257 template<typename _type>
258 _type&
259 unref(const std::reference_wrapper<_type>& x) ynothrow
260 {
261 return x.get();
262 }
264
265
276 template<typename _type>
277 struct ref_eq
278 {
279 yconstfn bool
280 operator()(const _type& _x, const _type& _y) const
281 {
282 return &_x == &_y;
283 }
284 };
285
291 template<bool, typename _tScalar>
292 struct xcrease_t
293 {
294 inline _tScalar&
295 operator()(_tScalar& _x)
296 {
297 return ++_x;
298 }
299 };
300 template<typename _tScalar>
301 struct xcrease_t<false, _tScalar>
302 {
303 inline _tScalar&
304 operator()(_tScalar& _x)
305 {
306 return --_x;
307 }
308 };
310
317 template<bool, typename _tScalar1, typename _tScalar2>
318 struct delta_assignment
319 {
320 yconstfn _tScalar1&
321 operator()(_tScalar1& x, _tScalar2 y)
322 {
323 return x += y;
324 }
325 };
326 template<typename _tScalar1, typename _tScalar2>
327 struct delta_assignment<false, _tScalar1, _tScalar2>
328 {
329 yconstfn _tScalar1&
330 operator()(_tScalar1& x, _tScalar2 y)
331 {
332 return x -= y;
333 }
334 };
336
341 template<bool _bIsPositive, typename _tScalar>
342 yconstfn _tScalar&
343 xcrease(_tScalar& _x)
344 {
345 return xcrease_t<_bIsPositive, _tScalar>()(_x);
346 }
347
353 template<bool _bIsPositive, typename _tScalar1, typename _tScalar2>
354 yconstfn _tScalar1&
355 delta_assign(_tScalar1& _x, _tScalar2& _y)
356 {
357 return delta_assignment<_bIsPositive, _tScalar1, _tScalar2>()(_x, _y);
358 }
359
360
365 template<typename _type>
366 struct deref_op
367 {
371 yconstfn _type*
372 operator()(_type& _x) const
373 {
374 return &_x;
375 }
376 };
377
378
383 template<typename _type>
384 struct const_deref_op
385 {
389 inline const _type*
390 operator()(const _type& _x) const
391 {
392 return &_x;
393 }
394 };
395
396
403 template<typename _type, typename _tPointer = _type*,
404 class _fCompare = std::less<_type>>
405 struct deref_comp
406 {
407 /*
408 \brief 比较指针指向的对象。
409 \return 若参数有空指针则 false ，否则判断是否满足 _fCompare()(*_x, *_y) 。
410 */
411 bool
412 operator()(const _tPointer& _x, const _tPointer& _y) const
413 {
414 return bool(_x) && bool(_y) && _fCompare()(*_x, *_y);
415 }
416 };
417
418
425 template<typename _tChar, class _fCompare = std::less<_tChar>>
426 struct deref_str_comp
427 {
432 bool
433 operator()(const _tChar* x, const _tChar* y) const
434 {
435 typedef std::char_traits<_tChar> traits_type;
436
437 return x && y && std::lexicographical_compare(x, x + traits_type
438 ::length(x), y, y + traits_type::length(y), _fCompare());
439 }
440 };
441
442 } // namespace ystdex;
443
444 #endif
445