YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ShellHelper.h
浏览该文件的文档.
1 /*
2  © 2010-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 INC_Helper_ShellHelper_h_
29 #define INC_Helper_ShellHelper_h_ 1
30 
31 #include "../ysbuild.h"
32 #include <ystdex/cast.hpp>
33 #include YFM_YCLib_Debug
34 #include YFM_YSLib_Service_YTimer // for Timers::HighResolutionClock;
35 #include YFM_YSLib_UI_TextList // for UI::TextList::ListType;
36 
37 namespace YSLib
38 {
39 
40 #ifndef NDEBUG
41 
47 {
48 protected:
51 
52 public:
53  DebugTimer(const std::string& str = "");
54  ~DebugTimer();
55 };
56 # define YSL_DEBUG_DECL_TIMER(_name, ...) DebugTimer _name(__VA_ARGS__);
57 #else
58 # define YSL_DEBUG_DECL_TIMER(...)
59 #endif
60 
61 
66 template<class _tShell, typename _tConnection = std::reference_wrapper<_tShell>>
68 {
69 public:
70  using Shell = _tShell;
71  using Connection = _tConnection;
72 
73 private:
75 
76 public:
77  template<typename... _tParam>
78  GShellSession(_tParam&&... args)
79  : conn(yforward(args)...)
80  {}
81 
82  virtual DefDeDtor(GShellSession)
83 
84  DefGetter(const ynothrow, _tShell&, Shell, conn)
85 };
86 
87 
88 namespace Text
89 {
90 
95 using EncodingInfoItem = std::pair<Encoding, const ucs2_t*>;
96 
102  {CharSet::GBK, u"GBK"}, {CharSet::UTF_16BE, u"UTF-16 Big Endian"},
103  {CharSet::UTF_16LE, u"UTF-16 Little Endian"},
104  {CharSet::UTF_32BE, u"UTF-32 Big Endian"},
105  {CharSet::UTF_32LE, u"UTF-16 Little Endian"}};
106 
107 } // namespace Text;
108 
109 
114 
115 //句柄语法糖。
116 
121 template<class _type, class _tHandle>
122 inline _type&
123 HandleToReference(_tHandle h) ythrow(std::bad_cast)
124 {
125  _type* _tmp(dynamic_cast<_type*>(get_raw(h)));
126 
127  if(YB_LIKELY(!_tmp))
128  throw std::bad_cast();
129  return *_tmp;
130 }
131 
136 template<class _tShell>
137 inline _tShell&
139 {
140  auto hShl(FetchShellHandle());
141 
142  YAssert(bool(hShl), "Null handle found.");
143  return ystdex::polymorphic_downcast<_tShell&>(*hShl);
144 }
145 
146 
147 //全局函数。
148 
153 template<class _tShl>
154 inline shared_ptr<Shell>
156 {
157  return GLocalStaticCache<_tShl, shared_ptr<Shell>>::GetPointer();
158 }
159 
164 template<class _tShl>
165 inline void
167 {
169 }
170 
171 
176 inline bool
177 IsNowShell(const shared_ptr<Shell>& hShl)
178 {
179  return FetchAppInstance().GetShellHandle() == hShl;
180 }
181 
186 inline errno_t
187 NowShellTo(const shared_ptr<Shell>& hShl)
188 {
189  return -!Activate(hShl);
190 }
191 
196 template<class _tShl>
197 inline errno_t
199 {
200  return NowShellTo(new _tShl());
201 }
202 
207 template<class _tShl>
208 inline errno_t
210 {
211  return NowShellTo(FetchStored<_tShl>());
212 }
213 
218 inline void
219 SetShellTo(const shared_ptr<Shell>& hShl,
221 {
222  // NOTE: It would make the message loop in dead lock when called more
223  // than once specifying on same destination shell.
224  PostMessage<SM_Set>(prior, hShl);
225 }
226 
231 template<class _tShl>
232 inline void
234 {
235  SetShellTo(make_shared<_tShl>());
236 }
237 
242 template<class _tShl>
243 inline void
245 {
246  SetShellTo(FetchStored<_tShl>());
247 }
248 
253 template<class _tShl>
254 inline void
256 {
257  NowShellToStored<_tShl>();
258 }
259 
264 template<class _tShl>
265 inline void
267 {
268  SetShellToStored<_tShl>();
269 }
270 
271 
278 inline void
280 {
281  dsk.~Desktop();
282  new(&dsk) Desktop(scr);
283 }
284 
285 
286 //资源相关定义和函数。
287 
288 namespace Drawing
289 {
290 
292 inline Color
294 {
295 //使用 std::time(0) 初始化随机数种子在 DeSmuME 上无效。
296 // std::srand(std::time(0));
297  return Color(std::rand(), std::rand(), std::rand(), 1);
298 }
299 
305 template<typename _tOut, typename _tGen>
306 void
307 ScrDraw(_tOut buf, _tGen&& f)
308 {
309  for(SDst y(0); y < MainScreenHeight; ++y)
310  for(SDst x(0); x < MainScreenWidth; yunseq(++x, ++buf))
311  *buf = yforward(f)(x, y);
312 }
313 
318 inline shared_ptr<Image>
320 {
321  return make_shared<Image>(p, MainScreenWidth, MainScreenHeight);
322 }
323 
328 template<typename _tPixel>
329 _tPixel*
330 CreateRawBitmap(const _tPixel* s, size_t n)
331 {
332  if(YB_LIKELY(s && n))
333  {
334  size_t size(sizeof(_tPixel) * n);
335  _tPixel* d(new _tPixel[size]);
336 
337  std::copy_n(s, size, d);
338  return d;
339  }
340  return nullptr;
341 }
342 
343 } // namespace Drawing;
344 
349 YF_API void
351 
352 
357 yconstexpr const char* DefaultTimeFormat("%04u-%02u-%02u %02u:%02u:%02u");
358 
364 YF_API const char*
365 TranslateTime(const std::tm&, const char* = DefaultTimeFormat);
366 YF_API const char*
367 TranslateTime(const std::time_t&, const char* = DefaultTimeFormat)
370 
371 
377 YF_API shared_ptr<UI::TextList::ListType>
379 
380 
386 {
387 private:
392  std::chrono::nanoseconds last_tick, now_tick;
398 
399 public:
404  std::chrono::nanoseconds MinimalInterval;
405 
410  FPSCounter(std::chrono::nanoseconds = {});
411 
417  DefGetter(const ynothrow, std::chrono::nanoseconds, LastTick, last_tick)
418  DefGetter(const ynothrow, std::chrono::nanoseconds, NowTick, now_tick)
420 
426  size_t
427  Refresh();
428 };
429 
431 
432 namespace UI
433 {
434 
439 YF_API void
440 SwitchVisible(IWidget&);
441 
446 YF_API void
447 SwitchVisibleToFront(IWidget&);
448 
449 
455 {
456 public:
461  std::reference_wrapper<IWidget> Container;
462 
463  ContainerSetter(IWidget& con)
464  : Container(con)
465  {}
466 
468  PDefHOp(void, (), IWidget& wgt) const
469  ImplExpr(SetContainerPtrOf(wgt, &Container.get()))
470 };
471 
472 
478 {
479 public:
484  std::reference_wrapper<const PaintContext> Context;
485 
486  ChildPainter(const PaintContext& pc)
487  : Context(pc)
488  {}
489 
490  void
491  operator()(IWidget& wgt)
492  {
493  PaintChild(wgt, Context);
494  }
495 };
496 
497 
502 template<class _tWidget>
503 inline void
505 {
506  wgt.SetRenderer(make_unique<BufferedRenderer>()),
507  wgt.Text = s;
508 }
509 
510 
517 YF_API shared_ptr<TextList::ListType>
518 FetchVisualStyleNames(String = u"<Default>");
519 
520 } // namespace UI;
521 
522 
523 } // namespace YSLib;
524 
525 #endif
526 
YF_API Application & FetchAppInstance() ynothrow
取应用程序实例。
shared_ptr< Image > CreateSharedScreenImage(ConstBitmapPtr p)
新建屏幕图像。
Definition: ShellHelper.h:319
全局局部静态单态存储器。
Definition: ystorage.hpp:151
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
void SetShellToStored()
通过主消息队列向全局 Shell 管理器内的对象转移控制权。
Definition: ShellHelper.h:244
Shell 连接会话。
Definition: ShellHelper.h:67
std::string event_info
Definition: ShellHelper.h:49
bool IsNowShell(const shared_ptr< Shell > &hShl)
判断句柄指定的 Shell 是否为当前线程空间中运行的 Shell 。
Definition: ShellHelper.h:177
size_t refresh_count
刷新计数。
Definition: ShellHelper.h:397
yconstexpr Encoding UTF_16LE(csUTF16LE)
Color GenerateRandomColor()
Definition: ShellHelper.h:293
yconstexpr Encoding UTF_16BE(csUTF16BE)
yconstexpr EncodingInfoItem Encodings[]
编码信息。
Definition: ShellHelper.h:101
yconstfn const string _tParams && args
Definition: Loader.h:111
yconstexpr Encoding UTF_32BE(csUTF32BE)
#define YF_API
Definition: Platform.h:64
YF_API void PaintChild(IWidget &wgt, PaintEventArgs &&e)
调用指定子部件的 Paint 事件绘制参数指定的事件发送者。
Definition: ywidget.cpp:174
YF_API const char * TranslateTime(const std::tm &, const char *=DefaultTimeFormat)
格式化时间字符串。
Definition: ShellHelper.cpp:84
errno_t NowShellToStored()
向全局 Shell 管理器的对象转移控制权。
Definition: ShellHelper.h:209
_tPixel * CreateRawBitmap(const _tPixel *s, size_t n)
使用 new 分配空间并复制无压缩位图。
Definition: ShellHelper.h:330
shared_ptr< Shell > FetchShellHandle() ynothrow
取当前应用程序线程空间中活动的 Shell 句柄。
Definition: yapp.h:152
ContainerSetter(IWidget &con)
Definition: ShellHelper.h:463
YF_API shared_ptr< UI::TextList::ListType > FetchFontFamilyNames()
取字型家族名称。
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
std::reference_wrapper< const PaintContext > Context
绘制上下文。
Definition: ShellHelper.h:484
yconstexpr const char * DefaultTimeFormat("%04u-%02u-%02u %02u:%02u:%02u")
默认时间格式字符串。
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
_type * get_raw(_type *const &p)
Definition: memory.hpp:45
ChildPainter(const PaintContext &pc)
Definition: ShellHelper.h:486
std::chrono::time_point< HighResolutionClock, duration > time_point
Definition: ytimer.h:51
_type & HandleToReference(_tHandle h) ythrow(std
句柄转换:对象引用。
Definition: ShellHelper.h:123
void CallStored()
封装通过消息队列向全局 Shell 管理器内的对象转移控制权。
Definition: ShellHelper.h:266
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: yexcept.h:51
C++ 转换模板。
GShellSession(_tParam &&...args)
Definition: ShellHelper.h:78
YSLib 标准字符串(使用 UCS-2 作为内部编码)。
Definition: ystring.h:47
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
YF_API void RemoveGlobalTasks()
从全局消息队列中移除所有后台消息。
Definition: ShellHelper.cpp:54
#define ImplExpr(...)
Definition: YBaseMacro.h:93
errno_t NowShellToNew()
向新建 Shell 对象转移控制权。
Definition: ShellHelper.h:198
shared_ptr< Shell > FetchStored()
取全局 Shell 句柄。
Definition: ShellHelper.h:155
_tWidget & wgt
Definition: ywgtevt.h:596
帧速率计数器。
Definition: ShellHelper.h:385
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
bool Activate(const shared_ptr< Shell > &hShl)
激活 Shell 对象:控制权转移给此对象以维持单线程运行。
Definition: yapp.h:164
void SetShellTo(const shared_ptr< Shell > &hShl, Messaging::Priority prior=Messaging::NormalPriority)
通过主消息队列向指定 Shell 对象转移控制权。
Definition: ShellHelper.h:219
YF_API void SwitchVisible(IWidget &)
切换部件显示状态并无效化。
桌面。
Definition: ydesktop.h:46
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
#define ythrow(...)
YSLib 动态异常规范:根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
std::chrono::nanoseconds MinimalInterval
计时间隔下界。
Definition: ShellHelper.h:404
_tShell & FetchShell()
取指定 Shell 句柄对应的 Shell 引用 。
Definition: ShellHelper.h:138
void ScrDraw(_tOut buf, _tGen &&f)
全屏幕描点。
Definition: ShellHelper.h:307
子部件绘制仿函数。
Definition: ShellHelper.h:477
errno_t NowShellTo(const shared_ptr< Shell > &hShl)
向句柄指定的 Shell 对象转移线程控制权。
Definition: ShellHelper.h:187
YF_API void SwitchVisibleToFront(IWidget &)
切换部件显示状态并请求提升至前端。
桌面背景。
Definition: ystyle.h:181
_tWidget _fCallable && f
Definition: ywgtevt.h:597
std::chrono::nanoseconds now_tick
Definition: ShellHelper.h:392
yconstexpr Encoding UTF_32LE(csUTF32LE)
yconstexpr Encoding GBK(csGBK)
序列设置仿函数。
Definition: ShellHelper.h:454
Color
控制台颜色枚举。
Definition: Video.h:458
Timers::HighResolutionClock::time_point base_tick
Definition: ShellHelper.h:50
yconstexpr Priority NormalPriority(0x80)
默认消息优先级。
void SetShellToNew()
通过主消息队列向新建 Shell 对象转移控制权。
Definition: ShellHelper.h:233
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
std::pair< Encoding, const ucs2_t * > EncodingInfoItem
编码信息项目。
Definition: ShellHelper.h:95
void operator()(IWidget &wgt)
Definition: ShellHelper.h:491
virtual DefDeDtor(GShellSession) DefGetter(const ynothrow
int errno_t
Definition: ydef.h:568
void ReleaseStored()
释放全局 Shell 。
Definition: ShellHelper.h:166
#define YB_LIKELY(expr)
Definition: ydef.h:297
void SetBufferRendererAndText(_tWidget &wgt, const String &s)
设置部件渲染器为 BufferedRenderer 及部件的 Text 成员。
Definition: ShellHelper.h:504
yconstexpr Encoding UTF_8(csUTF8)
std::reference_wrapper< IWidget > Container
容器。
Definition: ShellHelper.h:461
颜色。
Definition: Video.h:339
const PixelType * ConstBitmapPtr
Definition: Video.h:296
void CallStoredAtOnce()
封装向全局 Shell 管理器内的对象转移控制权。
Definition: ShellHelper.h:255
YF_API shared_ptr< TextList::ListType > FetchVisualStyleNames(String=u"<Default>")
取视觉样式名称。
u8 Priority
消息优先级。
Definition: ymsg.h:52
void ResetDesktop(Desktop &dsk, Devices::Screen &scr)
复位桌面。
Definition: ShellHelper.h:279
#define YAssert(_expr, _msg)
Definition: cassert.h:73
调试计时器。
Definition: ShellHelper.h:46