YSTest: ShellHelper.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ShellHelper.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2010 - 2013.
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 "YCLib/Debug.h"
34 #include "YSLib/Service/ytimer.h" // for Timers::HighResolutionClock;
35 #include "YSLib/UI/textlist.h" // for UI::TextList::ListType;
36
37 YSL_BEGIN
38
39 #ifndef NDEBUG
40
45 class YF_API DebugTimer
46 {
47 protected:
48 std::string event_info;
49 Timers::HighResolutionClock::time_point base_tick;
50
51 public:
52 DebugTimer(const std::string& str = "");
53 ~DebugTimer();
54 };
55 # define YSL_DEBUG_DECL_TIMER(_name, ...) DebugTimer _name(__VA_ARGS__);
56 #else
57 # define YSL_DEBUG_DECL_TIMER(...)
58 #endif
59
60
65 template<class _tShell, typename _tConnection = std::reference_wrapper<_tShell>>
66 class GShellSession
67 {
68 public:
69 typedef _tShell Shell;
70 typedef _tConnection Connection;
71
72 private:
73 Connection conn;
74
75 public:
76 template<typename... _tParam>
77 GShellSession(_tParam&&... args)
78 : conn(yforward(args)...)
79 {}
80
81 virtual DefEmptyDtor(GShellSession)
82
83 DefGetter(const ynothrow, _tShell&, Shell, conn)
84 };
85
86
87 YSL_BEGIN_NAMESPACE(Drawing)
88
94 template<typename _type>
95 string
96 to_string(const GBinaryGroup<_type>& val)
97 {
98 using YSLib::to_string;
99
100 return "(" + to_string(val.X) + ", " + to_string(val.Y) + ')';
101 }
102 YF_API string
103 to_string(const Size&);
104 YF_API string
105 to_string(const Rect&);
107
108 YSL_END_NAMESPACE(Drawing)
109
110 YSL_BEGIN_NAMESPACE(Text)
111
116 typedef std::pair<Encoding, const ucs2_t*> EncodingInfoItem;
117
122 yconstexpr EncodingInfoItem Encodings[] = {{CharSet::UTF_8, u"UTF-8"},
123 {CharSet::GBK, u"GBK"}, {CharSet::UTF_16BE, u"UTF-16 Big Endian"},
124 {CharSet::UTF_16LE, u"UTF-16 Little Endian"},
125 {CharSet::UTF_32BE, u"UTF-32 Big Endian"},
126 {CharSet::UTF_32LE, u"UTF-16 Little Endian"}};
127
128 YSL_END_NAMESPACE(Text)
129
130
131
135
136 //句柄语法糖。
137
138
142 template<class _type, class _tHandle>
143 inline _type&
144 HandleToReference(_tHandle h) ythrow(std::bad_cast)
145 {
146 _type* _tmp(dynamic_cast<_type*>(raw(h)));
147
148 if(YB_LIKELY(!_tmp))
149 throw std::bad_cast();
150 return *_tmp;
151 }
152
157 template<class _tShell>
158 inline _tShell&
159 FetchShell()
160 {
161 auto hShl(FetchShellHandle());
162
163 YAssert(bool(hShl), "Null handle found.");
164
165 return ystdex::polymorphic_downcast<_tShell&>(*hShl);
166 }
167
168
169 //全局函数。
170
175 template<class _tShl>
176 inline shared_ptr<Shell>
177 FetchStored()
178 {
179 return GLocalStaticCache<_tShl, shared_ptr<Shell>>::GetPointer();
180 }
181
186 template<class _tShl>
187 inline void
188 ReleaseStored()
189 {
190 GLocalStaticCache<_tShl, shared_ptr<Shell>>::Release();
191 }
192
193
198 inline bool
199 IsNowShell(const shared_ptr<Shell>& hShl)
200 {
201 return FetchAppInstance().GetShellHandle() == hShl;
202 }
203
208 inline errno_t
209 NowShellTo(const shared_ptr<Shell>& hShl)
210 {
211 return -!Activate(hShl);
212 }
213
218 template<class _tShl>
219 inline errno_t
220 NowShellToNew()
221 {
222 return NowShellTo(new _tShl());
223 }
224
229 template<class _tShl>
230 inline errno_t
231 NowShellToStored()
232 {
233 return NowShellTo(FetchStored<_tShl>());
234 }
235
240 inline void
241 SetShellTo(const shared_ptr<Shell>& hShl, Messaging::Priority prior = 0x80)
242 {
243 // NOTE: It would make the message loop in dead lock when called more
244 // than once specifying on same destination shell.
245 PostMessage<SM_SET>(prior, hShl);
246 }
247
252 template<class _tShl>
253 inline void
254 SetShellToNew()
255 {
256 SetShellTo(make_shared<_tShl>());
257 }
258
263 template<class _tShl>
264 inline void
265 SetShellToStored()
266 {
267 SetShellTo(FetchStored<_tShl>());
268 }
269
274 template<class _tShl>
275 inline void
276 CallStoredAtOnce()
277 {
278 NowShellToStored<_tShl>();
279 }
280
285 template<class _tShl>
286 inline void
287 CallStored()
288 {
289 SetShellToStored<_tShl>();
290 }
291
292
299 inline void
300 ResetDesktop(Desktop& dsk, Devices::Screen& scr)
301 {
302 dsk.~Desktop();
303 new(&dsk) Desktop(scr);
304 }
305
306
307 //资源相关定义和函数。
308
309 YSL_BEGIN_NAMESPACE(Drawing)
310
311
312 inline Color
313 GenerateRandomColor()
314 {
315 //使用 std::time(0) 初始化随机数种子在 DeSmuME 上无效。
316 // std::srand(std::time(0));
317 return Color(std::rand(), std::rand(), std::rand(), 1);
318 }
319
325 template<typename _tOut, typename _tGen>
326 void
327 ScrDraw(_tOut buf, _tGen&& f)
328 {
329 for(SDst y(0); y < MainScreenHeight; ++y)
330 for(SDst x(0); x < MainScreenWidth; yunseq(++x, ++buf))
331 *buf = yforward(f)(x, y);
332 }
333
338 inline shared_ptr<Image>
339 CreateSharedScreenImage(ConstBitmapPtr p)
340 {
341 return make_shared<Image>(p, MainScreenWidth, MainScreenHeight);
342 }
343
348 template<typename _tPixel>
349 _tPixel*
350 CreateRawBitmap(const _tPixel* s, size_t n)
351 {
352 if(YB_LIKELY(s && n))
353 {
354 size_t size(sizeof(_tPixel) * n);
355 _tPixel* d(new _tPixel[size]);
356
357 std::copy_n(s, size, d);
358 return d;
359 }
360 return nullptr;
361 }
362
363 YSL_END_NAMESPACE(Drawing)
364
365
369 YF_API void
370 RemoveGlobalTasks();
371
372
377 yconstexpr const char* DefaultTimeFormat("%04u-%02u-%02u %02u:%02u:%02u");
378
384 YF_API const char*
385 TranslateTime(const std::tm&, const char* = DefaultTimeFormat);
386 YF_API const char*
387 TranslateTime(const std::time_t&, const char* = DefaultTimeFormat)
388 ythrow(GeneralEvent);
390
391
396 YF_API shared_ptr<UI::TextList::ListType>
397 FetchFontFamilyNames();
398
399
404 class YF_API FPSCounter
405 {
406 private:
413 u64 last_tick;
414 u64 now_tick;
416
420 u32 refresh_count;
421
422 public:
427 u64 MinimalInterval;
428
433 FPSCounter(u64 = 0);
434
440 DefGetter(const ynothrow, u64, LastTick, last_tick)
441 DefGetter(const ynothrow, u64, NowTick, now_tick)
443
448 u32
449 Refresh();
450 };
451
453
454 YSL_BEGIN_NAMESPACE(UI)
455
460 YF_API void
461 SwitchVisible(IWidget&);
462
463
468 struct ContainerSetter
469 {
470 public:
475 std::reference_wrapper<IWidget> Container;
476
477 ContainerSetter(IWidget& con)
478 : Container(con)
479 {}
480
481 void
482 operator()(IWidget& wgt)
483 {
484 SetContainerPtrOf(wgt, &Container.get());
485 }
486 };
487
488
493 struct ChildPainter
494 {
495 public:
500 std::reference_wrapper<const PaintContext> Context;
501
502 ChildPainter(const PaintContext& pc)
503 : Context(pc)
504 {}
505
506 void
507 operator()(IWidget& wgt)
508 {
509 PaintChild(wgt, Context);
510 }
511 };
512
513
518 template<class _tWidget>
519 inline void
520 SetBufferRendererAndText(_tWidget& wgt, const String& s)
521 {
522 wgt.SetRenderer(make_unique<BufferedRenderer>()),
523 wgt.Text = s;
524 }
525
526 YSL_END_NAMESPACE(UI)
527
528
529 YSL_END
530
531 #endif
532