YSTest: Loader.h 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
Loader.h
浏览该文件的文档.
1 /*
2 © 2013-2014 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_UI_Loader_h_
29 #define YSL_INC_UI_Loader_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_UI_YUIContainer
33 #include YFM_YSLib_Core_ValueNode
34 #include <ystdex/cast.hpp> // for ystdex::polymorphic_downcast;
35
36 namespace YSLib
37 {
38
39 namespace UI
40 {
41
43 YF_API Rect
44 ParseRect(const string&);
45
46
52 template<typename _tWidget, typename... _tParams>
53 static unique_ptr<IWidget>
54 CreateUniqueWidget(_tParams&&... args)
55 {
56 return make_unique<_tWidget>(yforward(args)...);
57 }
58
64 template<typename _tWidget, typename... _tParams>
65 static void
66 InsertWidget(IWidget& wgt, _tParams&&... args)
67 {
68 UI::AddWidget(ystdex::polymorphic_downcast<_tWidget&>(wgt),
69 yforward(args)...);
70 }
71
72
74 inline bool
75 CheckChildName(const string& str)
76 {
77 return str.size() != 0 && str[0] != '$';
78 }
79
80
86 class YF_API WidgetNotFound : public LoggedEvent
87 {
88 public:
89 string NodeName;
90
91 WidgetNotFound(const string& name, const std::string& s,
92 LevelType l = Warning)
93 : LoggedEvent(s, l),
94 NodeName(name)
95 {}
96 };
97
98
106 yconstfn PDefH(const ValueNode&, AccessWidgetNode, const ValueNode& nd)
107 ImplRet(nd)
108 template<typename... _tParams>
109 const ValueNode&
110 AccessWidgetNode(const ValueNode& node, const string& name, _tParams&&... args)
111 {
112 try
113 {
114 return
115 AccessWidgetNode(node.at("$children").at(name), yforward(args)...);
116 }
117 catch(std::out_of_range&)
118 {
119 throw WidgetNotFound(node.GetName(), "Widget children not found.");
120 }
121 }
123
131 YF_API IWidget&
132 AccessWidget(const ValueNode&);
133 template<typename... _tParams>
134 IWidget&
135 AccessWidget(const ValueNode& node, const string& name, _tParams&&... args)
136 {
137 return AccessWidget(AccessWidgetNode(node, name, yforward(args)...));
138 }
140 template<class _tWidget, typename... _tParams>
141 _tWidget&
142 AccessWidget(const ValueNode& node, _tParams&&... args)
143 {
144 return dynamic_cast<_tWidget&>(AccessWidget(node, yforward(args)...));
145 }
147
148
150 template<typename... _tParams>
151 using GWidgetCreator = unique_ptr<IWidget>(*)(_tParams...);
152
154 template<typename... _tParams>
155 using GWidgetInserter = void(*)(IWidget&, _tParams...);
156
157
171 template<typename... _tParams>
172 class GWidgetRegister : public GHandlerRegisterBase<
173 GWidgetRegister<_tParams...>, string, GWidgetCreator<_tParams...>>
174 {
175 public:
176 using Base = GHandlerRegisterBase<GWidgetRegister<_tParams...>, string,
177 GWidgetCreator<_tParams...>>;
178
179 ImplS(Base) template<typename _tWidget, typename _fHandler>
180 static GWidgetCreator<_tParams...>
181 GetRegister(const string&)
182 {
183 return &CreateUniqueWidget<_tWidget, _tParams...>;
184 }
185 };
186
187
201 template<typename... _tParams>
202 class GWidgetInserterRegister : public GHandlerRegisterBase<
203 GWidgetInserterRegister<_tParams...>, string, GWidgetInserter<_tParams...>>
204 {
205 public:
206 using Base = GHandlerRegisterBase<GWidgetInserterRegister<_tParams...>,
207 string, GWidgetInserter<_tParams...>>;
208
209 ImplS(Base) template<typename _tWidget, typename _fHandler>
210 static GWidgetInserter<_tParams...>
211 GetRegister(const string&)
212 {
213 return &InsertWidget<_tWidget, _tParams...>;
214 }
215 };
216
217
222 class YF_API WidgetLoader
223 {
224 public:
225 GWidgetRegister<> Default{};
226 GWidgetRegister<const Rect&> Bounds{};
228 GWidgetInserterRegister<IWidget&> Insert{};
230 GWidgetInserterRegister<IWidget&, const ZOrderType&> InsertZOrdered{};
231
232 unique_ptr<IWidget>
233 DetectWidgetNode(const ValueNode&);
234
235 ValueNode
236 LoadUILayout(const string&);
237
238 ValueNode
239 TransformUILayout(const ValueNode&);
240 };
241
242
247 class YF_API DynamicWidget
248 {
249 public:
250 std::reference_wrapper<WidgetLoader> Loader;
251 ValueNode WidgetNode;
252
253 DynamicWidget(WidgetLoader& ldr, const string& str)
254 : Loader(ldr), WidgetNode(ldr.LoadUILayout(str))
255 {}
256 };
257
258
263 #define DeclDynWidget(_t, _n, ...) \
264 auto& _n(YSLib::UI::AccessWidget<_t>(__VA_ARGS__));
265
270 #define DeclDynWidgetN(_t, _n, ...) \
271 DeclDynWidget(_t, _n, __VA_ARGS__, #_n)
272
276 #define DeclDynWidgetNode(_t, _n) DeclDynWidgetN(_t, _n, node)
277
278 } // namespace UI;
279
280 } // namespace YSLib;
281
282 #endif
283
YSLib::Drawing::ImageDecoderFlags::Default
YModules.h
YSLib::UI::GWidgetRegister
加载器注册接口：加载一个或多个键和类初始化例程。
Definition: Loader.h:172
YSLib::UI::args
yconstfn const string _tParams && args
Definition: Loader.h:111
YSLib::UI::DynamicWidget
动态部件。
Definition: Loader.h:247
YSLib::UI::GWidgetInserterRegister::ImplS
ImplS(Base) template< typename _tWidget
platform_ex::Windows::l
l
Definition: MinGW32.cpp:50
YSLib::UI::DynamicWidget::WidgetNode
ValueNode WidgetNode
Definition: Loader.h:251
YSLib::GHandlerRegisterBase< GWidgetRegister< _tParams...>, string, GWidgetCreator< _tParams...> >::GetRegister
GWidgetCreator< _tParams...>GWidgetCreator< _tParams...> GetRegister() const ) template< typename..._tParams > auto Call(const string &key
YF_API
#define YF_API
Definition: Platform.h:64
YSLib::UI::DynamicWidget::DynamicWidget
DynamicWidget(WidgetLoader &ldr, const string &str)
Definition: Loader.h:253
YSLib::UI::GWidgetInserterRegister
加载器注册接口：加载一个或多个键和添加部件引用例程。
Definition: Loader.h:202
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
YSLib::UI::WidgetLoader
部件加载器。
Definition: Loader.h:222
YSLib::UI::DynamicWidget::Loader
std::reference_wrapper< WidgetLoader > Loader
Definition: Loader.h:250
YSLib::UI::PDefH
PDefH(void, Activate, Console &console, Drawing::Color fc=Drawing::ColorSpace::White) ImplExpr(Activate(console
激活：使用指定屏幕、有效性、前景色和默认背景色。
cast.hpp
C++ 转换模板。
platform::Descriptions::Warning
Definition: ycommon.h:75
YSLib::UI::wgt
_tWidget & wgt
Definition: ywgtevt.h:596
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:110
YSLib::UI::InsertWidget
static void InsertWidget(IWidget &wgt, _tParams &&...args)
注册部件加载器：添加部件引用。
Definition: Loader.h:66
YSLib::UI::GWidgetRegister::ImplS
ImplS(Base) template< typename _tWidget
YSLib::string
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
YSLib::ValueNode
值类型节点。
Definition: ValueNode.h:45
YSLib::GHandlerRegisterBase
注册处理器抽象模板：供派生类加载一个或多个键和指定类型关联的例程。
Definition: yfunc.hpp:328
YSLib::UI::WidgetNotFound::WidgetNotFound
WidgetNotFound(const string &name, const std::string &s, LevelType l=Warning)
Definition: Loader.h:91
YSLib::UI::CreateUniqueWidget
static unique_ptr< IWidget > CreateUniqueWidget(_tParams &&...args)
注册部件加载器：创建部件。
Definition: Loader.h:54
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: ycommon.h:69
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
YSLib::UI::AccessWidget
IWidget & AccessWidget(const ValueNode &node, const string &name, _tParams &&...args)
Definition: Loader.h:135
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
YSLib::UI::ImplRet
const IWidget &wgt ImplRet(wgt.GetView().IsVisible()) bool YF_API Contains(const IWidget &
判断点是否在部件的可视区域内。
YSLib::UI::ParseRect
YF_API Rect ParseRect(const string &)
Definition: Loader.cpp:43
YSLib::UI::AddWidget
void AddWidget(_tCon &con, IWidget &wgt)
向部件添加单一子部件。
Definition: yuicont.h:377
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: yexcept.h:58
YSLib::UI::GWidgetInserter
void(*)(IWidget &, _tParams...) GWidgetInserter
Definition: Loader.h:155
YSLib::UI::WidgetNotFound::NodeName
string NodeName
Definition: Loader.h:89
YSLib::UI::GWidgetCreator
unique_ptr< IWidget >(*)(_tParams...) GWidgetCreator
Definition: Loader.h:151
YSLib::UI::CheckChildName
bool CheckChildName(const string &str)
Definition: Loader.h:75
YSLib::UI::WidgetNotFound
找不到部件。
Definition: Loader.h:86