YSTest: Consoles.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

Consoles.cpp
浏览该文件的文档.
1 /*
2 © 2013-2015 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 #include "YCLib/YModules.h"
30 #include YFM_YCLib_Platform
31 #if YCL_Win32
32 # include YFM_Win32_YCLib_Consoles // for UTF8ToWCS;
33 # include YFM_YSLib_Core_YCoreUtilities // for YSLib::CheckPositiveScalar;
34
35 using namespace YSLib;
36 #endif
37
38 namespace platform_ex
39 {
40
41 #if YCL_Win32
42
43 inline namespace Windows
44 {
45
46 WConsole::WConsole(unsigned long dev)
47 : WConsole(::GetStdHandle(dev))
48 {}
49 WConsole::WConsole(::HANDLE h)
50 : h_std(h), saved_attr(GetScreenBufferInfo().wAttributes),
51 Attributes(saved_attr)
52 {
53 YAssert(h_std && h_std != INVALID_HANDLE_VALUE, "Invalid handle found.");
54 }
55 WConsole::~WConsole()
56 {
57 FilterExceptions(std::bind(&WConsole::RestoreAttributes, this), yfsig);
58 }
59
60 ::CONSOLE_SCREEN_BUFFER_INFO
61 WConsole::GetScreenBufferInfo() const
62 {
63 ::CONSOLE_SCREEN_BUFFER_INFO info;
64
65 YCL_CallWin32F(GetConsoleScreenBufferInfo, h_std, &info);
66 return info;
67 }
68
69 void
70 WConsole::SetSystemColor(std::uint8_t color)
71 {
72 char cmd[9];
73
74 std::sprintf(cmd, "COLOR %02x", int(color));
75 std::system(cmd);
76 }
77
78 void
79 WConsole::SetBackColor(std::uint8_t bc) ynothrow
80 {
81 Attributes = ComposeAttributes(FetchForeColor(Attributes), bc);
82 }
83 void
84 WConsole::SetCursorPosition(::COORD pos)
85 {
86 // NOTE: %::SetConsoleCursorPosition expects 1-based.
87 YCL_CallWin32F(SetConsoleCursorPosition, h_std, {short(pos.X + 1), short(pos.Y + 1)});
88 }
89 void
90 WConsole::SetForeColor(std::uint8_t fc) ynothrow
91 {
92 Attributes = ComposeAttributes(fc, FetchBackColor(Attributes));
93 }
94
95 ::WORD
96 WConsole::ComposeAttributes(std::uint8_t fore, std::uint8_t back) ynothrow
97 {
98 return (fore & 15) | ((back & 15) << 4);
99 }
100
101 void
102 WConsole::CursorUp(size_t num_rows)
103 {
104 if(num_rows != 0)
105 {
106 const auto pos(GetCursorPosition());
107
108 // XXX: Conversion to 'short' might be implementation-defined.
109 SetCursorPosition({short(pos.Y - short(num_rows)), pos.X});
110 }
111 }
112
113 void
114 WConsole::Erase(wchar_t c)
115 {
116 const auto size(GetScreenBufferInfo().dwSize);
117
118 Fill({short(), short()}, CheckPositiveScalar<unsigned long>(size.X)
119 * CheckPositiveScalar<unsigned long>(size.Y), c);
120 }
121
122 void
123 WConsole::Fill(::COORD coord, unsigned long n, wchar_t c)
124 {
125 YCL_CallWin32F(FillConsoleOutputCharacterW, h_std, c, n, coord, {});
126 YCL_CallWin32F(FillConsoleOutputAttribute, h_std, Attributes, n, coord, {});
127 YCL_CallWin32F(SetConsoleCursorPosition, h_std, {coord.X, coord.Y});
128 }
129
130 void
131 WConsole::RestoreAttributes()
132 {
133 // SetColor();
134 Update(GetSavedAttributes());
135 }
136
137 void
138 WConsole::Update()
139 {
140 Update(Attributes);
141 }
142 void
143 WConsole::Update(::WORD value)
144 {
145 YCL_CallWin32F(SetConsoleTextAttribute, h_std, value);
146 }
147
148 void
149 WConsole::UpdateBackColor(std::uint8_t fc)
150 {
151 SetBackColor(fc);
152 Update();
153 }
154
155 void
156 WConsole::UpdateForeColor(std::uint8_t fc)
157 {
158 SetForeColor(fc);
159 Update();
160 }
161
162 size_t
163 WConsole::WriteString(string_view sv)
164 {
165 YAssertNonnull(sv.data());
166 // FIXME: Support for non-BMP characters.
167 return WriteString(UTF8ToWCS(sv));
168 }
169 size_t
170 WConsole::WriteString(string_view sv, unsigned cp)
171 {
172 YAssertNonnull(sv.data());
173 return WriteString(MBCSToWCS(sv, cp));
174 }
175 size_t
176 WConsole::WriteString(wstring_view sv)
177 {
178 YAssertNonnull(sv.data());
179
180 unsigned long n;
181
182 YCL_CallWin32F(WriteConsoleW, h_std, sv.data(),
183 static_cast<unsigned long>(sv.length()), &n, yimpl({}));
184 return size_t(n);
185 }
186
187 unique_ptr<WConsole>
188 MakeWConsole(unsigned long h)
189 {
190 unique_ptr<WConsole> p_con;
191
192 TryExpr(p_con.reset(new WConsole(h)))
193 CatchIgnore(Win32Exception&)
194 return p_con;
195 }
196
197 } // inline namespace Windows;
198
199 #endif
200
201 } // namespace platform_ex;
202
YSLib::UI::fc
fc
Definition: Console.h:93
YSLib::Drawing::Fill
YF_API void Fill(const Graphics &, Color)
使用指定颜色填充图形接口上下文缓冲区。
Definition: YBlit.cpp:94
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:202
ystdex::wstring_view
basic_string_view< wchar_t > wstring_view
Definition: string_view.hpp:418
CHRLib::c
c
Definition: CharacterProcessing.h:81
yfsig
#define yfsig
展开为表示函数签名的预定义变量的宏。
Definition: ydef.h:702
YCL_CallWin32F
#define YCL_CallWin32F(_fn,...)
Definition: MinGW32.h:179
YSLib::size_t
name const ValueNode::Container const string &name ynothrow observer_ptr< ValueNode::Container > const string &name ynothrow observer_ptr< const ValueNode::Container > const string &name ynothrow size_t
Definition: ValueNode.h:639
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
platform_ex::Windows::WConsole::WConsole
WConsole(unsigned long h=STD_OUTPUT_HANDLE)
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:114
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
platform_ex
平台扩展命名空间。
Definition: Android.h:45
YModules.h
platform_ex::Windows::MBCSToWCS
YF_API wstring MBCSToWCS(string_view, unsigned=CP_ACP)
YSLib::p_con
name const ValueNode::Container * p_con
Definition: ValueNode.h:627
platform_ex::Windows::UTF8ToWCS
UTF8ToWCS
Definition: MinGW32.h:650
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321