YSTest: ShellHelper.cpp 源文件

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
36 DebugTimer::DebugTimer(const std::string& str)
37 : event_info(str), base_tick()
38 {
39 std::printf("Start tick of [%s] :\n", event_info.c_str());
40 base_tick = Timers::HighResolutionClock::now();
41 }
42 DebugTimer::~DebugTimer()
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
54 RemoveGlobalTasks()
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>
106 FetchFontFamilyNames()
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
121 FPSCounter::Refresh()
122 {
123 const std::chrono::nanoseconds tmp_tick(GetHighResolutionTicks());
124
125 if(YB_UNLIKELY(last_tick + MinimalInterval < tmp_tick))
126 {
127 last_tick = now_tick;
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
144 SwitchVisible(IWidget& wgt)
145 {
146 SetVisibleOf(wgt, !IsVisible(wgt));
147 Invalidate(wgt);
148 }
149
150 void
151 SwitchVisibleToFront(IWidget& wgt)
152 {
153 SetVisibleOf(wgt, !IsVisible(wgt));
154 RequestToFront(wgt);
155 }
156
157
158 shared_ptr<TextList::ListType>
159 FetchVisualStyleNames(String default_name)
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
YSLib::DebugTimer::event_info
std::string event_info
Definition: ShellHelper.h:49
YSLib::FPSCounter::refresh_count
size_t refresh_count
刷新计数。
Definition: ShellHelper.h:397
YSLib::DebugTimer::~DebugTimer
~DebugTimer()
Definition: ShellHelper.cpp:42
YSLib::u32
std::uint32_t u32
Definition: yadaptor.h:69
YSLib::UI::FetchGUIState
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
YSLib::UI::Invalidate
YF_API void Invalidate(IWidget &, const Rect &)
无效化：使相对于部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.cpp:111
YSLib::UI::IsVisible
IsVisible
Definition: ywidget.h:96
YSLib::Timers::HighResolutionClock::now
static time_point now() ynothrow
Definition: ytimer.h:61
YSLib::TranslateTime
YF_API const char * TranslateTime(const std::tm &, const char *=DefaultTimeFormat)
格式化时间字符串。
Definition: ShellHelper.cpp:84
YSLib::FPSCounter::last_tick
std::chrono::nanoseconds last_tick
内部计数。
Definition: ShellHelper.h:392
YSLib::FetchGlobalInstance
_tApp & FetchGlobalInstance() ynothrow
Definition: GUIApplication.h:111
YSLib::FetchFontFamilyNames
YF_API shared_ptr< UI::TextList::ListType > FetchFontFamilyNames()
取字型家族名称。
Definition: ShellHelper.cpp:106
YSLib::DefaultTimeFormat
yconstexpr const char * DefaultTimeFormat("%04u-%02u-%02u %02u:%02u:%02u")
默认时间格式字符串。
YSLib::GeneralEvent
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: yexcept.h:51
YB_UNLIKELY
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: ystring.h:47
YSLib::FPSCounter::Refresh
now_tick size_t Refresh()
刷新：更新计数器内部计数。
Definition: ShellHelper.cpp:121
YSLib::RemoveGlobalTasks
YF_API void RemoveGlobalTasks()
从全局消息队列中移除所有后台消息。
Definition: ShellHelper.cpp:54
YSLib::UI::wgt
_tWidget & wgt
Definition: ywgtevt.h:596
YSLib::UI::SwitchVisible
YF_API void SwitchVisible(IWidget &)
切换部件显示状态并无效化。
Definition: ShellHelper.cpp:144
platform::GetHighResolutionTicks
YF_API std::uint64_t GetHighResolutionTicks()
取高精度 tick 数。
Definition: Timer.cpp:157
YSLib::FetchDefaultFontCache
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。
Definition: Initialization.cpp:394
YSLib::string
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
ythrow
#define ythrow(...)
YSLib 动态异常规范：根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
YSLib::FPSCounter::MinimalInterval
std::chrono::nanoseconds MinimalInterval
计时间隔下界。
Definition: ShellHelper.h:404
YSLib::UI::SwitchVisibleToFront
YF_API void SwitchVisibleToFront(IWidget &)
切换部件显示状态并请求提升至前端。
Definition: ShellHelper.cpp:151
YSLib::FPSCounter::now_tick
std::chrono::nanoseconds now_tick
Definition: ShellHelper.h:392
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
YSLib::DebugTimer::base_tick
Timers::HighResolutionClock::time_point base_tick
Definition: ShellHelper.h:50
YModules.h
YSLib::DebugTimer::DebugTimer
DebugTimer(const std::string &str="")
Definition: ShellHelper.cpp:36
YSLib::UI::RequestToFront
YF_API void RequestToFront(IWidget &)
请求提升至容器前端。
Definition: ywidget.cpp:199
YSLib::Drawing::r
bounds & r
Definition: ydraw.h:220
YSLib::Drawing::format
format(fmt)
Definition: Image.cpp:242
ystdex::get_key
const first_tag get_key
Definition: iterator.hpp:784
YSLib::UI::FetchVisualStyleNames
YF_API shared_ptr< TextList::ListType > FetchVisualStyleNames(String=u"<Default>")
取视觉样式名称。
Definition: ShellHelper.cpp:159
YSLib::FPSCounter::FPSCounter
FPSCounter(std::chrono::nanoseconds={})
构造：使用指定计时间隔下界。
Definition: ShellHelper.cpp:115
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:73