YSTest: YApplication.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YApplication.h
浏览该文件的文档.
1 /*
2 © 2009-2016, 2018-2019 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 #ifndef YSL_INC_Core_YApplication_h_
29 #define YSL_INC_Core_YApplication_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YShell // for Shell, any, stack,
33 // std::is_nothrow_copy_constructible, locked_ptr, ystdex::decay_t,
34 // YSLib::unchecked_any_cast;
35 #include <ystdex/scope_guard.hpp> // for ystdex::unique_guard;
36
37 namespace YSLib
38 {
39
40 using Messaging::MessageQueue;
41
46 class YF_API Application : public Shell
47 {
48 private:
53 stack<any> on_exit{};
54 /*
55 \brief 初始化守卫互斥锁。
56 \since build 725
57 */
58 recursive_mutex on_exit_mutex{};
59 /*
60 \brief 主消息队列互斥锁。
61 \since build 551
62 */
63 recursive_mutex queue_mutex{};
64
65 protected:
70 MessageQueue qMain{};
75 shared_ptr<Shell> hShell{};
76
77 public:
79 Application();
80
82 ~Application() override;
83
85 DefGetter(const ynothrow, shared_ptr<Shell>, ShellHandle, hShell)
86
87
92 template<typename _func>
93 auto
94 AccessQueue(_func f) -> decltype(f(qMain))
95 {
96 lock_guard<recursive_mutex> lck(queue_mutex);
97
98 return f(qMain);
99 }
100
107 template<typename _tParam>
108 void
109 AddExit(_tParam&& arg)
110 {
111 lock_guard<recursive_mutex> lck(on_exit_mutex);
112
113 PushExit(yforward(arg));
114 }
115
116 template<typename _func>
117 void
118 AddExitGuard(_func f)
119 {
120 static_assert(std::is_nothrow_copy_constructible<_func>(),
121 "Invalid guard function found.");
122 lock_guard<recursive_mutex> lck(on_exit_mutex);
123
124 TryExpr(PushExit(ystdex::unique_guard(f)))
125 catch(...)
126 {
127 f();
128 throw;
129 }
130 }
131
137 template<typename _tParam>
138 locked_ptr<ystdex::decay_t<_tParam>, recursive_mutex>
139 LockAddExit(_tParam&& arg)
140 {
141 unique_lock<recursive_mutex> lck(on_exit_mutex);
142
143 PushExit(yforward(arg));
144 return {YSLib::unchecked_any_cast<ystdex::decay_t<_tParam>>(
145 &on_exit.top()), std::move(lck)};
146 }
148
155 void
156 OnGotMessage(const Message&) override;
157
158 private:
160 template<typename _tParam>
161 inline void
162 PushExit(_tParam&& arg)
163 {
164 on_exit.push(yforward(arg));
165 }
166
167 public:
173 bool
174 Switch(shared_ptr<Shell>&) ynothrow;
181 PDefH(bool, Switch, shared_ptr<Shell>&& h) ynothrow
182 ImplRet(Switch(h))
183 };
184
185
193 extern YF_API Application&
194 FetchAppInstance();
195
201 inline PDefH(shared_ptr<Shell>, FetchShellHandle, ) ynothrow
202 ImplRet(FetchAppInstance().GetShellHandle())
203
210 inline PDefH(bool, Activate, const shared_ptr<Shell>& hShl)
211 ImplRet(FetchAppInstance().Switch(shared_ptr<Shell>(hShl)))
212
213
218 yconstexpr const Messaging::Priority DefaultQuitPriority(0xF0);
219
227 YF_API void
228 PostMessage(const Message&, Messaging::Priority);
229 inline PDefH(void, PostMessage, Messaging::ID id, Messaging::Priority prior,
230 const ValueObject& vo = {})
231 ImplRet(PostMessage(Message(id, vo), prior))
232 inline PDefH(void, PostMessage, Messaging::ID id, Messaging::Priority prior,
233 ValueObject&& c)
234 ImplRet(PostMessage(Message(id, std::move(c)), prior))
235 template<Messaging::MessageID _vID>
236 inline void
237 PostMessage(Messaging::Priority prior,
238 const typename Messaging::SMessageMap<_vID>::TargetType& target)
239 {
240 PostMessage(_vID, prior, ValueObject(target));
241 }
243
248 YF_API void
249 PostQuitMessage(int = 0, Messaging::Priority p = DefaultQuitPriority);
250
251 } // namespace YSLib;
252
253 #endif
254
YSLib::Messaging::ID
yimpl(std::uint32_t) ID
消息标识。
Definition: YMessage.h:46
YSLib::Application::AddExitGuard
void AddExitGuard(_func f)
Definition: YApplication.h:118
CHRLib::c
c
Definition: CharacterProcessing.h:84
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:1072
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
YSLib::Application::AddExit
void AddExit(_tParam &&arg)
Definition: YApplication.h:109
YSLib::FetchAppInstance
YF_API Application & FetchAppInstance()
取应用程序实例。
Definition: GUIApplication.cpp:295
YF_API
#define YF_API
Definition: Platform.h:56
CHRLib::f
f(d, s)
YSLib::Messaging::Priority
std::uint8_t Priority
消息优先级。
Definition: YMessage.h:52
YSLib::locked_ptr
unique_ptr< _type, unlock_delete< _tMutex, _tLock > > locked_ptr
独占所有权的锁定指针：使用线程模型对应的互斥量和锁以及 unique_ptr 。
Definition: YShellDefinition.h:108
YSLib::Application::DefGetter
DefGetter(const ynothrow, shared_ptr< Shell >, ShellHandle, hShell) template< typename _func > auto AccessQueue(_func f) -> decltype(f(qMain))
取得线程空间中当前运行的 Shell 的句柄。
Definition: YApplication.h:85
YSLib::Application
程序实例。
Definition: YApplication.h:47
platform::containers::stack
std::stack< _type, _tSeqCon > stack
Definition: Container.h:153
YSLib::Messaging::SMessageMap
Definition: YMessageDefinition.h:72
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
YSLib::ValueObject
值类型对象类。
Definition: YObject.h:1068
ystdex::threading::lock_guard
Definition: pseudo_mutex.h:223
scope_guard.hpp
作用域守卫。
YSLib::Application::PushExit
void PushExit(_tParam &&arg)
Definition: YApplication.h:162
YSLib::Messaging::MessageQueue
消息队列。
Definition: YMessage.h:159
ystdex::single_thread::recursive_mutex
Definition: pseudo_mutex.h:773
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
YSLib 命名空间。
Definition: AndroidHost.h:46
YSLib::Activate
Activate
Definition: YApplication.h:210
YModules.h
YSLib 用户界面模块配置文件。
ystdex::unique_guard
guard< one_shot< decay_t< _func >, void, decay_t< _tState > >, _bNoThrow > unique_guard(_func &&f, _tState &&s=true)
Definition: scope_guard.hpp:95
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
YSLib::Application::LockAddExit
locked_ptr< ystdex::decay_t< _tParam >, recursive_mutex > LockAddExit(_tParam &&arg)
锁定添加的初始化守卫。
Definition: YApplication.h:139
platform::p
return p p
Definition: YCLib/FileSystem.h:209
platform::ID
std::uintmax_t ID
YF_Platform_* 宏替换值的公共类型。
Definition: YCommon.h:54
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
YSLib::vo
vo
Definition: YApplication.h:231
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
ystdex::threading::unique_lock
Definition: pseudo_mutex.h:566
YSLib::PDefH
PDefH(Environment &, FetchEnvironment,) ImplRet(FetchGlobalInstance().GetEnvironmentRef()) inline PDefH(GUIHost &
取单一环境对象引用。
NPL::_tParam
_tParam
Definition: NPLA.h:2367
YSLib::Messaging::Message
消息。
Definition: YMessage.h:67