YSTest: YFunc.hpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YFunc.hpp
浏览该文件的文档.
1 /*
2 © 2010-2016, 2018-2020 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 YSL_INC_Core_YFunc_hpp_
29 #define YSL_INC_Core_YFunc_hpp_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YShellDefinition
33 #include <ystdex/functional.hpp> // for <ystdex/functional.hpp>,
34 // ystdex::function;
35 #include YFM_YSLib_Adaptor_YTextBase // for string;
36
37 namespace YSLib
38 {
39
41 using ystdex::function;
42
43
49 template<typename _tKey, typename _type>
50 class GRecursiveCallContext
51 {
52 public:
53 using KeyType = _tKey;
54 using ValueType = _type;
55 using CallerType = function<ValueType(const GRecursiveCallContext&)>;
56
57 private:
58 function<CallerType(const KeyType&)> build;
59
60 public:
61 template<typename _func, yimpl(typename
62 = ystdex::exclude_self_t<GRecursiveCallContext, _func>)>
63 GRecursiveCallContext(_func f)
64 : build(f)
65 {}
66 DefDeCopyMoveCtorAssignment(GRecursiveCallContext)
67
68 PDefHOp(ValueType, (), const KeyType& key) const
69 ImplRet(build(key)(*this))
70 };
71
72
79 template<class _tDerived, typename _tKey, typename _fHandler>
80 struct GHandlerRegisterBase
81 {
82 private:
83 unordered_map<_tKey, _fHandler> registered_map{};
84
85 public:
87 YB_ATTR_nodiscard YB_PURE PDefH(bool, Contains, const _tKey& key) const
88 ImplRet(registered_map.count(key) != 0)
89
90 DeclSEntry(template<_type, _fHandler> _fHandler GetRegister() const)
91
93 template<typename... _tParams>
94 auto
95 Invoke(const _tKey& key, _tParams&&... args) const
96 -> ystdex::invoke_result_t<_fHandler&, _tParams&&...>
97 {
98 const auto i(registered_map.find(key));
99
100 if(i != registered_map.cend())
101 if(const auto& f = i->second)
102 {
103 // TODO: Do right trace?
104 // YTraceDe(Notice, "Found registered handler: %s.",
105 // to_string(key).c_str());
106 return ystdex::invoke(f, yforward(args)...);
107 }
108 return ystdex::invoke_result_t<_fHandler&, _tParams&&...>();
109 }
110
111 template<class _type>
112 void
113 Register(const _tKey& key)
114 {
115 registered_map.emplace(key, static_cast<_tDerived*>(this)
116 ->template GetRegister<_type, _fHandler>(key));
117 }
118 template<typename _tIn, class _type, class _tTuple>
119 void
120 Register(_tIn first, _tIn last)
121 {
122 YAssert(first != last && std::distance(first, last)
123 == decltype(std::distance(first, last))(
124 std::tuple_size<_tTuple>::value + 1), "Wrong range found.");
125 Register<_type>(*first);
126 ++first;
127 YAssert((first == last) == (std::tuple_size<_tTuple>::value == 0),
128 "Wrong number of parameters found.");
129 // static_if(std::tuple_size<_tTuple>::value != 0)
130 // RegisterTail<_tIn, std::tuple_element<0, _tTuple>,
131 // typename tuple_split<_tTuple>::tail>(first, last);
132 RegisterTail<_tIn>(ystdex::identity<_tTuple>(), first, last);
133 }
134 template<class _type, class... _types>
135 void
136 Register(std::initializer_list<string> il)
137 {
138 YAssert(il.size() == sizeof...(_types) + 1,
139 "Wrong size of initializer list found.");
140 Register<std::initializer_list<string>::const_iterator, _type,
141 tuple<_types...>>(il.begin(), il.end());
142 }
143
144 private:
146 template<typename _tIn>
147 void
148 RegisterTail(ystdex::identity<tuple<>>, _tIn first, _tIn last)
149 {
150 YAssert(first == last, "Wrong size of initializer list found.");
151 yunused(first),
152 yunused(last);
153 }
155 template<typename _tIn, class _type, class... _types>
156 void
157 RegisterTail(ystdex::identity<tuple<_type, _types...>>, _tIn first,
158 _tIn last)
159 {
160 Register<_tIn, _type, tuple<_types...>>(first, last);
161 }
162 };
163
164 } // namespace YSLib;
165
166 #endif
167
YSLib::GRecursiveCallContext::DefDeCopyMoveCtorAssignment
DefDeCopyMoveCtorAssignment(GRecursiveCallContext) PDefHOp(ValueType
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
YSLib::GRecursiveCallContext::KeyType
_tKey KeyType
Definition: YFunc.hpp:53
YSLib::GHandlerRegisterBase
注册处理器抽象模板：供派生类加载一个或多个键和指定类型关联的例程。
Definition: YFunc.hpp:81
ystdex::invoke
invoke_result_t< _fCallable, _tParams... > invoke(_fCallable &&f, _tParams &&... args)
调用可调用对象。
Definition: invoke.hpp:538
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
YSLib::GHandlerRegisterBase::GetRegister
YB_ATTR_nodiscard YB_PURE _fHandler _fHandler GetRegister() const) template< typename... _tParams > auto Invoke(const _tKey &key
ystdex::any_ops::distance
@ distance
Definition: any_iterator.hpp:82
YSLib::GHandlerRegisterBase::registered_map
unordered_map< _tKey, _fHandler > registered_map
Definition: YFunc.hpp:83
CHRLib::f
f(d, s)
YSLib::GHandlerRegisterBase::Register
void Register(_tIn first, _tIn last)
Definition: YFunc.hpp:120
YSLib::GRecursiveCallContext::CallerType
function< ValueType(const GRecursiveCallContext &)> CallerType
Definition: YFunc.hpp:55
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
ystdex::identity
恒等元函数。
Definition: meta.hpp:815
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
YSLib::GRecursiveCallContext
递归调用上下文。
Definition: YFunc.hpp:51
platform::containers::unordered_map
std::unordered_map< _tKey, _tMapped, _fHash, _fEqual, _tAlloc > unordered_map
Definition: Container.h:133
functional.hpp
函数和可调用对象。
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
YSLib::i
return i
Definition: YCoreUtilities.h:177
YSLib::GHandlerRegisterBase::RegisterTail
void RegisterTail(ystdex::identity< tuple< _type, _types... >>, _tIn first, _tIn last)
Definition: YFunc.hpp:157
YSLib::GRecursiveCallContext::build
function< CallerType(const KeyType &)> build
Definition: YFunc.hpp:58
ystdex::cpp2017::invoke_result_t
_t< invoke_result< _fCallable, _tParams... > > invoke_result_t
Definition: invoke.hpp:393
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YModules.h
YSLib 用户界面模块配置文件。
ystdex::exclude_self_t
enable_if_t<!is_same_param< _tSelected, _tParam >::value, _type > exclude_self_t
排除选择类型的特定参数的重载。
Definition: meta.hpp:865
YSLib::GHandlerRegisterBase::Register
void Register(std::initializer_list< string > il)
Definition: YFunc.hpp:136
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
DeclSEntry
#define DeclSEntry(...)
静态接口。
Definition: YBaseMacro.h:413
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
YSLib::GHandlerRegisterBase::PDefH
YB_ATTR_nodiscard YB_PURE PDefH(bool, Contains, const _tKey &key) const ImplRet(registered_map.count(key) !=0) DeclSEntry(template< _type
YSLib::GHandlerRegisterBase::Register
_tParams &&void Register(const _tKey &key)
Definition: YFunc.hpp:113
YSLib::GHandlerRegisterBase::RegisterTail
void RegisterTail(ystdex::identity< tuple<>>, _tIn first, _tIn last)
Definition: YFunc.hpp:148
ystdex::function
function_base< function_traits< _fSig, empty_function_policy::throwing >, _fSig > function
函数包装类模板。
Definition: function.hpp:900
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
yunused
#define yunused(...)
标记未使用的表达式。
Definition: ydef.h:1458
YSLib::args
YB_ATTR_nodiscard YB_PURE typename _tParams && args
Definition: ValueNode.h:1299
YSLib::GRecursiveCallContext::GRecursiveCallContext
GRecursiveCallContext(_func f)
Definition: YFunc.hpp:63
YSLib::GRecursiveCallContext::ValueType
_type ValueType
Definition: YFunc.hpp:54