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 
87 {
88 public:
89  string NodeName;
90 
91  WidgetNotFound(const string& name, const std::string& s,
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>
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>
203  GWidgetInserterRegister<_tParams...>, string, GWidgetInserter<_tParams...>>
204 {
205 public:
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 
223 {
224 public:
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 
248 {
249 public:
250  std::reference_wrapper<WidgetLoader> Loader;
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 
加载器注册接口:加载一个或多个键和类初始化例程。
Definition: Loader.h:172
yconstfn const string _tParams && args
Definition: Loader.h:111
动态部件。
Definition: Loader.h:247
ImplS(Base) template< typename _tWidget
ValueNode WidgetNode
Definition: Loader.h:251
GWidgetCreator< _tParams...>GWidgetCreator< _tParams...> GetRegister() const ) template< typename..._tParams > auto Call(const string &key
#define YF_API
Definition: Platform.h:64
DynamicWidget(WidgetLoader &ldr, const string &str)
Definition: Loader.h:253
加载器注册接口:加载一个或多个键和添加部件引用例程。
Definition: Loader.h:202
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
部件加载器。
Definition: Loader.h:222
std::reference_wrapper< WidgetLoader > Loader
Definition: Loader.h:250
PDefH(void, Activate, Console &console, Drawing::Color fc=Drawing::ColorSpace::White) ImplExpr(Activate(console
激活:使用指定屏幕、有效性、前景色和默认背景色。
C++ 转换模板。
_tWidget & wgt
Definition: ywgtevt.h:596
yconstfn const string & name
Definition: Loader.h:110
static void InsertWidget(IWidget &wgt, _tParams &&...args)
注册部件加载器:添加部件引用。
Definition: Loader.h:66
ImplS(Base) template< typename _tWidget
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
值类型节点。
Definition: ValueNode.h:45
注册处理器抽象模板:供派生类加载一个或多个键和指定类型关联的例程。
Definition: yfunc.hpp:328
WidgetNotFound(const string &name, const std::string &s, LevelType l=Warning)
Definition: Loader.h:91
static unique_ptr< IWidget > CreateUniqueWidget(_tParams &&...args)
注册部件加载器:创建部件。
Definition: Loader.h:54
RecordLevel
记录等级。
Definition: ycommon.h:69
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
IWidget & AccessWidget(const ValueNode &node, const string &name, _tParams &&...args)
Definition: Loader.h:135
const IWidget &wgt ImplRet(wgt.GetView().IsVisible()) bool YF_API Contains(const IWidget &
判断点是否在部件的可视区域内。
YF_API Rect ParseRect(const string &)
Definition: Loader.cpp:43
void AddWidget(_tCon &con, IWidget &wgt)
向部件添加单一子部件。
Definition: yuicont.h:377
记录日志的异常事件类。
Definition: yexcept.h:58
void(*)(IWidget &, _tParams...) GWidgetInserter
Definition: Loader.h:155
unique_ptr< IWidget >(*)(_tParams...) GWidgetCreator
Definition: Loader.h:151
bool CheckChildName(const string &str)
Definition: Loader.h:75
找不到部件。
Definition: Loader.h:86