YSTest: YFunc.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

YFunc.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
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 YFM_YSLib_Adaptor_YTextBase // for string;
34
35 namespace YSLib
36 {
37
43 template<typename _tKey, typename _tValue>
44 class GRecursiveCallContext
45 {
46 public:
47 using KeyType = _tKey;
48 using ValueType = _tValue;
49 using CallerType = std::function<ValueType(const GRecursiveCallContext&)>;
50
51 private:
52 std::function<CallerType(const KeyType&)> build;
53
54 public:
55 template<typename _func, yimpl(typename
56 = ystdex::exclude_self_t<GRecursiveCallContext, _func>)>
57 GRecursiveCallContext(_func f)
58 : build(f)
59 {}
60 DefDeCopyMoveCtorAssignment(GRecursiveCallContext)
61
62 PDefHOp(ValueType, (), const KeyType& key) const
63 ImplRet(build(key)(*this))
64 };
65
66
73 template<class _tDerived, typename _tKey, typename _fHandler>
74 struct GHandlerRegisterBase
75 {
76 private:
77 unordered_map<_tKey, _fHandler> registered_map{};
78
79 public:
81 PDefH(bool, Contains, const _tKey& key) const
82 ImplRet(registered_map.count(key) != 0)
83
84 DeclSEntry(template<_type, _fHandler> _fHandler GetRegister() const)
85
86 template<typename... _tParams>
87 auto
88 Call(const _tKey& key, _tParams&&... args)
89 -> ystdex::result_of_t<_fHandler&(_tParams&&...)>
90 {
91 if(const auto f = registered_map[key])
92 {
93 // TODO: Do right trace.
94 // YTraceDe(Notice, "Found registered handler: %s.",
95 // to_string(key).c_str());
96
97 return f(yforward(args)...);
98 }
99 return ystdex::result_of_t<_fHandler&(_tParams&&...)>();
100 }
101
102 template<class _type>
103 void
104 Register(const _tKey& key)
105 {
106 registered_map.emplace(key, static_cast<_tDerived*>(this)
107 ->template GetRegister<_type, _fHandler>(key));
108 }
109 template<typename _tIn, class _type, class _tTuple>
110 void
111 Register(_tIn first, _tIn last)
112 {
113 YAssert(first != last && std::distance(first, last)
114 == std::tuple_size<_tTuple>::value + 1, "Wrong range found.");
115 Register<_type>(*first);
116 ++first;
117 YAssert((first == last) == (std::tuple_size<_tTuple>::value == 0),
118 "Wrong number of parameters found.");
119 // static_if(std::tuple_size<_tTuple>::value != 0)
120 // RegisterTail<_tIn, std::tuple_element<0, _tTuple>,
121 // typename tuple_split<_tTuple>::tail>(first, last);
122 RegisterTail<_tIn>(ystdex::identity<_tTuple>(), first, last);
123 }
124 template<class _type, class... _types>
125 void
126 Register(std::initializer_list<string> il)
127 {
128 YAssert(il.size() == sizeof...(_types) + 1,
129 "Wrong size of initializer list found.");
130 Register<std::initializer_list<string>::const_iterator, _type,
131 tuple<_types...>>(il.begin(), il.end());
132 }
133
134 private:
136 template<typename _tIn>
137 void
138 RegisterTail(ystdex::identity<tuple<>>, _tIn first, _tIn last)
139 {
140 YAssert(first == last, "Wrong size of initializer list found.");
141 yunused(first),
142 yunused(last);
143 }
145 template<typename _tIn, class _type, class... _types>
146 void
147 RegisterTail(ystdex::identity<tuple<_type, _types...>>, _tIn first,
148 _tIn last)
149 {
150 Register<_tIn, _type, tuple<_types...>>(first, last);
151 }
152 };
153
154 } // namespace YSLib;
155
156 #endif
157
YSLib::GHandlerRegisterBase::Register
void Register(std::initializer_list< string > il)
Definition: YFunc.hpp:126
YSLib::GRecursiveCallContext::CallerType
std::function< ValueType(const GRecursiveCallContext &)> CallerType
Definition: YFunc.hpp:49
YSLib::GHandlerRegisterBase
注册处理器抽象模板：供派生类加载一个或多个键和指定类型关联的例程。
Definition: YFunc.hpp:74
YSLib::GHandlerRegisterBase::Register
void Register(_tIn first, _tIn last)
Definition: YFunc.hpp:111
YModules.h
YSLib::GHandlerRegisterBase::RegisterTail
void RegisterTail(ystdex::identity< tuple< _type, _types... >>, _tIn first, _tIn last)
Definition: YFunc.hpp:147
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
ystdex::identity
恒等元函数。
Definition: type_traits.hpp:961
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::GRecursiveCallContext::ValueType
_tValue ValueType
Definition: YFunc.hpp:48
YSLib::PDefH
PDefH(Environment &, FetchEnvironment,) ImplRet(FetchGlobalInstance().GetEnvironmentRef()) inline PDefH(GUIHost &
取单一环境对象引用。
YSLib::GHandlerRegisterBase::RegisterTail
void RegisterTail(ystdex::identity< tuple<>>, _tIn first, _tIn last)
Definition: YFunc.hpp:138
yunused
#define yunused(...)
标记未使用的表达式。
Definition: ydef.h:942
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:708
YSLib::GRecursiveCallContext::KeyType
_tKey KeyType
Definition: YFunc.hpp:47
YSLib::GRecursiveCallContext::build
std::function< CallerType(const KeyType &)> build
Definition: YFunc.hpp:52
DeclSEntry
#define DeclSEntry(...)
静态接口。
Definition: YBaseMacro.h:403
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
ystdex
特性测试宏。
Definition: ydef.h:796
YSLib::GRecursiveCallContext
递归调用上下文。
Definition: YFunc.hpp:44
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
YSLib::GHandlerRegisterBase::Register
void Register(const _tKey &key)
Definition: YFunc.hpp:104
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
YSLib::GRecursiveCallContext::DefDeCopyMoveCtorAssignment
DefDeCopyMoveCtorAssignment(GRecursiveCallContext) PDefHOp(ValueType
YSLib::GRecursiveCallContext::GRecursiveCallContext
GRecursiveCallContext(_func f)
Definition: YFunc.hpp:57
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
ystdex::cpp2014::result_of_t
typename result_of< _type >::type result_of_t
Definition: type_traits.hpp:362
ystdex::any_ops::distance
Definition: any_iterator.hpp:74