28 #ifndef YSL_INC_Core_yfunc_hpp_
29 #define YSL_INC_Core_yfunc_hpp_ 1
32 #include YFM_YSLib_Core_YShellDefinition
35 #include YFM_YSLib_Adaptor_YContainer
44 template<
typename _type,
typename _tArg,
typename _tRet>
45 class GHDynamicFunction
51 _tRet (*_f_ptr)(_type&, _tArg);
52 _tRet (_type::*_mf_ptr)(_tArg)
const;
58 Pointer(_tRet (&_f_)(_type&, _tArg))
65 Pointer(_tRet(_type::*_mf_ptr_)(_tArg)
const)
80 GHDynamicFunction(_tRet(&_f)(_type&, _tArg))
81 : _m_ptr(_f), _state(_func)
87 GHDynamicFunction(_tRet(_type::*_pf)(_tArg)
const)
88 : _m_ptr(_pf), _state(_mem_func)
96 operator()(_type& _r, _tArg _x)
const
99 return _m_ptr._f_ptr(_r, _x);
101 else if(_m_ptr._mf_ptr)
102 return (_r.*_m_ptr._mf_ptr)(_x);
108 template<
class _tNew>
110 operator()(
const _tNew& _r, _tArg _x)
const
112 if(_state == _mem_func && _m_ptr._mf_ptr)
116 return dynamic_cast<_type&
>(_r).*_m_ptr._mf_ptr(_x);
118 catch(std::bad_cast&)
128 template<
typename _type,
typename _tArg,
typename _tRet>
129 inline GHDynamicFunction<_type, _tArg, _tRet>
130 ConstructDynamicFunctionWith(_tRet (&_f)(_type&, _tArg))
132 return GHDynamicFunction<_type, _tArg, _tRet>(_f);
138 template<
typename _tRet,
typename _type,
typename _tArg>
139 inline GHDynamicFunction<_tRet, _type, _tArg>
140 ConstructDynamicFunctionWith(_tRet (_type::*_f)(_tArg)
const)
142 return GHDynamicFunction<_tRet, _type, _tArg>(_f);
151 template<
class _type,
typename _tRet,
typename _tPara,
class _tNew = _type>
155 _tRet(_type::*
_pm)(_tPara);
173 return _pm == rhs._pm;
197 return (dynamic_cast<_type&>(o).*
_pm)(
yforward(arg));
199 catch(std::bad_cast&)
209 template<
class _type,
typename _tRet,
typename _tPara,
class _tNew = _type>
214 _tRet(_type::*
_pm)(_tPara);
223 :
_po(dynamic_cast<_type*>(&obj)),
_pm(p)
233 return _po == rhs._po &&
_pm == rhs._pm;
263 template<
class _fCallable,
typename _tParm>
265 struct InversedCurrying
267 using Result =
typename _fCallable::Result;
268 using Parm1 =
typename _fCallable::Parm1;
275 InversedCurrying(_fCallable f_)
279 PDefHOp(
bool, ==,
const InversedCurrying&
r)
const
286 operator()(_tParm, Parm1 arg1)
const
294 struct PolymorphicFunctorBase
304 template<
class _tFunctor>
305 class GFunctor :
public PolymorphicFunctorBase,
public _tFunctor
308 GFunctor(_tFunctor&& _f)
309 : PolymorphicFunctorBase(), _tFunctor(
yforward(_f))
312 template<
typename... _tArgs>
313 GFunctor(_tArgs&&... _args)
314 : PolymorphicFunctorBase(),
327 template<
class _tDerived,
typename _tKey,
typename _fHandler>
340 template<typename... _tParams>
342 Call(const _tKey& key, _tParams&&...
args)
356 template<
class _type>
361 ->
template GetRegister<_type, _fHandler>(key));
363 template<
typename _tIn,
class _type,
class _tTuple>
368 == std::tuple_size<_tTuple>::value + 1,
"Wrong range found.");
369 Register<_type>(*first);
371 YAssert((first == last) == (std::tuple_size<_tTuple>::value == 0),
372 "Wrong number of parameters found.");
376 RegisterTail<_tIn>(
static_cast<_tTuple*
>(
nullptr), first, last);
378 template<
class _type,
class... _types>
382 YAssert(il.size() ==
sizeof...(_types) + 1,
383 "Wrong size of initializer list found.");
384 Register<std::initializer_list<string>::const_iterator, _type,
385 tuple<_types...>>(il.begin(), il.end());
389 template<
typename _tIn>
393 YAssert(first == last,
"Wrong size of initializer list found.");
397 template<
typename _tIn,
class _type,
class... _types>
PDefH(bool, Contains, const _tKey &key) const ImplRet(registered_map.count(key)!=0) DeclSEntry(template< _type
void Register(std::initializer_list< string > il)
#define DefDeDtor(_t)
定义默认析构函数。
static auto first(const _tIterator &i) -> decltype((i->first))
yconstfn ExpandMemberFirst(_tRet(_type::*p)(_tPara))
构造:使用函数指针。
#define yunused(...)
标记未使用的表达式。
typename result_of< _type >::type result_of_t
yconstfn const string _tParams && args
void RegisterTail(tuple< _type, _types...> *, _tIn first, _tIn last)
_fHandler _fHandler GetRegister() const ) template< typename..._tParams > auto Call(const _tKey &key
_tRet(_type::* _pm)(_tPara)
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
yconstfn ExpandMemberFirstBinder(_tNew &obj, _tRet(_type::*p)(_tPara))
构造:使用非 _type 类型对象引用和成员函数指针。
yconstfn bool operator==(const ExpandMemberFirst &rhs) const ynothrow
比较:相等关系。
_tRet operator()(_type &o, _tPara arg)
调用:使用对象引用和参数。
void RegisterTail(tuple<> *, _tIn first, _tIn last)
void Register(const _tKey &key)
_tRet operator()(_tPara &&arg)
调用:使用替换对象引用和参数。
yconstfn bool operator==(const ExpandMemberFirstBinder &rhs) const ynothrow
比较:相等关系。
_tRet(_type::* _pm)(_tPara)
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
_tRet operator()(_tN &, _tPara &&arg)
调用:使用替换对象引用和参数。
#define DeclSEntry(...)
静态接口。
注册处理器抽象模板:供派生类加载一个或多个键和指定类型关联的例程。
#define yconstfn
指定编译时常量函数。
Selected const shared_ptr< ListType > const pair< Color, Color > viewer Contains
void Register(_tIn first, _tIn last)
仿函数:替换非静态成员二元函数的第一个参数并绑定到指定对象。
_tRet operator()(_tNew &o, _tPara &&arg)
调用:使用非 _type 类型对象引用和参数。
#define YAssert(_expr, _msg)
unordered_map< _tKey, _fHandler > registered_map