YSTest: YApplication.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YApplication.cpp
浏览该文件的文档.
1 /*
2 © 2009-2016, 2020 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 "YSLib/Core/YModules.h"
29 #include YFM_YSLib_Core_YApplication
30 #include YFM_YSLib_Core_YException // for ExtractAndTrace;
31
32 namespace YSLib
33 {
34
35 Application::Application()
36 : Shell()
37 {}
38 Application::~Application()
39 {
40 // See $2020-04 @ %Documentation::Workflow.
41 qMain.clear();
42 if(YB_UNLIKELY(hShell))
43 YTraceDe(Warning, "Active shell found with count %ld.",
44 hShell.use_count());
45 hShell = {};
46 YTraceDe(Notice, "Uninitialization entered with %zu handler(s) to be"
47 " called.", on_exit.size());
48 // NOTE: This is needed because the standard containers (and adaptors)
49 // guarantee nothing about destruction order of contained elements.
50 while(!on_exit.empty())
51 on_exit.pop();
52 }
53
54 void
55 Application::OnGotMessage(const Message& msg)
56 {
57 TryExpr(Deref(GetShellHandle()).OnGotMessage(msg))
58 CatchIgnore(Messaging::MessageSignal&)
59 #ifndef NDEBUG
60 CatchExpr(std::exception& e, YTraceDe(Emergent, "Unexpected exception."),
61 ExtractAndTrace(e, Emergent), throw)
62 CatchExpr(..., YTraceDe(Emergent, "Unknown unexpected exception."), throw)
63 #endif
64 }
65
66 bool
67 Application::Switch(shared_ptr<Shell>& h) ynothrow
68 {
69 if(YB_LIKELY(hShell != h))
70 {
71 std::swap(hShell, h);
72 return true;
73 }
74 return {};
75 }
76
77
78 void
79 PostMessage(const Message& msg, Messaging::Priority prior)
80 {
81 FetchAppInstance().AccessQueue([=, &msg](MessageQueue& mq){
82 mq.Push(msg, prior);
83 });
84 }
85
86 void
87 PostQuitMessage(int n, Messaging::Priority prior)
88 {
89 YTraceDe(Informative, "Ready to post quit message with exit code = %d,"
90 " priority = %u.", n, unsigned(prior));
91 FetchAppInstance().AccessQueue([=](MessageQueue& mq){
92 mq.Emplace(prior, SM_Set, shared_ptr<Shell>());
93 mq.Emplace(prior, SM_Quit, n);
94 });
95 }
96
97 } // namespace YSLib;
98
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
ystdex::cpp2017::swap
void swap(optional< _type > &x, optional< _type > &y)
Definition: optional.h:888
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
YSLib::PostQuitMessage
YF_API void PostQuitMessage(int=0, Messaging::Priority p=DefaultQuitPriority)
以指定错误码和优先级发起 Shell 终止请求。
Definition: YApplication.cpp:87
YSLib::shared_ptr< Shell >
shared_ptr< Shell >
Definition: GUIApplication.h:308
platform::Descriptions::Emergent
@ Emergent
Definition: YCommon.h:179
YSLib::FetchAppInstance
YF_API Application & FetchAppInstance()
取应用程序实例。
Definition: GUIApplication.cpp:295
YSLib::Application::on_exit
stack< any > on_exit
初始化守卫。
Definition: YApplication.h:53
YSLib::Application::~Application
~Application() override
析构：释放 Shell 所有权和其它资源。
Definition: YApplication.cpp:38
YModules.h
YSLib 核心模块配置文件。
SM_Quit
#define SM_Quit
Definition: YMessageDefinition.h:61
YSLib::Messaging::MessageQueue::Push
void Push(const Message &, Priority)
以指定优先级插入参数指定的消息至消息队列中。
Definition: YMessage.cpp:70
YSLib::Application::Application
Application()
无参数构造：默认构造。
Definition: YApplication.cpp:35
YSLib::Messaging::Priority
std::uint8_t Priority
消息优先级。
Definition: YMessage.h:52
platform::Descriptions::Notice
@ Notice
Definition: YCommon.h:184
platform::Descriptions::Informative
@ Informative
Definition: YCommon.h:185
YSLib::Messaging::MessageQueue
消息队列。
Definition: YMessage.h:159
YSLib::ExtractAndTrace
YF_API void ExtractAndTrace(const std::exception &, RecordLevel=Err) ynothrow
使用 TraceException 展开和跟踪异常类型和信息。
YSLib::Application::OnGotMessage
void OnGotMessage(const Message &) override
处理消息：分发消息。
Definition: YApplication.cpp:55
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:407
YSLib::Shells::Shell
外壳程序：实现运行期控制流映像语义。
Definition: YShell.h:42
YSLib::PostMessage
const shared_ptr< Shell > &hShl ImplRet(FetchAppInstance().Switch(shared_ptr< Shell >(hShl))) yconstexpr const Messaging YF_API void PostMessage(const Message &, Messaging::Priority)
默认退出消息优先级。
Definition: YApplication.cpp:79
YSLib::Application::hShell
shared_ptr< Shell > hShell
当前 Shell 句柄：指示当前线程空间中运行的 Shell 。
Definition: YApplication.h:75
platform::Descriptions::Warning
@ Warning
Definition: YCommon.h:183
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YSLib::Messaging::MessageSignal
消息信号：表示单一处理中断的异常。
Definition: YMessage.h:299
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YB_LIKELY
#define YB_LIKELY(_expr)
Definition: ydef.h:827
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
platform::Deref
YB_PURE yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:547
SM_Set
#define SM_Set
Definition: YMessageDefinition.h:59
YSLib::Application::qMain
MessageQueue qMain
主消息队列。
Definition: YApplication.h:70
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:828
YSLib::Application::Switch
bool Switch(shared_ptr< Shell > &) ynothrow
切换：若参数非空，和线程空间中当前运行的 Shell 的句柄交换。
Definition: YApplication.cpp:67
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
YSLib::Messaging::Message
消息。
Definition: YMessage.h:67