YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ShellHelper.cpp
浏览该文件的文档.
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 #include "Helper/YModules.h"
29 #include YFM_Helper_ShellHelper
30 #include <cstdio> // for std::sprintf;
31 
32 namespace YSLib
33 {
34 
35 #ifndef NDEBUG
37  : event_info(str), base_tick()
38 {
39  std::printf("Start tick of [%s] :\n", event_info.c_str());
41 }
43 {
44  const double t((Timers::HighResolutionClock::now() - base_tick).count()
45  / 1e6);
46 
47  std::printf("Performed [%s] in: %f milliseconds.\n",
48  event_info.c_str(), t);
49 }
50 #endif
51 
52 
53 void
55 {
56  auto& app(FetchGlobalInstance());
57 
58  app.AccessQueue([&](MessageQueue& mq){
59  return mq.Remove(app.UIResponseLimit);
60  });
61 }
62 
63 
64 namespace
65 {
66 
68 inline void
69 snftime(char* buf, size_t /*n*/, const std::tm& tm,
70  const char* format = DefaultTimeFormat)
71 {
72  // FIXME: correct behavior for time with BC date(i.e. tm_year < -1900);
74  // tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
75  // FIXME: use std::snprintf;
76  std::sprintf(buf, format, tm.tm_year + 1900,
77  tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
78 }
79 
80 } // unnamed namespace;
81 
82 
83 const char*
84 TranslateTime(const std::tm& tm, const char* format)
85 {
86  static char str[80];
87 
88  // NOTE: 'std::strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &tm)'
89  // is correct but it makes the object file too large.
90  snftime(str, 80, tm, format);
91  return str;
92 }
93 const char*
94 TranslateTime(const std::time_t& t, const char* format) ythrow(GeneralEvent)
95 {
96  auto p(std::localtime(&t));
97 
98  if(YB_UNLIKELY(!p))
99  throw GeneralEvent("Get broken-down time object failed"
100  " @ TranslateTime#2;");
101  return TranslateTime(*p, format);
102 }
103 
104 
105 shared_ptr<UI::TextList::ListType>
107 {
108  const auto& mFamilies(FetchDefaultFontCache().GetFamilyIndices());
109 
110  return ystdex::make_shared<UI::TextList::ListType>(mFamilies.cbegin()
111  | ystdex::get_key, mFamilies.cend() | ystdex::get_key);
112 }
113 
114 
115 FPSCounter::FPSCounter(std::chrono::nanoseconds s)
116  : last_tick(GetHighResolutionTicks()), now_tick(), refresh_count(1),
117  MinimalInterval(s)
118 {}
119 
120 size_t
122 {
123  const std::chrono::nanoseconds tmp_tick(GetHighResolutionTicks());
124 
125  if(YB_UNLIKELY(last_tick + MinimalInterval < tmp_tick))
126  {
128 
129  const u32 r(1000000000000ULL * refresh_count
130  / ((now_tick = tmp_tick) - last_tick).count());
131 
132  refresh_count = 1;
133  return r;
134  }
135  else
136  ++refresh_count;
137  return 0;
138 }
139 
140 namespace UI
141 {
142 
143 void
145 {
146  SetVisibleOf(wgt, !IsVisible(wgt));
147  Invalidate(wgt);
148 }
149 
150 void
152 {
153  SetVisibleOf(wgt, !IsVisible(wgt));
154  RequestToFront(wgt);
155 }
156 
157 
158 shared_ptr<TextList::ListType>
160 {
161  const auto& styles(FetchGUIState().Styles);
162  auto p_list(ystdex::make_shared<TextList::ListType>(styles.cbegin()
163  | ystdex::get_key, styles.cend() | ystdex::get_key));
164 
165  YAssert(bool(p_list) && !p_list->empty(),
166  "Invalid style mapping state found");
167  p_list->front() = std::move(default_name);
168  return ystdex::make_shared<TextList::ListType>(p_list->cbegin(),
169  p_list->cend());
170 }
171 
172 } // namespace UI;
173 
174 } // namespace YSLib;
175 
std::string event_info
Definition: ShellHelper.h:49
size_t refresh_count
刷新计数。
Definition: ShellHelper.h:397
std::uint32_t u32
Definition: yadaptor.h:69
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
YF_API void Invalidate(IWidget &, const Rect &)
无效化:使相对于部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.cpp:111
static time_point now() ynothrow
Definition: ytimer.h:61
YF_API const char * TranslateTime(const std::tm &, const char *=DefaultTimeFormat)
格式化时间字符串。
Definition: ShellHelper.cpp:84
std::chrono::nanoseconds last_tick
内部计数。
Definition: ShellHelper.h:392
_tApp & FetchGlobalInstance() ynothrow
YF_API shared_ptr< UI::TextList::ListType > FetchFontFamilyNames()
取字型家族名称。
yconstexpr const char * DefaultTimeFormat("%04u-%02u-%02u %02u:%02u:%02u")
默认时间格式字符串。
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: yexcept.h:51
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
YSLib 标准字符串(使用 UCS-2 作为内部编码)。
Definition: ystring.h:47
now_tick size_t Refresh()
刷新:更新计数器内部计数。
YF_API void RemoveGlobalTasks()
从全局消息队列中移除所有后台消息。
Definition: ShellHelper.cpp:54
_tWidget & wgt
Definition: ywgtevt.h:596
YF_API void SwitchVisible(IWidget &)
切换部件显示状态并无效化。
YF_API std::uint64_t GetHighResolutionTicks()
取高精度 tick 数。
Definition: Timer.cpp:157
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
#define ythrow(...)
YSLib 动态异常规范:根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
std::chrono::nanoseconds MinimalInterval
计时间隔下界。
Definition: ShellHelper.h:404
YF_API void SwitchVisibleToFront(IWidget &)
切换部件显示状态并请求提升至前端。
std::chrono::nanoseconds now_tick
Definition: ShellHelper.h:392
Timers::HighResolutionClock::time_point base_tick
Definition: ShellHelper.h:50
DebugTimer(const std::string &str="")
Definition: ShellHelper.cpp:36
YF_API void RequestToFront(IWidget &)
请求提升至容器前端。
Definition: ywidget.cpp:199
bounds & r
Definition: ydraw.h:220
const first_tag get_key
Definition: iterator.hpp:784
YF_API shared_ptr< TextList::ListType > FetchVisualStyleNames(String=u"<Default>")
取视觉样式名称。
FPSCounter(std::chrono::nanoseconds={})
构造:使用指定计时间隔下界。
#define YAssert(_expr, _msg)
Definition: cassert.h:73