diff options
Diffstat (limited to 'src/common/point.h')
| -rw-r--r-- | src/common/point.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/common/point.h b/src/common/point.h new file mode 100644 index 000000000..c0a52ad8d --- /dev/null +++ b/src/common/point.h | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <type_traits> | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | // Represents a point within a 2D space. | ||
| 12 | template <typename T> | ||
| 13 | struct Point { | ||
| 14 | static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type!"); | ||
| 15 | |||
| 16 | T x{}; | ||
| 17 | T y{}; | ||
| 18 | |||
| 19 | #define ARITHMETIC_OP(op, compound_op) \ | ||
| 20 | friend constexpr Point operator op(const Point& lhs, const Point& rhs) noexcept { \ | ||
| 21 | return { \ | ||
| 22 | .x = static_cast<T>(lhs.x op rhs.x), \ | ||
| 23 | .y = static_cast<T>(lhs.y op rhs.y), \ | ||
| 24 | }; \ | ||
| 25 | } \ | ||
| 26 | friend constexpr Point operator op(const Point& lhs, T value) noexcept { \ | ||
| 27 | return { \ | ||
| 28 | .x = static_cast<T>(lhs.x op value), \ | ||
| 29 | .y = static_cast<T>(lhs.y op value), \ | ||
| 30 | }; \ | ||
| 31 | } \ | ||
| 32 | friend constexpr Point operator op(T value, const Point& rhs) noexcept { \ | ||
| 33 | return { \ | ||
| 34 | .x = static_cast<T>(value op rhs.x), \ | ||
| 35 | .y = static_cast<T>(value op rhs.y), \ | ||
| 36 | }; \ | ||
| 37 | } \ | ||
| 38 | friend constexpr Point& operator compound_op(Point& lhs, const Point& rhs) noexcept { \ | ||
| 39 | lhs.x = static_cast<T>(lhs.x op rhs.x); \ | ||
| 40 | lhs.y = static_cast<T>(lhs.y op rhs.y); \ | ||
| 41 | return lhs; \ | ||
| 42 | } \ | ||
| 43 | friend constexpr Point& operator compound_op(Point& lhs, T value) noexcept { \ | ||
| 44 | lhs.x = static_cast<T>(lhs.x op value); \ | ||
| 45 | lhs.y = static_cast<T>(lhs.y op value); \ | ||
| 46 | return lhs; \ | ||
| 47 | } | ||
| 48 | ARITHMETIC_OP(+, +=) | ||
| 49 | ARITHMETIC_OP(-, -=) | ||
| 50 | ARITHMETIC_OP(*, *=) | ||
| 51 | ARITHMETIC_OP(/, /=) | ||
| 52 | #undef ARITHMETIC_OP | ||
| 53 | |||
| 54 | friend constexpr bool operator==(const Point&, const Point&) = default; | ||
| 55 | }; | ||
| 56 | |||
| 57 | } // namespace Common | ||