From 85a6d12a647a63e578260e942dec844d2f5ab66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 15 Jul 2022 12:03:15 +0200 Subject: Use std::optional instead of std::unique_ptr for try_make. This requires C++17. --- Makefile | 2 +- c/defs.h | 9 ++++----- c/main.cpp | 8 ++++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 202fd6e..1ab1213 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PL = pl/cfg.pl pl/episode_data.pl pl/local_episodes.pl pl/track_episodes.pl OBJ = $(B)common.obj $(B)datalistview.obj $(B)episodelistview.obj $(B)listview.obj $(B)pl.obj $(B)resource.obj CC = swipl-ld -CFLAGS += -Wall -Wpedantic -O -ld-options,-mwindows +CFLAGS += -Wall -Wpedantic -O -cc-options,-std=c++17 -ld-options,-mwindows CFLAGS += -DUNICODE -D_UNICODE LDFLAGS += -lcomctl32 -luxtheme diff --git a/c/defs.h b/c/defs.h index ffebcb0..c8d76b3 100644 --- a/c/defs.h +++ b/c/defs.h @@ -2,6 +2,7 @@ #define DEFS_H #include +#include #include #include #include @@ -144,15 +145,13 @@ inline int Dpi(int i) } template -std::unique_ptr try_make_unique(A ...args) +std::optional try_make(A ...args) { - std::unique_ptr up; try { - up = std::make_unique(args...); + return T(args...); } catch (...) { - up = nullptr; + return {}; } - return up; } #endif diff --git a/c/main.cpp b/c/main.cpp index 8bf07fb..f0adee9 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -35,14 +35,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR tszErr; /* Set constant values. */ - if (auto upLib = try_make_unique(TEXT("uxtheme.dll"))) + if (auto upLib = try_make(TEXT("uxtheme.dll"))) if (upLib->GetProcAddress("SetWindowTheme")) { g_bThemes = 1; } g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL); /* Setup fonts. */ - if (auto upLib = try_make_unique(TEXT("User32.dll"))) { + if (auto upLib = try_make(TEXT("User32.dll"))) { if (upLib->GetProcAddress("SystemParametersInfo" WA)) { NONCLIENTMETRICS m; m.cbSize = sizeof(NONCLIENTMETRICS); @@ -140,8 +140,8 @@ static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) g_hWnd = (HWND)wParam; /* Get DPI. */ - if (auto upLib = try_make_unique(TEXT("User32.dll"))) - if (auto GetDpiForWindow = (UINT (*)(HWND))upLib->GetProcAddress("GetDpiForWindow")) + if (auto opLib = try_make(TEXT("User32.dll"))) + if (auto GetDpiForWindow = (UINT (*)(HWND))opLib->GetProcAddress("GetDpiForWindow")) g_iDPI = GetDpiForWindow(g_hWnd); /* Create child windows. */ -- cgit v1.2.3