YSTest: Loader.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Loader.cpp
浏览该文件的文档.
1 /*
2 © 2013-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 #include "YSLib/UI/YModules.h"
29 #include "NPL/YModules.h"
30 #include YFM_YSLib_UI_Loader // for to_std_string, istringtream;
31 #include <sstream> // for complete istringtream;
32 #include YFM_YSLib_UI_YPanel
33 #include YFM_NPL_SContext
34 #include YFM_NPL_Configuration
35
36 namespace YSLib
37 {
38
39 namespace UI
40 {
41
42 Rect
43 ParseRect(const string& str)
44 {
45 Rect res;
46
47 try
48 {
49 istringstream iss(str);
50
51 iss.exceptions(std::ios::failbit | std::ios::badbit);
52 iss >> res.X >> res.Y >> res.Width >> res.Height;
53 }
54 catch(std::exception& e)
55 {
56 YTraceDe(Warning, "Error: %s", e.what());
57 throw std::invalid_argument("Parse 'Rect' failed: bad state.");
58 }
59 YTraceDe(Debug, "ParseRect: %s.", to_string(res).c_str());
60 return res;
61 }
62
63
64 ImplDeDtor(WidgetNotFound)
65
66
67 IWidget&
68 AccessWidget(const ValueNode& node)
69 {
70 if(const auto p = AccessNodePtr(node, "$pointer"))
71 // NOTE: It may still throw %bad_any_cast.
72 return Deref(Access<shared_ptr<IWidget>>(*p));
73 throw WidgetNotFound(node.GetName(), "Widget pointer not found.");
74 }
75
76
77 unique_ptr<IWidget>
78 WidgetLoader::DetectWidgetNode(const ValueNode& node) const
79 {
80 const auto& child(AccessNode(node, "$type"));
81
82 if(const auto p_type_str = AccessPtr<string>(child))
83 {
84 if(const auto p_bounds_str = AccessChildPtr<string>(node, "$bounds"))
85 try
86 {
87 const Rect& bounds(ParseRect(*p_bounds_str));
88
89 return Bounds.Invoke(*p_type_str, bounds);
90 }
91 CatchIgnore(std::invalid_argument&)
92 return Default.Invoke(*p_type_str);
93 }
94 return {};
95 }
96
97 ValueNode
98 WidgetLoader::LoadUILayout(string_view sv) const
99 {
100 using namespace NPL;
101 Session sess{};
102
103 return TransformUILayout(
104 A1::LoadNode(SContext::Analyze(sess, sess.Process(sv))));
105 }
106
107 ValueNode
108 WidgetLoader::TransformUILayout(const ValueNode& node) const
109 {
110 if(unique_ptr<IWidget> p_new_widget{DetectWidgetNode(node)})
111 {
112 ValueNode res(AsNode(node.GetName()));
113 const auto& key(AccessChild<string>(node, "$type"));
114 const bool ins(Insert.Contains(key));
115 const bool insz(InsertZOrdered.Contains(key));
116
117 if(ins || insz)
118 {
119 ValueNode::Container node_con;
120
121 for(const auto& vn : node)
122 if(CheckChildName(vn.GetName()))
123 if(ValueNode child{TransformUILayout(vn)})
124 {
125 const auto& wgt_ptr_nd(AccessNode(child, "$pointer"));
126
127 if(const auto p_wgt_ptr
128 = AccessPtr<shared_ptr<IWidget>>(wgt_ptr_nd))
129 {
130 const auto& wgt_ptr(Deref(p_wgt_ptr));
131
132 const auto p_z(AccessChildPtr<string>(vn, "$z"));
133 auto z(DefaultZOrder);
134
135 if(insz && p_z)
136 try
137 {
138 const auto
139 r(std::stoul(to_std_string(*p_z)));
140
141 // TODO: Do not use magic number.
142 if(r < 0x100)
143 z = r;
144 }
145 CatchIgnore(std::invalid_argument&)
146 if(node_con.insert(std::move(child)).second)
147 {
148 auto& wgt(*wgt_ptr);
149
150 if(insz && (p_z || !ins))
151 InsertZOrdered.Invoke(key, *p_new_widget,
152 wgt, z);
153 else
154 Insert.Invoke(key, *p_new_widget, wgt);
155 }
156 }
157 }
158 res += {std::move(node_con), "$children"};
159 }
160 res += AsNode("$pointer", shared_ptr<IWidget>(std::move(p_new_widget)));
161 return res;
162 }
163 return {};
164 }
165
166 } // namespace UI;
167
168 } // namespace YSLib;
169
ystdex::mapped_set< ValueNode, ystdex::less<>, MappedSetTraits, allocator_type >
YSLib::Drawing::Rect::Width
SDst Width
长。
Definition: YGDIBase.h:545
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
ystdex::mapped_set::insert
std::pair< iterator, bool > insert(const value_type &x)
Definition: set.hpp:528
YSLib::UI::WidgetLoader::InsertZOrdered
GWidgetInserterRegister< IWidget &, const ZOrder & > InsertZOrdered
Definition: Loader.h:244
YSLib::UI::WidgetLoader::TransformUILayout
ValueNode TransformUILayout(const ValueNode &) const
变换 UI 布局树：根据 NPLA1 中间表示动态创建部件树。
Definition: Loader.cpp:108
YSLib::UI::WidgetLoader::Bounds
GWidgetRegister< const Rect & > Bounds
Definition: Loader.h:240
platform::strings::to_std_string
_tString to_std_string(basic_string_view< _tChar > sv)
Definition: Container.h:256
YSLib::UI::ImplDeDtor
ImplDeDtor(Thumb) void DecorateAsCloseButton(Thumb &tmb)
Definition: Button.cpp:125
YSLib::Access
YB_ATTR_nodiscard YB_PURE const _type & Access(const ValueNode &node)
Definition: ValueNode.h:964
YModules.h
NPL 库模块配置文件。
YSLib::UI::ParseRect
YF_API Rect ParseRect(const string &)
Definition: Loader.cpp:43
YSLib::ValueNode
值类型节点。
Definition: ValueNode.h:73
YSLib::UI::DefaultZOrder
const ZOrder DefaultZOrder(64)
默认 Z 顺序值。
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:407
NPL
Definition: NPL/Configuration.h:35
YSLib::Drawing::GBinaryGroup::X
_type X
分量。
Definition: YGDIBase.h:305
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
platform::Descriptions::Warning
@ Warning
Definition: YCommon.h:183
YSLib::UI::e
void e
Definition: YWidget.h:356
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:788
YSLib::AsNode
YB_ATTR_nodiscard YB_PURE ValueNode AsNode(const ValueNode::allocator_type &a, _tString &&str, _tParams &&... args)
Definition: ValueNode.h:1304
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YSLib::UI::WidgetLoader::Default
GWidgetRegister Default
Definition: Loader.h:239
YModules.h
YSLib 用户界面模块配置文件。
YSLib::Drawing::Rect::Height
SDst Height
宽。
Definition: YGDIBase.h:545
YSLib::str
const char * str
Definition: YException.cpp:60
platform::Deref
YB_PURE yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:547
YSLib::Drawing::bounds
The boundary is out of the const Rect & bounds
Definition: YDraw.h:127
YSLib::UI::wgt
IWidget & wgt
Definition: YControl.h:113
NPL::Session
会话：分析组成 NPL 基本翻译单元的源代码。
Definition: SContext.h:797
YSLib::UI::AccessWidget
IWidget & AccessWidget(ValueNode &node, const string &name, _tParams &&... args)
Definition: Loader.h:148
YSLib::AccessPtr
YB_ATTR_nodiscard YB_PURE observer_ptr< _type > AccessPtr(ValueNode &node) ynothrow
访问节点的指定类型对象指针。
Definition: ValueNode.h:976
NPL::c_str
YF_API const char src c_str())) YF_API void InstallDirectory(const string &
安装目录：复制目录树。
Definition: Dependency.h:140
YSLib::UI::WidgetLoader::DetectWidgetNode
unique_ptr< IWidget > DetectWidgetNode(const ValueNode &) const
Definition: Loader.cpp:78
NPL::A1::LoadNode
YB_ATTR_nodiscard ValueNode LoadNode(_type &&tree, _tParams &&... args)
加载 NPLA1 翻译单元。
Definition: NPL/Configuration.h:48
YSLib::Drawing::GBinaryGroup::Y
_type Y
Definition: YGDIBase.h:305
YSLib::AccessNodePtr
YB_ATTR_nodiscard YB_PURE observer_ptr< ValueNode > AccessNodePtr(ValueNode::Container *, const _tKey &) ynothrow
Definition: ValueNode.h:1149
platform::p
return p p
Definition: YCLib/FileSystem.h:209
YSLib::UI::WidgetLoader::Insert
GWidgetInserterRegister< IWidget & > Insert
Definition: Loader.h:242
platform::ios::istringstream
istringstream_for< string > istringstream
Definition: Container.h:310
YSLib::AccessNode
YB_ATTR_nodiscard YF_API YB_PURE ValueNode & AccessNode(ValueNode::Container *, const string &)
访问节点。
Definition: ValueNode.cpp:102
NPL::SContext::Analyze
void Analyze(TermNode &root, const _tParseResult &parse_result)
分析指定源，取抽象语法树储存至指定值类型节点。
Definition: SContext.h:1127
YSLib::UI::WidgetLoader::LoadUILayout
ValueNode LoadUILayout(string_view) const
加载 NPLA1 翻译单元，翻译后变换为 UI 布局树。
Definition: Loader.cpp:98
platform::Descriptions::Debug
@ Debug
Definition: YCommon.h:186