aboutsummaryrefslogtreecommitdiff
path: root/c/util.h
blob: 668d0407efbd5656736af0ed69e9d93183366adf (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef UTIL_H
#define UTIL_H

#include <cassert>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <windows.h>

#define CONCAT_IMPL(a, b) a##b
#define CONCAT(a, b) CONCAT_IMPL(a, b)
#define _ CONCAT(unused_, __LINE__)

#define SET_TERMINATE							\
	std::set_terminate([]() noexcept				\
	{								\
		EBMessageBox(What(), L"Fatal Error", MB_ICONERROR);	\
		_Exit(1);						\
	})

/* Scope guard. */
template <typename F>
struct Finally
{
	F f;
	inline Finally(F f) noexcept : f(f) {}
	inline ~Finally() { f(); }
};
#define FINALLY Finally _ = [=]()

/* Unique is similar to unique_ptr, but it is designed for pointers
 * and handles of type T, where T is not an RAII class. Further, the
 * managed object is assumed to be in an invalid state until checked
 * with Good or Bad. Upon destruction, the object, if valid, is passed
 * to a given destructor function F. */
template <typename T, auto F>
struct Unique
{
	union { T v; };
	bool ok = false;
	Unique() noexcept {}
	Unique(T v) noexcept : v(v) {}
	Unique& operator =(T v_) noexcept
	{
		Destroy();
		v = v_;
		ok = false;
		return *this;
	}
	Unique(Unique&& other) noexcept : v(std::move(other.v)), ok(other.ok)
	{
		other.ok = false;
	}
	Unique& operator =(Unique&& other) noexcept
	{
		Destroy();
		v = std::move(other.v);
		ok = other.ok;
		other.ok = false;
		return *this;
	}
	bool Good(T u) noexcept
	{
		return !(ok = v == u);
	}
	bool Bad(T u) noexcept
	{
		return !(ok = v != u);
	}
	template <typename, auto> class UniqueOk;
	template <auto E>
	UniqueOk<T, F> ThrowIf(T u) noexcept
	{
		if (v == u)
			throw E;
		return std::move(*this);
	}
	~Unique()
	{
		Destroy();
	}
private:
	void Destroy()
	{
		if (ok) {
			if constexpr (F) F(v);
			v.~T();
		}
	}
};

/* UniqueOk contains a Unique that has already been validated. */
template <typename T, auto F>
struct UniqueOk
{
	T v;
	UniqueOk(Unique<T, F>&& u) : v(std::move(u.v))
	{
		assert(u.ok, "UniqueOk may not be constructed from non-ok Unique");
		u.ok = false;
	}
	UniqueOk& operator =(Unique<T, F>&& u)
	{
		assert(u.ok, "UniqueOk may not be constructed from non-ok Unique");
		F(v);
		v = std::move(u.v);
		u.ok = false;
		return *this;
	}
	~UniqueOk()
	{
		F(v);
	}
};

/* Buf is a span-like structure of a buffer and its size. */
template <typename T>
struct Buf
{
	T* data;
	size_t c;
	Buf(T* data, size_t c) noexcept : data(data), c(c) {}
	Buf(std::basic_string<T>& s) noexcept : data(s.data()), c(s.capacity()) {}
	template <size_t N> Buf(T (&data)[N]) noexcept : data(data), c(N) {}
	operator T*() const noexcept { return data; }
	T& operator *() const noexcept { return *data; }
	T& operator [](size_t i) const noexcept { return data[i]; }
	Buf<T> operator +(size_t i) const noexcept { return {data+i, c-i}; }
	//T operator -(size_t i) { return {data-i, c+i}; }
};

inline int Cmp(const int a, const int b)
{
	if (a == b) return 0;
	if (a > b) return 1;
	return -1;
}

inline size_t Min(size_t a, size_t b)
{
	return a < b? a: b;
}

template <typename T, size_t N>
inline size_t Len(T (&)[N])
{
	return N-1;
}

/* Format wide string. */
template<typename... T>
inline std::enable_if_t<!std::disjunction_v<std::is_class<T>...>, int>
Swprintf(Buf<wchar_t> buf, const wchar_t* const fmt, T... xs)
{
	return _snwprintf_s(buf, buf.c, _TRUNCATE, fmt, xs...);
}

/* Format static narrow string. */
template<typename... T>
inline std::enable_if_t<!std::disjunction_v<std::is_class<T>...>, int>
Sprintf(Buf<char> buf, const char* const fmt, T... xs)
{
	return _snprintf_s(buf, buf.c, _TRUNCATE, fmt, xs...);
}

/* Copy to static wide string buffer. */
inline wchar_t* Wcscpy(Buf<wchar_t> dst, const wchar_t* const src)
{
	const size_t len = Min(dst.c, wcslen(src)+1);
	memcpy(dst, src, len*sizeof(wchar_t));
	dst[len-1] = 0;
	return dst;
}

/* Copy to static narrow string buffer. */
inline char* Strcpy(Buf<char> dst, const char* const src)
{
	const size_t len = Min(dst.c, strlen(src)+1);
	memcpy(dst, src, len);
	dst[len-1] = 0;
	return dst;
}

#endif