Mir
point_generic.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_GENERIC_H_
18 #define MIR_GEOMETRY_POINT_GENERIC_H_
19 
20 #include "dimensions_generic.h"
21 #include <ostream>
22 
23 namespace mir
24 {
25 namespace geometry
26 {
27 namespace detail
28 {
29 struct PointBase{};
30 }
31 namespace generic
32 {
33 template<template<typename> typename T>
34 struct Size;
35 template<template<typename> typename T>
36 struct Displacement;
37 
38 template<template<typename> typename T>
40 {
41  template<typename Tag>
42  using Corresponding = T<Tag>;
43 
44  using SizeType = Size<T>;
46 
47  constexpr Point() = default;
48  constexpr Point(Point const&) = default;
49  Point& operator=(Point const&) = default;
50 
51  template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
52  explicit constexpr Point(P const& other) noexcept
53  : x{T<XTag>{other.x}},
54  y{T<YTag>{other.y}}
55  {
56  }
57 
58  template<typename XType, typename YType>
59  constexpr Point(XType&& x, YType&& y) : x(x), y(y) {}
60 
61  T<XTag> x;
62  T<YTag> y;
63 };
64 
65 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
66 inline constexpr bool operator == (P const& lhs, P const& rhs)
67 {
68  return lhs.x == rhs.x && lhs.y == rhs.y;
69 }
70 
71 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
72 inline constexpr bool operator != (P const& lhs, P const& rhs)
73 {
74  return lhs.x != rhs.x || lhs.y != rhs.y;
75 }
76 
77 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
78 inline constexpr P operator+(P lhs, Corresponding<P, DeltaXTag> rhs) { return{lhs.x + rhs, lhs.y}; }
79 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
80 inline constexpr P operator+(P lhs, Corresponding<P, DeltaYTag> rhs) { return{lhs.x, lhs.y + rhs}; }
81 
82 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
83 inline constexpr P operator-(P lhs, Corresponding<P, DeltaXTag> rhs) { return{lhs.x - rhs, lhs.y}; }
84 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
85 inline constexpr P operator-(P lhs, Corresponding<P, DeltaYTag> rhs) { return{lhs.x, lhs.y - rhs}; }
86 
87 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
88 inline P& operator+=(P& lhs, Corresponding<P, DeltaXTag> rhs) { lhs.x += rhs; return lhs; }
89 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
90 inline P& operator+=(P& lhs, Corresponding<P, DeltaYTag> rhs) { lhs.y += rhs; return lhs; }
91 
92 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
93 inline P& operator-=(P& lhs, Corresponding<P, DeltaXTag> rhs) { lhs.x -= rhs; return lhs; }
94 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
95 inline P& operator-=(P& lhs, Corresponding<P, DeltaYTag> rhs) { lhs.y -= rhs; return lhs; }
96 
97 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
98 std::ostream& operator<<(std::ostream& out, P const& value)
99 {
100  out << value.x << ", " << value.y;
101  return out;
102 }
103 
104 }
105 }
106 }
107 
108 #endif // MIR_GEOMETRY_POINT_GENERIC_H_
constexpr D operator-(D const &lhs, D const &rhs)
Definition: displacement_generic.h:94
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
typename GeometricType::template Corresponding< Tag > Corresponding
Definition: dimensions_generic.h:139
constexpr bool operator!=(D const &lhs, D const &rhs)
Definition: displacement_generic.h:75
constexpr D::PointType & operator-=(typename D::PointType &lhs, D const &rhs)
Definition: displacement_generic.h:136
constexpr D operator+(D const &lhs, D const &rhs)
Definition: displacement_generic.h:88
constexpr bool operator==(D const &lhs, D const &rhs)
Definition: displacement_generic.h:69
constexpr D::PointType & operator+=(typename D::PointType &lhs, D const &rhs)
Definition: displacement_generic.h:130
Definition: splash_session.h:22
Definition: displacement.h:30
Definition: size.h:30
Used for determining if a type is a point.
Definition: point_generic.h:29
Definition: displacement_generic.h:43
Definition: point_generic.h:40
T< XTag > x
Definition: point_generic.h:61
Point & operator=(Point const &)=default
T< Tag > Corresponding
Definition: point_generic.h:42
T< YTag > y
Definition: point_generic.h:62
constexpr Point(P const &other) noexcept
Definition: point_generic.h:52
constexpr Point()=default
constexpr Point(Point const &)=default
constexpr Point(XType &&x, YType &&y)
Definition: point_generic.h:59
Definition: size_generic.h:41

Copyright © 2012-2022 Canonical Ltd.
Generated on Tue May 31 01:20:27 UTC 2022
This documentation is licensed under the GPL version 2 or 3.