aboutsummaryrefslogtreecommitdiff
path: root/c/common.h
blob: 4b42f45ee26e5cc3ac381108f44166a3d5076cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef COMMON_H
#define COMMON_H

#include <memory>
#include <optional>
#include <windows.h>

#ifdef UNICODE
#define WA "W"
#else
#define WA "A"
#endif

int EBMessageBox(const TCHAR* tszText, const TCHAR* tszCaption, unsigned uType);

struct Win32Error : public std::exception
{
	Win32Error();
	Win32Error(DWORD dwErr);
	~Win32Error();
	template <typename T = char> const T* what() const noexcept;

private:
	DWORD m_dwErr;
	char* m_szMsg = NULL;
	wchar_t* m_wszMsg = NULL;
};

struct Library
{
	Library(const TCHAR* tszLibrary);
	~Library();
	template <class T> T* GetProcAddress(const char* szProc);
private:
	HMODULE m_hModule;
};

template <typename T>
T* Library::GetProcAddress(const char* const szProc)
{
	return (T*)(void*)::GetProcAddress(m_hModule, szProc);
}

/* Create and return an object of type C. If construction fails,
 * return nothing. The returned value must be checked before being
 * used, as dereferencing is undefined if the value is empty. */
template <typename T, typename... U>
std::optional<T> maybe_make(U... xs)
{
	try {
		return T(xs...);
	} catch (...) {
		return {};
	}
}

/* Call Windows API function, throwing error on NULL. */
template <typename T>
inline T require(const T x)
{
	if (!x) throw Win32Error();
	return x;
}

/* Call Windows API function, showing a warning on NULL. */
template <typename T>
inline T prefer(const T x)
{
	if (!x) {
		EBMessageBox(Win32Error().what<TCHAR>(),
		    TEXT("System Error"), MB_ICONWARNING);
		return (T)NULL;
	}
	return x;
}

/* Conditionally select between ANSI and wide string literal. */
#define AWTEXT(t, s) cond_text<t>(s, L"" s)
template <typename T> constexpr const T* cond_text(const char*, const wchar_t*);
template <> constexpr const char* cond_text<char>(const char* sz, const wchar_t*) { return sz; }
template <> constexpr const wchar_t* cond_text<wchar_t>(const char*, const wchar_t* wsz) { return wsz; }

/* Conditionally select between ANSI and wide Windows API function. */
#define AWFUN(t, f) cond_fun<t, f##A, f##W>()
template <typename T, auto F, auto G>
constexpr std::enable_if_t<std::is_same_v<T, char>, decltype(F)> cond_fun() { return F; }
template <typename T, auto F, auto G>
constexpr std::enable_if_t<std::is_same_v<T, wchar_t>, decltype(G)> cond_fun() { return G; }

/* Return integer scaled for current DPI. */
inline int Dpi(const int i)
{
	extern int g_iDPI;
	return MulDiv(i, g_iDPI, 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 pRr)
{
	if (!GetClientRect(hWnd, pRr)) return 0;
	return MapWindowPoints(hWnd, GetParent(hWnd), (POINT*)pRr, 2);
}

inline BOOL SetWindowRect(const HWND hWnd,
    const long left, const long top, const long right, const long bottom)
{
	return MoveWindow(hWnd,
	    left, top,
	    right-left, bottom-top,
	    TRUE);
}

inline BOOL SetWindowRect(const HWND hWnd, const RECT r)
{
	return SetWindowRect(hWnd, r.left, r.top, r.right, r.bottom);
}

#endif