Mir
point.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2020 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 
24 namespace mir
25 {
26 namespace geometry
27 {
28 namespace generic
29 {
30 template<typename T>
31 struct Size;
32 template<typename T>
33 struct Displacement;
34 
35 template<typename T>
36 struct 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 
58 template<typename T>
59 inline constexpr bool operator == (Point<T> const& lhs, Point<T> const& rhs)
60 {
61  return lhs.x == rhs.x && lhs.y == rhs.y;
62 }
63 
64 template<typename T>
65 inline constexpr bool operator != (Point<T> const& lhs, Point<T> const& rhs)
66 {
67  return lhs.x != rhs.x || lhs.y != rhs.y;
68 }
69 
70 template<typename T>
71 inline constexpr Point<T> operator+(Point<T> lhs, DeltaX<T> rhs) { return{lhs.x + rhs, lhs.y}; }
72 template<typename T>
73 inline constexpr Point<T> operator+(Point<T> lhs, DeltaY<T> rhs) { return{lhs.x, lhs.y + rhs}; }
74 
75 template<typename T>
76 inline constexpr Point<T> operator-(Point<T> lhs, DeltaX<T> rhs) { return{lhs.x - rhs, lhs.y}; }
77 template<typename T>
78 inline constexpr Point<T> operator-(Point<T> lhs, DeltaY<T> rhs) { return{lhs.x, lhs.y - rhs}; }
79 
80 template<typename T>
81 inline Point<T>& operator+=(Point<T>& lhs, DeltaX<T> rhs) { lhs.x += rhs; return lhs; }
82 template<typename T>
83 inline Point<T>& operator+=(Point<T>& lhs, DeltaY<T> rhs) { lhs.y += rhs; return lhs; }
84 
85 template<typename T>
86 inline Point<T>& operator-=(Point<T>& lhs, DeltaX<T> rhs) { lhs.x -= rhs; return lhs; }
87 template<typename T>
88 inline Point<T>& operator-=(Point<T>& lhs, DeltaY<T> rhs) { lhs.y -= rhs; return lhs; }
89 
90 template<typename T>
91 std::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_

Copyright © 2012-2022 Canonical Ltd.
Generated on Thu Sep 8 12:37:23 UTC 2022
This documentation is licensed under the GPL version 2 or 3.