-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
141 lines (112 loc) · 4.15 KB
/
util.h
File metadata and controls
141 lines (112 loc) · 4.15 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#if !defined(UTIL_H)
#define UTIL_H
#include <memory>
#include <string>
#include <string_view>
#include <utility>
namespace trainlist8 {
namespace util {
extern "C" using WSAsyncCallback = void WINAPI(HRESULT, WS_CALLBACK_MODEL, void *);
// An adapter that allows a Windows Web Services asynchronous operation to be co_awaited in WinRT.
class AsyncWSAdapter final {
public:
explicit AsyncWSAdapter();
AsyncWSAdapter(AsyncWSAdapter &&);
operator WS_ASYNC_CONTEXT *();
winrt::Windows::Foundation::IAsyncAction checkChannelOperation(HRESULT immediateResult, WS_CHANNEL *channel);
private:
winrt::handle event;
WS_ASYNC_CONTEXT context;
HRESULT asyncResult;
static WSAsyncCallback rawCallback;
};
// A delete for channels that can be used with unique_ptr.
class ChannelDeleter final {
public:
void operator()(WS_CHANNEL *channel) const;
private:
static WSAsyncCallback rawCallback;
};
// A deleter for fonts that can be used with unique_ptr.
class FontDeleter final {
public:
using pointer = HFONT;
void operator()(HFONT font) const;
};
// A deleter for Windows Web Services heaps that can be used with unique_ptr.
class HeapDeleter final {
public:
void operator()(WS_HEAP *heap) const;
};
// A deleter for image lists that can be used with unique_ptr.
class ImageListDeleter final {
public:
using pointer = HIMAGELIST;
void operator()(HIMAGELIST imageList) const;
};
// A deleter for messages that can be used with unique_ptr.
class MessageDeleter final {
public:
void operator()(WS_MESSAGE *message) const;
};
// An RAII-managed registration of a window class.
class WindowClassRegistration final {
public:
explicit WindowClassRegistration(const WNDCLASSEXW &wc);
explicit WindowClassRegistration(const WindowClassRegistration &) = delete;
~WindowClassRegistration();
void operator=(const WindowClassRegistration &) = delete;
private:
const wchar_t *className;
HINSTANCE instance;
};
namespace impl {
template<std::size_t n>
class char8_literal final {
public:
char8_t chars[n];
private:
template<std::size_t ... i>
constexpr char8_literal(const char8_t(&s)[n], std::index_sequence<i...>) :
chars{s[i]...} {
}
public:
constexpr char8_literal(const char8_t(&s)[n]) :
char8_literal(s, std::make_index_sequence<n>()) {
}
};
template<char8_literal literal, std::size_t ... i>
constexpr const std::array<unsigned char, sizeof...(i)> bytes_of_string{literal.chars[i] ...};
template<char8_literal literal, std::size_t ... i>
constexpr const std::array<unsigned char, sizeof...(i)> &make_bytes_of_string(std::index_sequence<i...>) {
return bytes_of_string<literal, i...>;
}
}
// An XML string pointing at a string literal.
template<impl::char8_literal literal>
constexpr WS_XML_STRING operator""_as_xml() {
const std::array<unsigned char, std::size(literal.chars)> &a = impl::make_bytes_of_string<literal>(std::make_index_sequence<std::size(literal.chars)>());
WS_XML_STRING ret = {
.length = a.size() - 1,
.bytes = const_cast<unsigned char *>(a.data()),
};
return ret;
}
// Constructs the font used in message boxes, but at a specified point size scaled for a screen of a specified DPI.
std::unique_ptr<HFONT, FontDeleter> createMessageBoxFont(unsigned int size, unsigned int dpi);
// Create a window, throwing an exception on failure.
HWND createWindowEx(DWORD exStyle, const wchar_t *className, const wchar_t *windowName, DWORD style, int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE instance, void *param);
// Loads an icon, throwing an exception on failure.
HICON loadIconWithScaleDown(HINSTANCE instance, const wchar_t *name, int width, int height);
// Loads an image, throwing an exception on failure.
HANDLE loadImage(HINSTANCE instance, const wchar_t *name, unsigned int type, int width, int height, unsigned int options);
// Loads a string, throwing an exception on failure.
std::wstring loadString(HINSTANCE instance, unsigned int id);
// Loads a string, throwing an exception on failure.
std::wstring_view loadStringView(HINSTANCE instance, unsigned int id);
// Loads a string and formats it with inserts, throwing an exception on failure.
std::wstring loadAndFormatString(HINSTANCE instance, unsigned int id, ...);
}
}
#endif