YSTest: GUIApplication.cpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
GUIApplication.cpp
浏览该文件的文档.
1 /*
2 © 2013-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_GUIApplication
30 #include YFM_Helper_Host
31 #include YFM_Helper_Initialization
32 #if YF_Multithread == 1
33 # include <thread> // for std::this_thread::*;
34 #endif
35
36 namespace YSLib
37 {
38
39 using namespace Drawing;
40
41 namespace
42 {
43
45 GUIApplication* ApplicationPtr;
46
51 inline Message
52 FetchIdleMessage()
53 {
54 return Message(SM_Input);
55 }
56
61 inline void
62 Idle(Messaging::Priority prior)
63 {
64 // NOTE: Wait for GUI input of any shells. Post message for specific shell
65 // would cause low performance when there are many candidate messages
66 // of distinct shells.
67 PostMessage(FetchIdleMessage(), prior);
68 }
69
70 } // unnamed namespace;
71
72
73 GUIApplication::GUIApplication()
74 : Application()
75 #if YF_Hosted
76 , p_hosted()
77 #endif
78 {
79 YAssert(!ApplicationPtr, "Duplicate instance found.");
80 ApplicationPtr = this;
81 InitializeEnviornment();
82 #if YF_Hosted
83 p_hosted = make_unique<Host::Environment>();
84 #endif
85 }
86
87 GUIApplication::~GUIApplication()
88 {
89 Uninitialize();
90 ApplicationPtr = {};
91 }
92
93 #if YF_Hosted
94 Host::Environment&
95 GUIApplication::GetHost()
96 {
97 YAssertNonnull(p_hosted);
98 return *p_hosted;
99 }
100 #endif
101
102 bool
103 GUIApplication::DealMessage()
104 {
105 return AccessQueue([this](MessageQueue& mq){
106 using namespace Shells;
107
108 if(mq.empty())
109 // Idle(UIResponseLimit);
110 OnGotMessage(FetchIdleMessage());
111 else
112 {
113 const auto i(mq.cbegin());
114
115 if(YB_UNLIKELY(i->second.GetMessageID() == SM_Quit))
116 return false;
117 if(i->first < UIResponseLimit)
118 Idle(UIResponseLimit);
119 OnGotMessage(i->second);
120 mq.erase(i);
121 }
122 return true;
123 });
124 }
125
126
127 GUIApplication&
128 FetchGlobalInstance() ynothrow
129 {
130 YAssertNonnull(ApplicationPtr);
131 return *ApplicationPtr;
132 }
133
134 /* extern */ Application&
135 FetchAppInstance() ynothrow
136 {
137 return FetchGlobalInstance();
138 }
139
140
141 void
142 Execute(GUIApplication& app, shared_ptr<Shell> p_shl)
143 {
144 #if YF_Hosted
145 Host::FetchEnvironment().ExitOnAllWindowThreadCompleted = true;
146 #endif
147 if(YB_UNLIKELY(!Activate(p_shl)))
148 throw LoggedEvent("Failed activating the main shell.");
149 while(app.DealMessage())
150 ;
151 }
152
153 } // namespace YSLib;
154
YSLib::FetchAppInstance
YF_API Application & FetchAppInstance() ynothrow
取应用程序实例。
Definition: GUIApplication.cpp:135
YSLib::PostMessage
YF_API void PostMessage(const Message &, Messaging::Priority) ynothrow
全局默认队列消息发送函数。
Definition: yapp.cpp:75
YF_Hosted
#define YF_Hosted
宿主环境。
Definition: Platform.h:138
YSLib::GUIApplication::UIResponseLimit
Messaging::Priority UIResponseLimit
用户界面输入响应阈值。
Definition: GUIApplication.h:68
YSLib::Application::Application
Application()
无参数构造。
Definition: yapp.cpp:34
YSLib::FetchGlobalInstance
_tApp & FetchGlobalInstance() ynothrow
Definition: GUIApplication.h:111
SM_Quit
#define SM_Quit
Definition: ymsgdef.h:62
YSLib::GUIApplication::~GUIApplication
~GUIApplication() override
析构：释放资源。
Definition: GUIApplication.cpp:87
YSLib::Execute
YF_API void Execute(GUIApplication &, shared_ptr< Shell >=make_shared< Shells::GUIShell >())
执行程序主消息循环。
Definition: GUIApplication.cpp:142
YB_UNLIKELY
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
YSLib::Application
程序实例。
Definition: yapp.h:46
SM_Input
#define SM_Input
Definition: ymsgdef.h:67
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::Activate
bool Activate(const shared_ptr< Shell > &hShl)
激活 Shell 对象：控制权转移给此对象以维持单线程运行。
Definition: yapp.h:164
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:81
YSLib::Drawing::if
if(YB_LIKELY(!error)) if(YB_LIKELY(!(error
YSLib::Uninitialize
YF_API void Uninitialize() ynothrow
反初始化。
Definition: Initialization.cpp:366
YModules.h
YSLib::InitializeEnviornment
YF_API void InitializeEnviornment()
初始化环境。
Definition: Initialization.cpp:231
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: yexcept.h:58
YSLib::Application::OnGotMessage
void OnGotMessage(const Message &) override
处理消息：分发消息。
Definition: yapp.cpp:49
YSLib::GUIApplication::DealMessage
bool DealMessage()
处理当前消息。
Definition: GUIApplication.cpp:103
YSLib::GUIApplication
平台相关的应用程序类。
Definition: GUIApplication.h:51
YSLib::GUIApplication::GUIApplication
GUIApplication()
private 构造函数：非内联。
Definition: GUIApplication.cpp:73
YSLib::Messaging::Priority
u8 Priority
消息优先级。
Definition: ymsg.h:52
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:73