Skip to content

Commit 3cb9852

Browse files
committed
C++14 upgrade to C++17: 部分字符串类型修改为wstring_view
1 parent 96ecd5f commit 3cb9852

11 files changed

Lines changed: 53 additions & 45 deletions

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(Timer VERSION 0.2)
33

4-
set(CMAKE_CXX_STANDARD 14)
5-
set(CMAKE_CXX_STANDARD_REQUIRED True)
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
file(GLOB SOURCES "*.cpp" "timer.rc")
88
add_executable(timer WIN32 ${SOURCES})
99
target_compile_definitions(timer PRIVATE _UNICODE UNICODE)
10+
target_compile_features(timer PRIVATE cxx_std_17)
1011

1112
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

date.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Date {
99
const auto now = std::chrono::system_clock::now();
1010
const auto now_t = std::chrono::system_clock::to_time_t(now);
1111

12-
struct tm tm;
12+
struct tm tm{};
1313
localtime_s(&tm, &now_t);
1414
return tm;
1515
}
@@ -26,18 +26,18 @@ namespace Date {
2626

2727

2828
std::chrono::hours CurrTimeHour() {
29-
auto tm = currTM();
29+
const auto tm = currTM();
3030

3131
return std::chrono::hours(tm.tm_hour);
3232
}
3333

3434
std::chrono::minutes CurrTimeMin() {
35-
auto tm = currTM();
35+
const auto tm = currTM();
3636
return std::chrono::minutes(tm.tm_min);
3737
}
3838

3939
std::chrono::seconds CurrTimeSec() {
40-
auto tm = currTM();
40+
const auto tm = currTM();
4141
return std::chrono::seconds(tm.tm_sec);
4242
}
4343

digital_font.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <windows.h>
22
#include <gdiplus.h>
3-
#include <cmath>
3+
#include <string_view>
44
#include <array>
55
#include "gdi_obj.h"
66
#include "digital_font.h"
@@ -136,27 +136,27 @@ namespace DigitalFont {
136136
}
137137

138138
// 绘制时钟
139-
void DrawClock(HDC hdc, std::wstring &time_str) {
139+
void DrawClock(HDC hdc, const std::wstring_view time_str) {
140140
if (time_str.empty()) return;
141141

142142
int start_x = PAINT_START_X;
143-
int start_y = PAINT_START_Y;
143+
constexpr int start_y = PAINT_START_Y;
144144

145-
for (wchar_t c: time_str) {
145+
for (const wchar_t c: time_str) {
146146
// 数字
147147
if (c >= L'0' && c <= L'9') {
148-
int digit = c - L'0';
148+
const int digit = c - L'0';
149149
DrawBkDigit(hdc, start_x, start_y, digit);
150150
// 每个数码管数字的长度是 SEG_LENGTH + 2*SEG_WIDTH
151151
start_x += SEG_LENGTH + 2 * SEG_WIDTH + DIGIT_SPACING;
152152
}
153153

154154
// 冒号
155155
if (c == L':') {
156-
int colon_y = start_y + (SEG_LENGTH * 2 + SEG_WIDTH * 3) / 2;
156+
constexpr int colon_y = start_y + (SEG_LENGTH * 2 + SEG_WIDTH * 3) / 2;
157157

158158
IGDI::AutoGDI<HBRUSH> brush(CreateSolidBrush(RGB(0, 0, 0)));
159-
HBRUSH old_brash = (HBRUSH) SelectObject(hdc, brush);
159+
const auto old_brash = static_cast<HBRUSH>(SelectObject(hdc, brush));
160160

161161
Rectangle(hdc,
162162
start_x, colon_y - COLON_SIZE - COLON_OFFSET_Y,

digital_font.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace DigitalFont {
1515
constexpr auto COLON_GAP = 20; // 冒号之间的间距
1616
constexpr auto COLON_OFFSET_Y = 8; // 冒号的偏移量
1717

18-
1918
void DrawDigit(HDC hdc, int x, int y, int digit);
2019

21-
void DrawClock(HDC hdc, std::wstring &time_str);
20+
void DrawClock(HDC hdc, std::wstring_view time_str);
2221
}

font.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Font {
99
namespace {
10-
HFONT createScaledFont(HDC hdc, _In_ const RECT &rect, _In_ const std::wstring &str) {
10+
HFONT createScaledFont(HDC hdc, _In_ const RECT &rect, _In_ const std::wstring_view str) {
1111
// 计算字体大小
1212
int font_size_height = rect.bottom - rect.top;
1313
int font_size_width = rect.right - rect.left;
@@ -33,7 +33,7 @@ namespace Font {
3333
}
3434
}
3535

36-
void DrawScaledText(HDC hdc, _In_ RECT &rect, _In_ const std::wstring &str) {
36+
void DrawScaledText(HDC hdc, _In_ RECT &rect, _In_ const std::wstring_view str) {
3737
if (str.empty()) return;
3838

3939
IGDI::AutoGDI<HFONT> h_font(createScaledFont(hdc, rect, str));
@@ -42,7 +42,7 @@ namespace Font {
4242

4343
DrawText(
4444
hdc,
45-
str.c_str(),
45+
str.data(),
4646
-1,
4747
&rect,
4848
DT_SINGLELINE

font.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
constexpr auto FONT_FAMILY = L"Arial";
77

88
namespace Font {
9-
void DrawScaledText(HDC hdc, _In_ RECT &rect, _In_ const std::wstring &str);
9+
void DrawScaledText(HDC hdc, _In_ RECT &rect, _In_ const std::wstring_view str);
1010
}

gdi_obj.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@
55
// 自动包装GDI对象,在对象生命周期结束时自动释放
66
namespace IGDI {
77
template<typename T>
8-
struct is_gdi_type : std::false_type {
8+
struct is_gdi_type {
9+
static constexpr bool value = false;
910
};
1011

1112
template<>
12-
struct is_gdi_type<HFONT> : std::true_type {
13+
struct is_gdi_type<HFONT> {
14+
static constexpr bool value = true;
1315
};
1416

1517
template<>
16-
struct is_gdi_type<HBRUSH> : std::true_type {
18+
struct is_gdi_type<HBRUSH> {
19+
static constexpr bool value = true;
1720
};
1821

1922
template<>
20-
struct is_gdi_type<HPEN> : std::true_type {
23+
struct is_gdi_type<HPEN> {
24+
static constexpr bool value = true;
2125
};
2226

2327
template<typename T>
2428
class AutoGDI {
25-
static_assert(is_gdi_type<T>::value, "T must be a GDI object");
29+
static_assert(is_gdi_type<T>::value == true, "T must be a GDI object");
30+
2631
T obj;
2732

2833
public:

main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
int WINAPI WinMain(
8-
_In_ HINSTANCE hInstance,
8+
_In_ const HINSTANCE hInstance,
99
_In_opt_ HINSTANCE hPrevInstance,
1010
_In_ LPSTR lpCmdLine,
1111
_In_ int nCmdShow
@@ -14,14 +14,14 @@ int WINAPI WinMain(
1414
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
1515
}
1616

17-
auto ret = CreateTimeClassAndWindow(hInstance, nCmdShow);
17+
const auto ret = CreateTimeClassAndWindow(hInstance, nCmdShow);
1818
if (ret > 0) {
1919
return ret;
2020
}
2121

2222
MSG msg;
2323

24-
while (GetMessage(&msg, NULL, 0, 0) > 0) {
24+
while (GetMessage(&msg, nullptr, 0, 0) > 0) {
2525
TranslateMessage(&msg);
2626
DispatchMessage(&msg);
2727
}

setting_window.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static LRESULT CALLBACK settingWndProc(
3838
}
3939

4040

41-
static void registerSettingClass(_In_ HINSTANCE hInstance) {
41+
static void registerSettingClass(_In_ const HINSTANCE hInstance) {
4242
WNDCLASSEX setting_wcex;
4343

4444
setting_wcex.cbSize = sizeof(WNDCLASSEX);
@@ -65,23 +65,24 @@ static void registerSettingClass(_In_ HINSTANCE hInstance) {
6565
/// <param name="hInstance">应用程序实例的句柄。</param>
6666
/// <param name="nCmdShow">窗口显示方式的标志。</param>
6767
/// <returns>如果成功则返回 0,失败则返回 1。</returns>
68-
int CreateSettingClassAndWindow(_In_ HINSTANCE hInstance, _In_ int nCmdShow) {
68+
int CreateSettingClassAndWindow(_In_ HINSTANCE hInstance, _In_ const int nCmdShow) {
6969
WNDCLASSEX setting_wcex = {sizeof(WNDCLASSEX)};
7070
if (FALSE == GetClassInfoEx(hInstance, TEST_CLASS_NAME, &setting_wcex)) {
7171
registerSettingClass(hInstance);
7272
}
7373

74-
HWND setting_hwnd = CreateWindowEx(
74+
75+
const HWND setting_hwnd = CreateWindowEx(
7576
0,
7677
TEST_CLASS_NAME,
7778
TEST_WINDOW_TITLE,
7879
WS_OVERLAPPEDWINDOW,
7980
CW_USEDEFAULT, CW_USEDEFAULT,
8081
TEST_WINDOW_WIDTH, TEST_WINDOW_HEIGHT,
81-
NULL,
82-
NULL,
82+
nullptr,
83+
nullptr,
8384
hInstance,
84-
NULL
85+
nullptr
8586
);
8687
Debug::TryLastErrorMessageBox();
8788

time_window.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ struct WindowDisplay {
2525
};
2626

2727

28-
static void Exit(HWND hwnd) {
28+
static void Exit(const HWND hwnd) {
2929
KillTimer(hwnd, DATE_TIMER_ID);
3030
KillTimer(hwnd, ANIMATION_TIMER_ID);
3131
PostQuitMessage(0);
3232
}
3333

34-
static void timerWindowFadeIn(HWND hwnd, WindowDisplay &wd) {
34+
static void timerWindowFadeIn(const HWND hwnd, WindowDisplay &wd) {
3535
wd.is_show = TRUE;
3636
wd.fading = TRUE;
37-
SetTimer(hwnd, ANIMATION_TIMER_ID, 10, NULL); // 启动新定时器
37+
SetTimer(hwnd, ANIMATION_TIMER_ID, 10, nullptr); // 启动新定时器
3838
}
3939

40-
static void timerWindowFadeOut(HWND hwnd, WindowDisplay &wd) {
40+
static void timerWindowFadeOut(const HWND hwnd, WindowDisplay &wd) {
4141
wd.is_show = FALSE;
4242
wd.fading = TRUE;
43-
SetTimer(hwnd, ANIMATION_TIMER_ID, 10, NULL); // 启动新定时器
43+
SetTimer(hwnd, ANIMATION_TIMER_ID, 10, nullptr); // 启动新定时器
4444
}
4545

46-
static void updateTimerWindowFadeAnimation(HWND hwnd, WindowDisplay &wd) {
46+
static void updateTimerWindowFadeAnimation(const HWND hwnd, WindowDisplay &wd) {
4747
// 提醒窗口开始动画
4848
if (wd.fading && wd.is_show) {
4949
wd.alpha += FADE_DURATION;
@@ -119,7 +119,7 @@ static LRESULT CALLBACK timeWndProc(
119119
g_step_down.reset(Date::NextHourDistance());
120120
}
121121

122-
InvalidateRect(hWnd, NULL, TRUE);
122+
InvalidateRect(hWnd, nullptr, TRUE);
123123
}
124124

125125
if (wParam == ANIMATION_TIMER_ID) {

0 commit comments

Comments
 (0)