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

Copyright © 2012-2022 Canonical Ltd.
Generated on Thu Mar 3 22:46:49 UTC 2022
This documentation is licensed under the GPL version 2 or 3.