YSTest: AndroidHost.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

AndroidHost.h
浏览该文件的文档.
1 /*
2 © 2014-2016 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
29 #ifndef INC_Android_Helper_AndroidHost_h_
30 #define INC_Android_Helper_AndroidHost_h_ 1
31
32 #include "YModules.h"
33 #include YFM_Helper_Environment // for unique_ptr, std::atomic,
34 // Devices::AndroidScreen, ystdex::aligned_store_cast;
35 #if YCL_Android
36 # include <thread>
37 # include <android/native_activity.h>
38 # include <android/configuration.h>
39 # include YFM_YCLib_JNI
40 # include YFM_YCLib_Host
41 #else
42 //# error "Currently only Android is supported."
43 #endif
44
45 namespace YSLib
46 {
47
48 #if YCL_Android
49 namespace Android
50 {
51
53 namespace JNI = platform_ex::JNI;
55 using platform_ex::UniqueHandle;
56
61 struct YF_API ConfigurationDeleter
62 {
64 using pointer = ::AConfiguration*;
65
67 PDefHOp(void, (), pointer p) const ynothrow
68 ImplRet(::AConfiguration_delete(p))
69 };
70
71
77 class YF_API NativeHost
78 {
79 protected:
86 std::thread thrdMain{};
88 vector<byte> vecSavedState{};
89 std::function<void(void*&, size_t&)> fSaveState{};
90
91 public:
97 int MainReturnValue = EXIT_SUCCESS;
98
99 private:
101 lref<::ANativeActivity> activity;
102 unique_ptr<::AConfiguration, ConfigurationDeleter> p_config;
104 lref<::ALooper> looper;
105 unique_ptr<InputQueue> p_input_queue{};
107
108 mutex msg_mutex{};
110 pair<UniqueHandle, UniqueHandle> msg_pipe;
111 std::function<void()> msg_task{};
113
117 mutable mutex state_mutex{};
122 std::atomic<bool> paused{{}};
124 unique_ptr<Devices::AndroidScreen> p_screen;
125
126 public:
127 NativeHost(::ANativeActivity&, void*, size_t);
128 ~NativeHost();
129
131 DefPred(const ynothrow, Paused, paused)
133 DefGetter(const ynothrow, ::ANativeActivity&, ActivityRef, activity.get())
135 Devices::Screen&
136 GetHostScreenRef() const ynothrow;
137 DefGetter(const ynothrow, Devices::AndroidScreen&, ScreenRef,
138 Deref(p_screen))
139
140
144 template<typename _type>
145 void
146 BindSavedState(const _type& state)
147 {
148 // TODO: Wait for 'std::is_trivially_copyable' to be implemented.
149 // static_assert(std::is_trivially_copyable<ystdex::remove_reference_t<
150 // _type>>(), "Invalid state type found.");
151 lock_guard<mutex> lck(state_mutex);
152
153 fSaveState = [&](void*& p_saved_state, size_t& saved_size){
154 p_saved_state = std::malloc(sizeof(_type));
155 if(p_saved_state)
156 // TODO: Use inherited class of exception.
157 throw std::bad_alloc();
158 new(p_saved_state) _type(state);
159 saved_size = sizeof(_type);
160 };
161 }
162
163 void
164 ClearSavedStateHandler();
165
166 void
167 LoadConfiguration();
168
169 void
170 ReleaseSavedState() ynothrow;
171
173 void
174 ResizeScreen(const Drawing::Size&);
175
176 template<typename _type>
177 void
178 Restore(_type& state) const
179 {
180 // TODO: Wait for 'std::is_trivially_copyable' to be implemented.
181 // static_assert(std::is_trivially_copyable<ystdex::remove_reference_t<
182 // _type>>(), "Invalid state type found.");
183
184 RestoreSavedState(ystdex::aligned_store_cast<byte*>(&state));
185 }
186
192 void
193 RestoreSavedState(byte* p_byte) const;
194
196 void
197 RunOnUIThread(std::function<void()>);
198
199 void*
200 SaveInstanceState(size_t* p_len);
201 };
202
204 YF_API ::ANativeWindow&
205 FetchDefaultWindow() ynothrow;
206
207 YF_API NativeHost&
208 FetchNativeHostInstance() ynothrow;
209
210 } // namespace Android;
211 #endif
212
213 } // namespace YSLib;
214 #endif
215
platform::p
while p p
Definition: YCLib/FileSystem.h:225
ystdex::lref::get
_type & get() const
Definition: ref.hpp:107
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::Devices::Screen
屏幕。
Definition: YDevice.h:136
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:440
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:293
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
ystdex::single_thread::lock_guard
Definition: pseudo_mutex.h:197
ystdex::single_thread::mutex
Definition: pseudo_mutex.h:63
YSLib::LoadConfiguration
YF_API ValueNode LoadConfiguration(bool={})
载入默认配置。
Definition: Initialization.cpp:411
YF_API
#define YF_API
Definition: Platform.h:56
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:231
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。