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 
38 
39 #ifndef NDEBUG
40 
46 {
47 protected:
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>>
67 {
68 public:
69  typedef _tShell Shell;
70  typedef _tConnection Connection;
71 
72 private:
74 
75 public:
76  template<typename... _tParam>
77  GShellSession(_tParam&&... args)
78  : conn(yforward(args)...)
79  {}
80 
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 
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&
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>
178 {
179  return GLocalStaticCache<_tShl, shared_ptr<Shell>>::GetPointer();
180 }
181 
186 template<class _tShl>
187 inline void
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
221 {
222  return NowShellTo(new _tShl());
223 }
224 
229 template<class _tShl>
230 inline errno_t
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
255 {
256  SetShellTo(make_shared<_tShl>());
257 }
258 
263 template<class _tShl>
264 inline void
266 {
267  SetShellTo(FetchStored<_tShl>());
268 }
269 
274 template<class _tShl>
275 inline void
277 {
278  NowShellToStored<_tShl>();
279 }
280 
285 template<class _tShl>
286 inline void
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
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>
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
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)
390 
391 
396 YF_API shared_ptr<UI::TextList::ListType>
398 
399 
405 {
406 private:
416 
421 
422 public:
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 
455 
460 YF_API void
461 SwitchVisible(IWidget&);
462 
463 
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 
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 
527 
528 
529 YSL_END
530 
531 #endif
532