Mir
point.h
Go to the documentation of this file.
1/*
2 * Copyright © Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2 or 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef MIR_GEOMETRY_POINT_H_
18#define MIR_GEOMETRY_POINT_H_
19
20#include "forward.h"
21#include "dimensions.h"
22#include <ostream>
23
24namespace mir
25{
26namespace geometry
27{
28namespace generic
29{
30template<typename T>
31struct Size;
32template<typename T>
33struct Displacement;
34
35template<typename T>
36struct Point
37{
38 using ValueType = T;
39
40 constexpr Point() = default;
41 constexpr Point(Point const&) = default;
42 Point& operator=(Point const&) = default;
43
44 template<typename U>
45 explicit constexpr Point(Point<U> const& other) noexcept
46 : x{X<T>{other.x}},
47 y{Y<T>{other.y}}
48 {
49 }
50
51 template<typename XType, typename YType>
52 constexpr Point(XType&& x, YType&& y) : x(x), y(y) {}
53
54 X<T> x;
55 Y<T> y;
56};
57
58template<typename T>
59inline constexpr bool operator == (Point<T> const& lhs, Point<T> const& rhs)
60{
61 return lhs.x == rhs.x && lhs.y == rhs.y;
62}
63
64template<typename T>
65inline constexpr bool operator != (Point<T> const& lhs, Point<T> const& rhs)
66{
67 return lhs.x != rhs.x || lhs.y != rhs.y;
68}
69
70template<typename T>
71inline constexpr Point<T> operator+(Point<T> lhs, DeltaX<T> rhs) { return{lhs.x + rhs, lhs.y}; }
72template<typename T>
73inline constexpr Point<T> operator+(Point<T> lhs, DeltaY<T> rhs) { return{lhs.x, lhs.y + rhs}; }
74
75template<typename T>
76inline constexpr Point<T> operator-(Point<T> lhs, DeltaX<T> rhs) { return{lhs.x - rhs, lhs.y}; }
77template<typename T>
78inline constexpr Point<T> operator-(Point<T> lhs, DeltaY<T> rhs) { return{lhs.x, lhs.y - rhs}; }
79
80template<typename T>
81inline Point<T>& operator+=(Point<T>& lhs, DeltaX<T> rhs) { lhs.x += rhs; return lhs; }
82template<typename T>
83inline Point<T>& operator+=(Point<T>& lhs, DeltaY<T> rhs) { lhs.y += rhs; return lhs; }
84
85template<typename T>
86inline Point<T>& operator-=(Point<T>& lhs, DeltaX<T> rhs) { lhs.x -= rhs; return lhs; }
87template<typename T>
88inline Point<T>& operator-=(Point<T>& lhs, DeltaY<T> rhs) { lhs.y -= rhs; return lhs; }
89
90template<typename T>
91std::ostream& operator<<(std::ostream& out, Point<T> const& value)
92{
93 out << value.x << ", " << value.y;
94 return out;
95}
96
97}
98}
99}
100
101#endif // MIR_GEOMETRY_POINT_H_
Definition: size.h:29
Point< T > & operator+=(Point< T > &lhs, DeltaX< T > rhs)
Definition: point.h:81
constexpr bool operator==(Point< T > const &lhs, Point< T > const &rhs)
Definition: point.h:59
Point< T > & operator-=(Point< T > &lhs, DeltaX< T > rhs)
Definition: point.h:86
constexpr Point< T > operator-(Point< T > lhs, DeltaX< T > rhs)
Definition: point.h:76
constexpr Point< T > operator-(Point< T > lhs, DeltaY< T > rhs)
Definition: point.h:78
constexpr bool operator!=(Point< T > const &lhs, Point< T > const &rhs)
Definition: point.h:65
Point< T > & operator-=(Point< T > &lhs, DeltaY< T > rhs)
Definition: point.h:88
constexpr Point< T > operator+(Point< T > lhs, DeltaY< T > rhs)
Definition: point.h:73
constexpr Point< T > operator+(Point< T > lhs, DeltaX< T > rhs)
Definition: point.h:71
Point< T > & operator+=(Point< T > &lhs, DeltaY< T > rhs)
Definition: point.h:83
Basic geometry types. Types for dimensions, displacements, etc. and the operations that they support.
Definition: size.h:27
Definition: runner.h:27
X< T > x
Definition: point.h:54
constexpr Point(Point< U > const &other) noexcept
Definition: point.h:45
Y< T > y
Definition: point.h:55
Point & operator=(Point const &)=default
constexpr Point()=default
constexpr Point(Point const &)=default
constexpr Point(XType &&x, YType &&y)
Definition: point.h:52
Definition: size.h:37

Copyright © 2012-2023 Canonical Ltd.
Generated on Tue 2 May 10:01:24 UTC 2023
This documentation is licensed under the GPL version 2 or 3.