YSTest: yfunc.hpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
yfunc.hpp
浏览该文件的文档.
1 ﻿/*
2 Copyright (C) by Franksoft 2010 - 2012.
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 INCLUDED_CORE_YFUNC_HPP_
29 #define INCLUDED_CORE_YFUNC_HPP_ 1
30
31 #include "ysdef.h"
32 #include <functional>
33 #include <typeinfo>
34
35 YSL_BEGIN
36
37 #if 0
38 /*
39 \brief 调用时动态类型检查仿函数模板。
40 */
41 template<typename _type, typename _tArg, typename _tRet>
42 class GHDynamicFunction
43 {
44 private:
45 const union Pointer
46 {
47 public:
48 _tRet (*_f_ptr)(_type&, _tArg);
49 _tRet (_type::*_mf_ptr)(_tArg) const;
50
54 explicit
55 Pointer(_tRet (&_f_)(_type&, _tArg))
56 : _f_ptr(&_f_)
57 {}
61 explicit
62 Pointer(_tRet(_type::*_mf_ptr_)(_tArg) const)
63 : _mf_ptr(_mf_ptr_)
64 {}
65 } _m_ptr;
66 const enum
67 {
68 _func = 1,
69 _mem_func = 2
70 } _state;
71
72 public:
76 explicit
77 GHDynamicFunction(_tRet(&_f)(_type&, _tArg))
78 : _m_ptr(_f), _state(_func)
79 {}
83 explicit
84 GHDynamicFunction(_tRet(_type::*_pf)(_tArg) const)
85 : _m_ptr(_pf), _state(_mem_func)
86 {}
87
92 _tRet
93 operator()(_type& _r, _tArg _x) const
94 {
95 if(_state == _func)
96 return _m_ptr._f_ptr(_r, _x);
97 // if(_state == _mem_func && _m_ptr._mf_ptr)
98 else if(_m_ptr._mf_ptr)
99 return (_r.*_m_ptr._mf_ptr)(_x);
100 }
105 template<class _tNew>
106 _tRet
107 operator()(const _tNew& _r, _tArg _x) const
108 {
109 if(_state == _mem_func && _m_ptr._mf_ptr)
110 {
111 try
112 {
113 return dynamic_cast<_type&>(_r).*_m_ptr._mf_ptr(_x);
114 }
115 catch(std::bad_cast&)
116 {}
117 }
118 }
119 };
120
121
125 template<typename _type, typename _tArg, typename _tRet>
126 inline GHDynamicFunction<_type, _tArg, _tRet>
127 ConstructDynamicFunctionWith(_tRet (&_f)(_type&, _tArg))
128 {
129 return GHDynamicFunction<_type, _tArg, _tRet>(_f);
130 }
135 template<typename _tRet, typename _type, typename _tArg>
136 inline GHDynamicFunction<_tRet, _type, _tArg>
137 ConstructDynamicFunctionWith(_tRet (_type::*_f)(_tArg) const)
138 {
139 return GHDynamicFunction<_tRet, _type, _tArg>(_f);
140 }
141 #endif
142
143
148 template<class _type, typename _tRet, typename _tPara, class _tNew = _type>
149 struct ExpandMemberFirst
150 {
151 private:
152 _tRet(_type::*_pm)(_tPara);
153
154 public:
158 yconstfn
159 ExpandMemberFirst(_tRet(_type::*p)(_tPara))
160 : _pm(p)
161 {}
162
167 yconstfn bool
168 operator==(const ExpandMemberFirst& rhs) const ynothrow
169 {
170 return _pm == rhs._pm;
171 }
172
177 _tRet
178 operator()(_type& o, _tPara arg)
179 {
180 if(YB_LIKELY(_pm))
181 return o.*_pm(arg);
182 }
188 _tRet
189 operator()(_tNew& o, _tPara&& arg)
190 {
191 if(YB_LIKELY(_pm))
192 try
193 {
194 return (dynamic_cast<_type&>(o).*_pm)(yforward(arg));
195 }
196 catch(std::bad_cast&)
197 {}
198 }
199 };
200
201
206 template<class _type, typename _tRet, typename _tPara, class _tNew = _type>
207 struct ExpandMemberFirstBinder
208 {
209 private:
210 _type* _po;
211 _tRet(_type::*_pm)(_tPara);
212
213 public:
218 yconstfn
219 ExpandMemberFirstBinder(_tNew& obj, _tRet(_type::*p)(_tPara))
220 : _po(dynamic_cast<_type*>(&obj)), _pm(p)
221 {}
222
227 yconstfn bool
228 operator==(const ExpandMemberFirstBinder& rhs) const ynothrow
229 {
230 return _po == rhs._po && _pm == rhs._pm;
231 }
232
238 _tRet
239 operator()(_tPara&& arg)
240 {
241 if(YB_LIKELY(_po && _pm))
242 return (_po->*_pm)(yforward(arg));
243 }
249 template<class _tN>
250 _tRet
251 operator()(_tN&, _tPara&& arg)
252 {
253 if(YB_LIKELY(_po && _pm))
254 return (_po->*_pm)(yforward(arg));
255 }
256 };
257
258
259 #if 0
260
261 template<class _fCallable, typename _tParm>
262 struct InversedCurrying
263 {
264 typedef typename _fCallable::Result Result;
265 typedef typename _fCallable::Parm1 Parm1;
266
267 _fCallable f;
268
272 InversedCurrying(_fCallable f_)
273 : f(f_)
274 {}
275
276 PDefHOp(bool, ==, const InversedCurrying& r) const
277 ImplRet(f == r.f)
278
279
282 Result
283 operator()(_tParm, Parm1 arg1) const
284 {
285 return f(arg1);
286 }
287 };
288
289
291 struct PolymorphicFunctorBase
292 {
293 DefDeDtor(PolymorphicFunctorBase)
294 };
295
296
297 /*
298 \brief 多态仿函数模板。
299 \note 继承其它仿函数。
300 */
301 template<class _tFunctor>
302 class GFunctor : public PolymorphicFunctorBase, public _tFunctor
303 {
304 public:
305 GFunctor(_tFunctor&& _f)
306 : PolymorphicFunctorBase(), _tFunctor(yforward(_f))
307 {}
308
309 template<typename... _tArgs>
310 GFunctor(_tArgs&&... _args)
311 : PolymorphicFunctorBase(),
312 _tFunctor(yforward(_args)...)
313 {}
314 };
315 #endif
316
317 YSL_END
318
319 #endif
320