aboutsummaryrefslogtreecommitdiff
path: root/c/common.h
blob: acb8f26922e84beca1cd5e0bdf6fac19c1af4223 (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
#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(DWORD dwErr);
	~Win32Error();
	virtual const char* what() const noexcept override;
	virtual const TCHAR* twhat() const noexcept;
private:
	DWORD m_dwErr;
	char* m_szMsg = NULL;
	wchar_t* m_wszMsg = NULL;
};

struct Library
{
	Library(const TCHAR* tszLibrary);
	~Library();
	template <typename 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 <class C, typename ...T>
std::optional<C> maybe_make(T ...args)
{
	try {
		return C(args...);
	} catch (...) {
		return {};
	}
}

/* Call Windows API function and throw error if NULL is returned. */
template <auto f, typename ...T>
inline auto throw_nil(T ...args)
{
	auto r = f(args...);
	if (!r) throw Win32Error(GetLastError());
	return r;
}

/* Call Windows API function and show a warning if NULL is returned. */
template <auto f, typename ...T>
inline auto warn_nil(T ...args)
{
	decltype(f(args...)) r;
	try {
		r = throw_nil<f>(args...);
	} catch (Win32Error& e) {
		EBMessageBox(e.twhat(), TEXT("System Error"), MB_ICONWARNING);
	}
	return r;
}

/* 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;
}

#endif