-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathpoint.h
319 lines (273 loc) · 9.53 KB
/
point.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#pragma once
namespace til // Terminal Implementation Library. Also: "Today I Learned"
{
using CoordType = int32_t;
inline constexpr CoordType CoordTypeMin = INT32_MIN;
inline constexpr CoordType CoordTypeMax = INT32_MAX;
namespace details
{
template<typename T, typename U = T>
constexpr U extract(const ::base::CheckedNumeric<T>& num)
{
U val;
if (!num.AssignIfValid(&val))
{
throw gsl::narrowing_error{};
}
return val;
}
}
struct point
{
CoordType x = 0;
CoordType y = 0;
constexpr point() noexcept = default;
constexpr point(CoordType x, CoordType y) noexcept :
x{ x }, y{ y }
{
}
// This template will convert to point from floating-point args;
// a math type is required. If you _don't_ provide one, you're going to
// get a compile-time error about "cannot convert from initializer-list to til::point"
template<typename TilMath, typename T>
constexpr point(TilMath, const T x, const T y) :
x{ TilMath::template cast<CoordType>(x) }, y{ TilMath::template cast<CoordType>(y) }
{
}
constexpr bool operator==(const point rhs) const noexcept
{
// `__builtin_memcmp` isn't an official standard, but it's the
// only way at the time of writing to get a constexpr `memcmp`.
return __builtin_memcmp(this, &rhs, sizeof(rhs)) == 0;
}
constexpr bool operator!=(const point rhs) const noexcept
{
return __builtin_memcmp(this, &rhs, sizeof(rhs)) != 0;
}
constexpr explicit operator bool() const noexcept
{
return (x > 0) & (y > 0);
}
constexpr bool operator<(const point other) const noexcept
{
return y < other.y || (y == other.y && x < other.x);
}
constexpr bool operator<=(const point other) const noexcept
{
return y < other.y || (y == other.y && x <= other.x);
}
constexpr bool operator>(const point other) const noexcept
{
return y > other.y || (y == other.y && x > other.x);
}
constexpr bool operator>=(const point other) const noexcept
{
return y > other.y || (y == other.y && x >= other.x);
}
constexpr point operator+(const point other) const
{
auto copy = *this;
copy += other;
return copy;
}
constexpr point& operator+=(const point other)
{
x = details::extract(::base::CheckAdd(x, other.x));
y = details::extract(::base::CheckAdd(y, other.y));
return *this;
}
constexpr point operator-(const point other) const
{
auto copy = *this;
copy -= other;
return copy;
}
constexpr point& operator-=(const point other)
{
x = details::extract(::base::CheckSub(x, other.x));
y = details::extract(::base::CheckSub(y, other.y));
return *this;
}
constexpr point operator*(const point other) const
{
auto copy = *this;
copy *= other;
return copy;
}
constexpr point& operator*=(const point other)
{
x = details::extract(::base::CheckMul(x, other.x));
y = details::extract(::base::CheckMul(y, other.y));
return *this;
}
constexpr point operator/(const point other) const
{
auto copy = *this;
copy /= other;
return copy;
}
constexpr point& operator/=(const point other)
{
x = details::extract(::base::CheckDiv(x, other.x));
y = details::extract(::base::CheckDiv(y, other.y));
return *this;
}
constexpr point operator*(const til::CoordType scale) const
{
return point{
details::extract(::base::CheckMul(x, scale)),
details::extract(::base::CheckMul(y, scale)),
};
}
constexpr point operator/(const til::CoordType scale) const
{
return point{
details::extract(::base::CheckDiv(x, scale)),
details::extract(::base::CheckDiv(y, scale)),
};
}
template<typename T>
constexpr T narrow_x() const
{
return gsl::narrow<T>(x);
}
template<typename T>
constexpr T narrow_y() const
{
return gsl::narrow<T>(y);
}
#ifdef _WINDEF_
explicit constexpr point(const POINT other) noexcept :
x{ other.x }, y{ other.y }
{
}
constexpr POINT to_win32_point() const noexcept
{
return { x, y };
}
// til::point and POINT have the exact same layout at the time of writing,
// so this function lets you unsafely "view" this point as a POINT
// if you need to pass it to a Win32 function.
//
// Use as_win32_point() as sparingly as possible because it'll be a pain to hack
// it out of this code base once til::point and POINT aren't the same anymore.
// Prefer casting to POINT and back to til::point instead if possible.
POINT* as_win32_point() noexcept
{
#pragma warning(suppress : 26490) // Don't use reinterpret_cast (type.1).
return std::launder(reinterpret_cast<POINT*>(this));
}
#endif
#ifdef DCOMMON_H_INCLUDED
template<typename TilMath>
constexpr point(TilMath, const D2D1_POINT_2F other) :
x{ TilMath::template cast<CoordType>(other.x) },
y{ TilMath::template cast<CoordType>(other.y) }
{
}
constexpr D2D1_POINT_2F to_d2d_point() const noexcept
{
return { static_cast<float>(x), static_cast<float>(y) };
}
#endif
#ifdef WINRT_Windows_Foundation_H
template<typename TilMath>
constexpr point(TilMath, const winrt::Windows::Foundation::Point other) :
x{ TilMath::template cast<CoordType>(other.X) },
y{ TilMath::template cast<CoordType>(other.Y) }
{
}
winrt::Windows::Foundation::Point to_winrt_point() const noexcept
{
return { static_cast<float>(x), static_cast<float>(y) };
}
#endif
#ifdef WINRT_Microsoft_Terminal_Core_H
explicit constexpr point(const winrt::Microsoft::Terminal::Core::Point other) :
x{ other.X }, y{ other.Y }
{
}
winrt::Microsoft::Terminal::Core::Point to_core_point() const noexcept
{
return { x, y };
}
#endif
std::wstring to_string() const
{
return wil::str_printf<std::wstring>(L"(X:%d, Y:%d)", x, y);
}
};
constexpr point wrap_coord(const COORD pt) noexcept
{
return { pt.X, pt.Y };
}
constexpr COORD unwrap_coord(const point pt)
{
return {
gsl::narrow<short>(pt.x),
gsl::narrow<short>(pt.y),
};
}
constexpr HRESULT unwrap_coord_hr(const point pt, COORD& out) noexcept
{
short x = 0;
short y = 0;
if (narrow_maybe(pt.x, x) && narrow_maybe(pt.y, y))
{
out.X = x;
out.Y = y;
return S_OK;
}
RETURN_WIN32(ERROR_UNHANDLED_EXCEPTION);
}
// point_span can be pictured as a "selection" range inside our text buffer. So given
// a text buffer of 10x4, a start of 4,1 and end of 7,3 the span might look like this:
// +----------+
// | |
// | xxxxxx|
// |xxxxxxxxxx|
// |xxxxxxxx |
// +----------+
// At the time of writing there's a push to make selections have an exclusive end coordinate,
// so the interpretation of end might change soon (making this comment potentially outdated).
struct point_span
{
til::point start;
til::point end;
};
}
#ifdef __WEX_COMMON_H__
namespace WEX::TestExecution
{
template<>
class VerifyOutputTraits<til::point>
{
public:
static WEX::Common::NoThrowString ToString(const til::point point)
{
return WEX::Common::NoThrowString(point.to_string().c_str());
}
};
template<>
class VerifyCompareTraits<til::point, til::point>
{
public:
static constexpr bool AreEqual(const til::point expected, const til::point actual) noexcept
{
return expected == actual;
}
static constexpr bool AreSame(const til::point expected, const til::point actual) noexcept
{
return &expected == &actual;
}
static constexpr bool IsLessThan(const til::point expectedLess, const til::point expectedGreater) = delete;
static constexpr bool IsGreaterThan(const til::point expectedGreater, const til::point expectedLess) = delete;
static constexpr bool IsNull(const til::point object) noexcept
{
return object == til::point{};
}
};
};
#endif