YSTest: Loader.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

Loader.h
浏览该文件的文档.
1 /*
2 © 2013-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_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
78 inline PDefH(bool, CheckChildName, string_view sv)
79 ImplRet((YAssertNonnull(sv.data()), !sv.empty() && sv.front() != '$'))
80
81
82
87 class YF_API WidgetNotFound : public LoggedEvent
88 {
89 public:
90 string NodeName;
91
96 YB_NONNULL(3)
97 WidgetNotFound(string_view name, const char* s,
98 RecordLevel lv = Warning)
99 : LoggedEvent(s, lv),
100 NodeName((Nonnull(name.data()), name))
101 {}
102 WidgetNotFound(string_view name, const string_view sv,
103 RecordLevel lv = Warning)
104 : LoggedEvent(sv, lv),
105 NodeName((Nonnull(name.data()), name))
106 {}
107
109 DefDeCopyCtor(WidgetNotFound)
114 ~WidgetNotFound() override;
115 };
116
117
118 /*
119 \throw WidgetNotFound 没有找到指定名称的部件。
120 \note 不抛出 std::out_of_range （已全部捕获并抛出为 WidgetNotFound ）。
121 \since build 667
122 */
124
126 yconstfn PDefH(ValueNode&, AccessWidgetNode, ValueNode& nd)
127 ImplRet(nd)
128 template<typename... _tParams>
129 ValueNode&
130 AccessWidgetNode(ValueNode& node, const string& name, _tParams&&... args)
131 {
132 TryRet(AccessWidgetNode(AccessNode(AccessNode(node, "$children"), name),
133 yforward(args)...))
134 CatchThrow(std::out_of_range&,
135 WidgetNotFound(node.GetName(), "Widget children not found."))
136 }
138
144 YF_API IWidget&
145 AccessWidget(const ValueNode&);
146 template<typename... _tParams>
147 IWidget&
148 AccessWidget(ValueNode& node, const string& name, _tParams&&... args)
149 {
150 return AccessWidget(AccessWidgetNode(node, name, yforward(args)...));
151 }
153 template<class _tWidget, typename... _tParams>
154 _tWidget&
155 AccessWidget(ValueNode& node, _tParams&&... args)
156 {
157 return dynamic_cast<_tWidget&>(AccessWidget(node, yforward(args)...));
158 }
160
161
162
164 template<typename... _tParams>
165 using GWidgetCreator = unique_ptr<IWidget>(*)(_tParams...);
166
168 template<typename... _tParams>
169 using GWidgetInserter = void(*)(IWidget&, _tParams...);
170
171
185 template<typename... _tParams>
186 class GWidgetRegister : public GHandlerRegisterBase<
187 GWidgetRegister<_tParams...>, string, GWidgetCreator<_tParams...>>
188 {
189 public:
190 using Base = GHandlerRegisterBase<GWidgetRegister<_tParams...>, string,
191 GWidgetCreator<_tParams...>>;
192
193 ImplS(Base) template<typename _tWidget, typename _fHandler>
194 static GWidgetCreator<_tParams...>
195 GetRegister(const string&)
196 {
197 return &CreateUniqueWidget<_tWidget, _tParams...>;
198 }
199 };
200
201
215 template<typename... _tParams>
216 class GWidgetInserterRegister : public GHandlerRegisterBase<
217 GWidgetInserterRegister<_tParams...>, string, GWidgetInserter<_tParams...>>
218 {
219 public:
220 using Base = GHandlerRegisterBase<GWidgetInserterRegister<_tParams...>,
221 string, GWidgetInserter<_tParams...>>;
222
223 ImplS(Base) template<typename _tWidget, typename _fHandler>
224 static GWidgetInserter<_tParams...>
225 GetRegister(const string&)
226 {
227 return &InsertWidget<_tWidget, _tParams...>;
228 }
229 };
230
231
236 class YF_API WidgetLoader
237 {
238 public:
239 GWidgetRegister<> Default{};
240 GWidgetRegister<const Rect&> Bounds{};
242 GWidgetInserterRegister<IWidget&> Insert{};
244 GWidgetInserterRegister<IWidget&, const ZOrder&> InsertZOrdered{};
245
247 unique_ptr<IWidget>
248 DetectWidgetNode(const ValueNode&);
249
256 ValueNode
257 LoadUILayout(string_view);
258
270 ValueNode
271 TransformUILayout(const ValueNode&);
272 };
273
274
279 class YF_API DynamicWidget
280 {
281 public:
283 lref<WidgetLoader> Loader;
284 ValueNode WidgetNode;
285
290 DynamicWidget(WidgetLoader& ldr, string_view sv)
291 : Loader(ldr), WidgetNode(ldr.LoadUILayout(sv))
292 {}
293 };
294
295
300 #define DeclDynWidget(_t, _n, ...) \
301 auto& _n(YSLib::UI::AccessWidget<_t>(__VA_ARGS__));
302
307 #define DeclDynWidgetN(_t, _n, ...) \
308 DeclDynWidget(_t, _n, __VA_ARGS__, #_n)
309
313 #define DeclDynWidgetNode(_t, _n) DeclDynWidgetN(_t, _n, node)
314
315 } // namespace UI;
316
317 } // namespace YSLib;
318
319 #endif
320
YSLib::UI::GWidgetRegister
加载器注册接口：加载一个或多个键和类初始化例程。
Definition: Loader.h:186
YSLib::UI::GWidgetCreator
unique_ptr< IWidget >(*)(_tParams...) GWidgetCreator
Definition: Loader.h:165
YSLib::UI::GWidgetRegister::ImplS
ImplS(Base) template< typename _tWidget
YSLib::UI::DynamicWidget
动态部件。
Definition: Loader.h:279
YSLib::UI::PDefH
PDefH(void, DisplayFor, IWidget &wgt, Timers::TimeSpan delay) ImplExpr(Show(wgt)
在一定时间内显示部件。
YSLib::UI::args
yconstfn const string _tParams && args
Definition: Loader.h:131
CatchThrow
#define CatchThrow(_ex,...)
指定抛出指定异常作为异常处理器。
Definition: YBaseMacro.h:141
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::UI::DynamicWidget::Loader
lref< WidgetLoader > Loader
Definition: Loader.h:283
YSLib::UI::WidgetLoader
部件加载器。
Definition: Loader.h:236
cast.hpp
C++ 转换模板。
UI
platform::KeyCodes::DS::Insert
Definition: Keys.h:260
YSLib::GHandlerRegisterBase
注册处理器抽象模板：供派生类加载一个或多个键和指定类型关联的例程。
Definition: YFunc.hpp:74
YSLib::UI::CreateUniqueWidget
static unique_ptr< IWidget > CreateUniqueWidget(_tParams &&...args)
注册部件加载器：创建部件。
Definition: Loader.h:54
YSLib::UI::wgt
IWidget & wgt
Definition: YControl.h:112
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:120
YSLib::UI::ImplRet
const IWidget &wgt ImplRet(wgt.GetView().IsVisible()) bool YF_API Contains(const IWidget &
判断点是否在部件的可视区域内。
YSLib::UI::AddWidget
void AddWidget(_tCon &con, IWidget &wgt)
向部件添加单一子部件。
Definition: YUIContainer.h:420
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:50
TryRet
#define TryRet(...)
指定表达式在 try 块中求值并返回。
Definition: YBaseMacro.h:124
platform::containers::string
basic_string< char > string
Definition: Container.h:101
YSLib::AccessNode
const ValueNode & AccessNode(const ValueNode &node, const _tKey &name)
Definition: ValueNode.h:866
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
YSLib::UI::GWidgetInserter
void(*)(IWidget &, _tParams...) GWidgetInserter
Definition: Loader.h:169
YSLib::UI::AccessWidget
IWidget & AccessWidget(ValueNode &node, const string &name, _tParams &&...args)
Definition: Loader.h:148
YSLib::RecordLevel
YF_API RecordLevel
Definition: YException.h:142
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
YSLib::UI::GWidgetInserterRegister::ImplS
ImplS(Base) template< typename _tWidget
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:161
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
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:56
YSLib::UI::GWidgetInserterRegister
加载器注册接口：加载一个或多个键和添加部件引用例程。
Definition: Loader.h:216
YModules.h
YSLib::ValueNode
值类型节点。
Definition: ValueNode.h:58
YSLib::YB_NONNULL
YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
platform::Nonnull
_type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:422
platform::Descriptions::Warning
Definition: YCommon.h:167
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:162
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:130
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
YSLib::UI::InsertWidget
static void InsertWidget(IWidget &wgt, _tParams &&...args)
注册部件加载器：添加部件引用。
Definition: Loader.h:66
YSLib::UI::DynamicWidget::WidgetNode
ValueNode WidgetNode
Definition: Loader.h:284
YSLib::UI::ParseRect
YF_API Rect ParseRect(const string &)
Definition: Loader.cpp:43
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
YSLib::Drawing::ImageDecoderFlags::Default
YSLib::UI::DynamicWidget::DynamicWidget
DynamicWidget(WidgetLoader &ldr, string_view sv)
Definition: Loader.h:290
YSLib::lv
RecordLevel lv
Definition: YException.cpp:58