YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
source/main.cpp
浏览该文件的文档.
1 /*
2  © 2009-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 #include "ShlExplorer.h"
29 
30 using namespace platform;
31 using std::puts;
32 
33 namespace YSLib
34 {
35 
36 //调试功能。
37 namespace
38 {
39 
41 class Log : private noncopyable
42 {
43 public:
47  yconstfn DefDeCtor(Log)
51  virtual DefDeDtor(Log)
52 
57  Log&
58  operator<<(char)
59  {
60  return *this;
61  }
66  Log&
67  operator<<(const char*)
68  {
69  return *this;
70  }
74  Log&
75  operator<<(const string& s)
76  {
77  return operator<<(s.c_str());
78  }
79 
84  void
85  Error(const char*)
86  {}
90  void
91  Error(const string& s)
92  {
93  Error(s.c_str());
94  }
99  void
100  FatalError(const char* s)
101  {
102  ShowFatalError(s);
103  }
108  void
109  FatalError(const string& s)
110  {
111  FatalError(s.c_str());
112  }
113 };
114 
115 
116 #ifdef YSL_USE_MEMORY_DEBUG
117 
118 void
119 OnExit_DebugMemory_continue()
120 {
121  std::puts("Input to continue...");
123 }
124 
128 template<std::size_t _vN, class _tCon>
129 void
130 OnExit_DebugMemory_print(_tCon& con, const char* item_name)
131 {
132  if(!con.empty())
133  {
134  std::fprintf(stderr, "%i %s(s) detected:\n", con.size(), item_name);
135 
136  typename _tCon::size_type n(0);
137 
138  for(const auto& item : con)
139  {
140  if(n++ < _vN)
141  GetDebugMemoryList().Print(item, stderr);
142  else
143  {
144  n = 0;
145  OnExit_DebugMemory_continue();
146  }
147  }
148  OnExit_DebugMemory_continue();
149  }
150 }
151 
155 void
156 OnExit_DebugMemory()
157 {
158  using namespace platform;
159 
160  YDebugSetStatus();
161  YDebugBegin();
162  std::puts("Normal exit;");
163 
164 // std::FILE* fp(std::freopen("memdbg.log", "w", stderr));
165  OnExit_DebugMemory_print<4>(GetDebugMemoryList().Blocks, "memory leak");
166  OnExit_DebugMemory_print<4>(GetDebugMemoryList().DuplicateDeletedBlocks,
167  "duplicate memory deleting");
168 #if 0
169  DebugMemory.PrintAll(stderr);
170  DebugMemory.PrintAll(fp);
171  DebugMemory.PrintAllDuplicate(stderr);
172  DebugMemory.PrintAllDuplicate(fp);
173 #endif
174 // std::fclose(fp);
175  std::puts("Input to terminate...");
176  WaitForInput();
177 }
178 
179 #endif
180 
184 void
185 Repaint(Desktop& dsk)
186 {
187  SetInvalidationOf(dsk);
188  dsk.Validate();
189  dsk.Update();
190 }
191 
192 } // unnamed namespace;
193 
194 
196 
197 
198 
199 } // namespace YSLib;
200 
201 int
203 {
204  using namespace YSLib;
205 
206  Log log;
207 
208  try
209  {
210  {
211  //应用程序实例。
212  DSApplication app;
213 
214  //platform::YDebugSetStatus(false);
215  {
216  using namespace YSLib::UI;
217  using YReader::Image_N;
218 
219  Desktop dsk_m(app.GetScreenUp()), dsk_s(app.GetScreenDown());
220  Label lblTitle({50, 20, 100, 22}), lblStatus({60, 80, 120, 22}),
221  lblDetails({30, 20, 160, 22});
222  ProgressBar pb({8, 168, 240, 16}, Image_N);
223 
224  AddWidgets(dsk_m, lblTitle, lblStatus, pb);
225  dsk_s += lblDetails;
226  yunseq(
227  dsk_m.Background = SolidBrush({240, 216, 192}),
228  dsk_s.Background
230  //Color(240, 216, 240),
231  lblTitle.Text = G_APP_NAME,
232  lblStatus.Text = u"Now loading...",
233  lblDetails.Text = u"初始化中,请稍后……",
234  lblDetails.ForeColor = Drawing::ColorSpace::White
235  );
236  lblDetails.Background = nullptr;
237  SetInvalidationOf(dsk_m);
238  yunseq(dsk_m.Validate(), dsk_s.Validate());
239  dsk_m.Update(), dsk_s.Update();
240  //初始化所有图像资源。
241  for(size_t i(0); i < Image_N; ++i)
242  {
243  pb.SetValue(i);
244  // Invalidate(pb);
245  dsk_m.Background
246  = SolidBrush(Color(255 - i * 255 / Image_N, 216, 192));
247  Repaint(dsk_m);
249  }
250  pb.SetValue(Image_N);
251  Repaint(dsk_m);
252  }
253  Execute(app, make_shared<YReader::ShlExplorer>());
254  //释放 Shell (必要,保证释放 Shell 且避免资源泄漏)。
256  }
257 
258  #ifdef YSL_USE_MEMORY_DEBUG
259  OnExit_DebugMemory();
260  #endif
261  }
262  catch(FatalError& e)
263  {
264  HandleFatalError(e);
265  }
266  catch(LoggedEvent& e)
267  {
268  log.FatalError("Unhandled logged event with level = "
269  + to_string(e.GetLevel()) + " : " + e.what());
270  }
271  catch(std::exception& e)
272  {
273  // TODO: Distinguish errors from %std::runtime_error and YSLib-defined
274  // exceptions.
275  log.FatalError(string("Unhandled std::exception: ") + e.what());
276  }
277  catch(...)
278  {
279  log.FatalError("Unhandled exception @ main function;");
280  }
281  // TODO: Return exit code properly.
282  return 0;
283 }
284 
std::FILE ConversionState fp
Definition: chrproc.h:88
#define DefDeDtor(_t)
定义默认析构函数。
Definition: YBaseMacro.h:146
#define G_APP_NAME
Definition: Shells.h:58
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
void Cleanup()
全局资源清理。
Definition: Shells.cpp:84
YF_API void YDebugBegin()
调试模式:显示控制台。
Definition: Debug.cpp:75
int main()
void SetInvalidationOf(IWidget &wgt)
Definition: ywidget.cpp:60
void AddWidgets(_tCon &con, _tWidgets &...wgts)
向部件容器添加子部件。
Definition: yuicont.h:400
YF_API void Execute(GUIApplication &, shared_ptr< Shell >=make_shared< Shells::GUIShell >())
执行程序主消息循环。
YF_API void YDebugSetStatus(bool=true)
调试模式:设置状态。
Definition: Debug.cpp:63
YF_API void WaitForInput()
等待任意按键。
Definition: Input.cpp:47
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
标签。
Definition: label.h:104
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
桌面背景。
Definition: ystyle.h:181
shared_ptr< Image > & FetchImage(size_t)
Definition: Shells.cpp:94
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
Color
控制台颜色枚举。
Definition: Video.h:458
YB_NORETURN YF_API void HandleFatalError(const FatalError &) ynothrow
处理致命错误:显示错误信息并终止程序。
记录日志的异常事件类。
Definition: yexcept.h:58
DefDeCtor(BGRA) yconstfn BGRA(typename Trait
Definition: Video.h:102
单色画刷。
Definition: YBrush.h:46
File & operator<<(File &f, const Configuration &conf)
void ShowFatalError(const char *s)
Definition: DSMain.cpp:181
yconstexpr size_t Image_N(3)
文件浏览器。
进度条。
Definition: progress.h:46