YSTest: Host.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
Host.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2013.
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 Inc_Helper_Host_h_
29 #define Inc_Helper_Host_h_ 1
30
31 #include "Helper/HostWindow.h" // for Devices::DSScreen, Host::Window;
32 #if YCL_MULTITHREAD == 1
33 # include <thread>
34 # include <mutex>
35 # include <atomic>
36 #endif
37 #include "YSLib/UI/yrender.h"
38 #include "YSLib/UI/ywidget.h" // for UI::GetSizeOf;
39 #include "ScreenBuffer.h" // for ScreenRegionBuffer;
40
41 YSL_BEGIN
42
43 #if YCL_HOSTED
44 YSL_BEGIN_NAMESPACE(Host)
45
46
47 yconstexpr wchar_t WindowClassName[]{L"YFramework Window"};
48
49
54 class RenderWindow : public Window
55 {
56 private:
57 std::reference_wrapper<HostRenderer> renderer;
58
59 public:
60 RenderWindow(NativeWindowHandle h, HostRenderer& r)
61 : Window(h), renderer(r)
62 {}
63
64 DefGetter(const ynothrow, HostRenderer&, Renderer, renderer)
65
66
67 void
68 OnPaint() override;
69 };
70
71
76 class WindowThread
77 {
78 private:
79 unique_ptr<Window> p_wnd;
80 std::thread thrd;
81
82 public:
83 template<typename... _tParams>
84 WindowThread(_tParams&&... args)
85 : p_wnd(), thrd(std::mem_fn(&WindowThread::ThreadFunc<typename
86 ystdex::qualified_decay<_tParams>::type...>), this,
87 ystdex::decay_forward(yforward(args))...)
88 {}
90 DefDelMoveCtor(WindowThread)
91 ~WindowThread();
92
93 DefGetter(const ynothrow, Window*, WindowPtr, p_wnd.get())
94
95 private:
96 template<typename _fCallable, typename... _tParams>
97 void
98 ThreadFunc(_fCallable&& f, _tParams&&... args)
99 {
100 ThreadLoop(yforward(f)(yforward(args)...));
101 }
102
104 void
105 ThreadLoop(NativeWindowHandle);
106 void
107 ThreadLoop(unique_ptr<Window>);
108
109 public:
110 static void
111 WindowLoop(Window&);
112 };
113
114
119 class HostRenderer : public UI::BufferedRenderer
120 {
121 private:
122 std::reference_wrapper<UI::IWidget> widget;
124 ScreenRegionBuffer rbuf;
125 WindowThread thrd;
126
127 public:
129 template<typename... _tParams>
130 HostRenderer(UI::IWidget& wgt, _tParams&&... args)
131 : BufferedRenderer(),
132 widget(wgt), rbuf(GetSizeOf(wgt)),
133 thrd(std::mem_fn(&HostRenderer::MakeRenderWindow<typename
134 ystdex::qualified_decay<_tParams>::type...>), this,
135 ystdex::decay_forward(yforward(args))...)
136 {}
137 DefDeMoveCtor(HostRenderer)
138
139 DefGetter(const ynothrow, UI::IWidget&, WidgetRef, widget.get())
140 DefGetterMem(const ynothrow, Window*, WindowPtr, thrd)
141
142 void
143 SetSize(const Drawing::Size&) override;
144
145 YB_NORETURN HostRenderer*
146 Clone() const override
147 {
148 throw LoggedEvent("HostRenderer::Clone: Not implemented.");
149 }
150 // DefClone(const override, HostRenderer, Clone)
151
153 template<typename _fCallable, typename... _tParams>
154 unique_ptr<Window>
155 MakeRenderWindow(_fCallable&& f, _tParams&&... args)
156 {
157 return unique_ptr<Window>(new
158 RenderWindow(yforward(f)(yforward(args)...), *this));
159 }
160
161 void
162 Update(Drawing::BitmapPtr);
163
165 template<typename _type>
166 void
167 UpdateToSurface(_type& sf)
168 {
169 sf.Update(rbuf);
170 }
171 };
172
173
178 class Environment
179 {
180 private:
188 map<NativeWindowHandle, Window*> wnd_map;
193 mutable std::mutex wmap_mtx;
194 # if YCL_MULTITHREAD == 1
195 # if YCL_MINGW32
196
197 ::HINSTANCE h_instance;
198 # endif
199
204 std::atomic<size_t> wnd_thrd_count;
205
206 public:
212 std::atomic<bool> ExitOnAllWindowThreadCompleted;
213 # endif
214
215 public:
216 Environment();
217 ~Environment();
218
224 Window*
225 GetForegroundWindow() const ynothrow;
226
232 void
233 AddMappedItem(NativeWindowHandle, Window*);
234
235 # if YCL_MULTITHREAD == 1
236
241 void
242 EnterWindowThread()
243 {
244 ++wnd_thrd_count;
245 }
246 # endif
247
253 Window*
254 FindWindow(NativeWindowHandle) const ynothrow;
255
260 static void
261 HostLoop();
262
263 # if YCL_MULTITHREAD == 1
264
270 void
271 LeaveWindowThread();
272 # endif
273
279 void
280 RemoveMappedItem(NativeWindowHandle) ynothrow;
281
283 void
284 UpdateRenderWindows();
285 };
286
287 YSL_END_NAMESPACE(Host)
288 #endif
289
290 YSL_END
291
292 #endif
293