diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-03 21:57:27 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-03 21:57:27 +0200 |
commit | 04daecdd21e86e7556a9462380096530228cd6ca (patch) | |
tree | ed2a8134532607687c165c4a13d6f6c020c42d18 | |
parent | 32bb4696396e48521614d00e5b8f6e6586822f31 (diff) | |
download | EpisodeBrowser-04daecdd21e86e7556a9462380096530228cd6ca.tar.gz |
Split common.h to util.h, wcharptr.h and win.h.
-rw-r--r-- | c/datalistview.cpp | 1 | ||||
-rw-r--r-- | c/debug.cpp | 3 | ||||
-rw-r--r-- | c/episodelistview.cpp | 3 | ||||
-rw-r--r-- | c/layout.cpp | 2 | ||||
-rw-r--r-- | c/layout.h | 2 | ||||
-rw-r--r-- | c/listview.cpp | 2 | ||||
-rw-r--r-- | c/main.cpp | 2 | ||||
-rw-r--r-- | c/pl.h | 2 | ||||
-rw-r--r-- | c/util.h | 19 | ||||
-rw-r--r-- | c/wcharptr.cpp | 64 | ||||
-rw-r--r-- | c/wcharptr.h | 33 | ||||
-rw-r--r-- | c/win.cpp (renamed from c/common.cpp) | 60 | ||||
-rw-r--r-- | c/win.h (renamed from c/common.h) | 45 |
13 files changed, 128 insertions, 110 deletions
diff --git a/c/datalistview.cpp b/c/datalistview.cpp index e6cc6ca..e44592b 100644 --- a/c/datalistview.cpp +++ b/c/datalistview.cpp @@ -5,7 +5,6 @@ #include <SWI-Prolog.h> #include "resource.h" -#include "common.h" #include "datalistview.h" #include "episodelistview.h" #include "listview.h" diff --git a/c/debug.cpp b/c/debug.cpp index 07006a9..934533b 100644 --- a/c/debug.cpp +++ b/c/debug.cpp @@ -1,8 +1,9 @@ #include <cstddef> +#include <cstdio> #include <windows.h> -#include "common.h" #include "debug.h" +#include "win.h" struct Avg { int id; diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 313f82f..b3a581d 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -4,11 +4,12 @@ #include <SWI-Prolog.h> #include "resource.h" -#include "common.h" #include "datalistview.h" #include "episodelistview.h" #include "listview.h" #include "pl.h" +#include "util.h" +#include "win.h" EpisodeListView::EpisodeListView(const HWND hWndParent) : ListView(hWndParent, (HMENU)IDC_EPISODELISTVIEW, 0) diff --git a/c/layout.cpp b/c/layout.cpp index 94978bb..cbfb831 100644 --- a/c/layout.cpp +++ b/c/layout.cpp @@ -1,9 +1,9 @@ #include <windows.h> -#include "common.h" #include "episodelistview.h" #include "datalistview.h" #include "layout.h" +#include "win.h" extern HWND g_hWnd; extern HWND g_hWndStatus; @@ -3,9 +3,9 @@ #include <windows.h> -#include "common.h" #include "datalistview.h" #include "pl.h" +#include "win.h" void UpdateLayout(int w = 0, int h = 0); diff --git a/c/listview.cpp b/c/listview.cpp index 531e9dd..107d34b 100644 --- a/c/listview.cpp +++ b/c/listview.cpp @@ -1,9 +1,9 @@ #include <windows.h> #include <commctrl.h> -#include "common.h" #include "listview.h" #include "layout.h" +#include "win.h" extern HFONT g_hfNormal; static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); @@ -6,11 +6,11 @@ #include "debug.h" #include "resource.h" -#include "common.h" #include "datalistview.h" #include "episodelistview.h" #include "layout.h" #include "pl.h" +#include "util.h" /* Looked-up constants. */ int g_dpi = 96; @@ -5,7 +5,7 @@ #include <windows.h> #include <SWI-Prolog.h> -#include "common.h" +#include "wcharptr.h" wchar_ptr PlString(const term_t t, const int flags = CVT_WRITE); diff --git a/c/util.h b/c/util.h new file mode 100644 index 0000000..babe0c5 --- /dev/null +++ b/c/util.h @@ -0,0 +1,19 @@ +#ifndef UTIL_H +#define UTIL_H + +#include <memory> + +template<size_t N, typename... T> +inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) +{ + return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...); +} + +inline int Cmp(const int a, const int b) +{ + if (a == b) return 0; + if (a > b) return 1; + return -1; +} + +#endif diff --git a/c/wcharptr.cpp b/c/wcharptr.cpp new file mode 100644 index 0000000..e406ed1 --- /dev/null +++ b/c/wcharptr.cpp @@ -0,0 +1,64 @@ +#include <utility> +#include <windows.h> + +#include "wcharptr.h" +#include "win.h" + +wchar_ptr::wchar_ptr() noexcept {} + +wchar_ptr::wchar_ptr(wchar_t* const s) noexcept : m_p(s) {} + +wchar_ptr& wchar_ptr::operator=(wchar_t* const s) noexcept +{ + if (m_p != s) { + delete m_p; + m_p = s; + } + return *this; +} + +wchar_ptr::wchar_ptr(wchar_ptr&& other) noexcept + : m_p(std::exchange(other.m_p, nullptr)) {} + +wchar_ptr& wchar_ptr::operator=(wchar_ptr&& other) noexcept +{ + std::swap(m_p, other.m_p); + return *this; +} + +wchar_ptr::operator wchar_t*() noexcept +{ + return m_p; +} + +wchar_t* wchar_ptr::release() noexcept +{ + wchar_t* p2 = m_p; + m_p = nullptr; + return p2; +} + +wchar_ptr::~wchar_ptr() noexcept +{ + delete m_p; +} + +wchar_ptr wchar_ptr::from_narrow(const char* const src, const int cp) +{ + int cbMultiByte = strlen(src)+1; + int cchWideChar = MultiByteToWideChar(cp, 0, src, cbMultiByte, NULL, 0); + wchar_t* dst = new wchar_t[cchWideChar]; + if (!MultiByteToWideChar(cp, 0, src, cbMultiByte, dst, cchWideChar)) { + delete dst; + throw Win32Error(); + } + return dst; +} + +wchar_ptr wchar_ptr::copy(const wchar_t* const src) +{ + const int cb = wcslen(src)+1; + wchar_t* dst = new wchar_t[cb]; + memcpy(dst, src, cb*sizeof(wchar_t)); + return dst; +} diff --git a/c/wcharptr.h b/c/wcharptr.h new file mode 100644 index 0000000..1f579b5 --- /dev/null +++ b/c/wcharptr.h @@ -0,0 +1,33 @@ +#ifndef WCHARPTR_H +#define WCHARPTR_H + +#include <windows.h> + +/* wchar_ptr: Simple wrapper for wide C strings. */ +struct wchar_ptr +{ + /* Named copy constructors (expensive). */ + static wchar_ptr from_narrow(const char* buf, int cp = CP_UTF8); + static wchar_ptr copy(const wchar_t* s); + + /* Non-explicit copies are disabled. */ + wchar_ptr(wchar_ptr& other) = delete; + wchar_ptr& operator=(wchar_ptr& other) = delete; + + wchar_ptr() noexcept; + ~wchar_ptr(); + operator wchar_t*() noexcept; + + wchar_ptr(wchar_t* s) noexcept; + wchar_ptr& operator=(wchar_t* s) noexcept; + + wchar_ptr(wchar_ptr&& other) noexcept; + wchar_ptr& operator=(wchar_ptr&& other) noexcept; + + /* Return pointer, releasing ownership. */ + wchar_t *release() noexcept; +private: + wchar_t* m_p = nullptr; +}; + +#endif @@ -1,65 +1,7 @@ #include <utility> #include <windows.h> -#include "common.h" - -wchar_ptr::wchar_ptr() noexcept {} - -wchar_ptr::wchar_ptr(wchar_t* const s) noexcept : m_p(s) {} - -wchar_ptr& wchar_ptr::operator=(wchar_t* const s) noexcept -{ - if (m_p != s) { - delete m_p; - m_p = s; - } - return *this; -} - -wchar_ptr::wchar_ptr(wchar_ptr&& other) noexcept : m_p(std::exchange(other.m_p, nullptr)) {} - -wchar_ptr& wchar_ptr::operator=(wchar_ptr&& other) noexcept -{ - std::swap(m_p, other.m_p); - return *this; -} - -wchar_ptr::operator wchar_t*() noexcept -{ - return m_p; -} - -wchar_t* wchar_ptr::release() noexcept -{ - wchar_t* p2 = m_p; - m_p = nullptr; - return p2; -} - -wchar_ptr::~wchar_ptr() noexcept -{ - delete m_p; -} - -wchar_ptr wchar_ptr::from_narrow(const char* const src, const int cp) -{ - int cbMultiByte = strlen(src)+1; - int cchWideChar = MultiByteToWideChar(cp, 0, src, cbMultiByte, NULL, 0); - wchar_t* dst = new wchar_t[cchWideChar]; - if (!MultiByteToWideChar(cp, 0, src, cbMultiByte, dst, cchWideChar)) { - delete dst; - throw Win32Error(); - } - return dst; -} - -wchar_ptr wchar_ptr::copy(const wchar_t* const src) -{ - const int cb = wcslen(src)+1; - wchar_t* dst = new wchar_t[cb]; - memcpy(dst, src, cb*sizeof(wchar_t)); - return dst; -} +#include "win.h" Win32Error::Win32Error() : code(GetLastError()) {} Win32Error::Win32Error(const DWORD code) : code(code) {} @@ -1,40 +1,12 @@ -#ifndef COMMON_H -#define COMMON_H +#ifndef WIN_H +#define WIN_H -#include <memory> #include <optional> #include <windows.h> int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); int GetRelativeCursorPos(HWND hWnd, POINT* pt); -/* wchar_ptr: Simple wrapper for wide C strings. */ -struct wchar_ptr -{ - /* Named copy constructors (expensive). */ - static wchar_ptr from_narrow(const char* buf, int cp = CP_UTF8); - static wchar_ptr copy(const wchar_t* s); - - /* Non-explicit copies are disabled. */ - wchar_ptr(wchar_ptr& other) = delete; - wchar_ptr& operator=(wchar_ptr& other) = delete; - - wchar_ptr() noexcept; - ~wchar_ptr(); - operator wchar_t*() noexcept; - - wchar_ptr(wchar_t* s) noexcept; - wchar_ptr& operator=(wchar_t* s) noexcept; - - wchar_ptr(wchar_ptr&& other) noexcept; - wchar_ptr& operator=(wchar_ptr&& other) noexcept; - - /* Return pointer, releasing ownership. */ - wchar_t *release() noexcept; -private: - wchar_t* m_p = nullptr; -}; - /* Win32Error: Exception for Windows API errors. */ struct Win32Error : public std::exception { @@ -67,12 +39,6 @@ T* Library::GetProcAddress(const char* const szProc) return (T*)(void*)::GetProcAddress(m_hModule, szProc); } -template<size_t N, typename... T> -inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) -{ - return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...); -} - /* Variable template for caching values from GetSystemMetrics. */ template <int I> auto Metric = GetSystemMetrics(I); @@ -103,13 +69,6 @@ inline int Dpi(const int i) return MulDiv(i, g_dpi, 96); } -inline int Cmp(const int a, const int b) -{ - if (a == b) return 0; - if (a > b) return 1; - return -1; -} - /* Get window rectangle relative to parent. */ inline BOOL GetRelativeRect(const HWND hWnd, RECT* const rr) { |