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 
74  : Application()
76  , p_hosted()
77 #endif
78 {
79  YAssert(!ApplicationPtr, "Duplicate instance found.");
80  ApplicationPtr = this;
82 #if YF_Hosted
83  p_hosted = make_unique<Host::Environment>();
84 #endif
85 }
86 
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
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 
129 {
130  YAssertNonnull(ApplicationPtr);
131  return *ApplicationPtr;
132 }
133 
134 /* extern */Application&
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 
YF_API Application & FetchAppInstance() ynothrow
取应用程序实例。
YF_API void PostMessage(const Message &, Messaging::Priority) ynothrow
全局默认队列消息发送函数。
Definition: yapp.cpp:75
#define YF_Hosted
宿主环境。
Definition: Platform.h:138
Messaging::Priority UIResponseLimit
用户界面输入响应阈值。
Application()
无参数构造。
Definition: yapp.cpp:34
_tApp & FetchGlobalInstance() ynothrow
#define SM_Quit
Definition: ymsgdef.h:62
~GUIApplication() override
析构:释放资源。
YF_API void Execute(GUIApplication &, shared_ptr< Shell >=make_shared< Shells::GUIShell >())
执行程序主消息循环。
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
程序实例。
Definition: yapp.h:46
#define SM_Input
Definition: ymsgdef.h:67
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
bool Activate(const shared_ptr< Shell > &hShl)
激活 Shell 对象:控制权转移给此对象以维持单线程运行。
Definition: yapp.h:164
#define YAssertNonnull(_expr)
Definition: cassert.h:81
if(YB_LIKELY(!error)) if(YB_LIKELY(!(error
YF_API void Uninitialize() ynothrow
反初始化。
YF_API void InitializeEnviornment()
初始化环境。
记录日志的异常事件类。
Definition: yexcept.h:58
void OnGotMessage(const Message &) override
处理消息:分发消息。
Definition: yapp.cpp:49
bool DealMessage()
处理当前消息。
平台相关的应用程序类。
GUIApplication()
private 构造函数:非内联。
u8 Priority
消息优先级。
Definition: ymsg.h:52
#define YAssert(_expr, _msg)
Definition: cassert.h:73