Mir
displacement_generic.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2021 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_DISPLACEMENT_GENERIC_H_
18 #define MIR_GEOMETRY_DISPLACEMENT_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 struct SizeBase;
31 struct DisplacementBase{};
32 }
33 namespace generic
34 {
35 template<template<typename> typename T>
36 struct Point;
37 
38 template<template<typename> typename T>
39 struct Size;
40 
41 template<template<typename> typename T>
43 {
44  template<typename Tag>
45  using Corresponding = T<Tag>;
46 
48  using SizeType = Size<T>;
49 
50  constexpr Displacement() {}
51  constexpr Displacement(Displacement const&) = default;
52  Displacement& operator=(Displacement const&) = default;
53 
54  template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
55  explicit constexpr Displacement(D const& other) noexcept
56  : dx{T<DeltaXTag>{other.dx}},
57  dy{T<DeltaYTag>{other.dy}}
58  {
59  }
60 
61  template<typename DeltaXType, typename DeltaYType>
62  constexpr Displacement(DeltaXType&& dx, DeltaYType&& dy) : dx{dx}, dy{dy} {}
63 
64  T<DeltaXTag> dx;
65  T<DeltaYTag> dy;
66 };
67 
68 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
69 inline constexpr bool operator==(D const& lhs, D const& rhs)
70 {
71  return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
72 }
73 
74 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
75 inline constexpr bool operator!=(D const& lhs, D const& rhs)
76 {
77  return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
78 }
79 
80 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
81 std::ostream& operator<<(std::ostream& out, D const& value)
82 {
83  out << '(' << value.dx << ", " << value.dy << ')';
84  return out;
85 }
86 
87 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
88 inline constexpr D operator+(D const& lhs, D const& rhs)
89 {
90  return D{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
91 }
92 
93 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
94 inline constexpr D operator-(D const& lhs, D const& rhs)
95 {
96  return D{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
97 }
98 
99 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
100 inline constexpr D operator-(D const& rhs)
101 {
102  return D{-rhs.dx, -rhs.dy};
103 }
104 
105 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
106 inline constexpr typename D::PointType operator+(typename D::PointType const& lhs, D const& rhs)
107 {
108  return typename D::PointType{lhs.x + rhs.dx, lhs.y + rhs.dy};
109 }
110 
111 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
112 inline constexpr typename D::PointType operator+(D const& lhs, typename D::PointType const& rhs)
113 {
114  return typename D::PointType{rhs.x + lhs.dx, rhs.y + lhs.dy};
115 }
116 
117 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
118 inline constexpr typename D::PointType operator-(typename D::PointType const& lhs, D const& rhs)
119 {
120  return typename D::PointType{lhs.x - rhs.dx, lhs.y - rhs.dy};
121 }
122 
123 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
124 inline constexpr typename P::DisplacementType operator-(P const& lhs, P const& rhs)
125 {
126  return typename P::DisplacementType{lhs.x - rhs.x, lhs.y - rhs.y};
127 }
128 
129 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
130 inline constexpr typename D::PointType& operator+=(typename D::PointType& lhs, D const& rhs)
131 {
132  return lhs = lhs + rhs;
133 }
134 
135 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
136 inline constexpr typename D::PointType& operator-=(typename D::PointType& lhs, D const& rhs)
137 {
138  return lhs = lhs - rhs;
139 }
140 
141 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
142 inline bool operator<(D const& lhs, D const& rhs)
143 {
144  return lhs.length_squared() < rhs.length_squared();
145 }
146 
147 template<typename Scalar, typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
148 inline constexpr D operator*(Scalar scale, D const& disp)
149 {
150  return D{scale*disp.dx, scale*disp.dy};
151 }
152 
153 template<typename Scalar, typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
154 inline constexpr D operator*(D const& disp, Scalar scale)
155 {
156  return scale*disp;
157 }
158 
159 template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
160 inline constexpr typename S::DisplacementType as_displacement(S const& size)
161 {
162  return typename S::DisplacementType{size.width.as_value(), size.height.as_value()};
163 }
164 
165 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
166 inline constexpr typename D::SizeType as_size(D const& disp)
167 {
168  return typename D::SizeType{disp.dx.as_value(), disp.dy.as_value()};
169 }
170 
171 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
172 inline constexpr typename P::DisplacementType as_displacement(P const& point)
173 {
174  return typename P::DisplacementType{point.x.as_value(), point.y.as_value()};
175 }
176 
177 template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
178 inline constexpr typename D::PointType as_point(D const& disp)
179 {
180  return typename D::PointType{disp.dx.as_value(), disp.dy.as_value()};
181 }
182 }
183 }
184 }
185 
186 #endif // MIR_GEOMETRY_DISPLACEMENT_GENERIC_H_
constexpr D operator-(D const &lhs, D const &rhs)
Definition: displacement_generic.h:94
bool operator<(D const &lhs, D const &rhs)
Definition: displacement_generic.h:142
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
constexpr D::PointType as_point(D const &disp)
Definition: displacement_generic.h:178
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 D::SizeType as_size(D const &disp)
Definition: displacement_generic.h:166
constexpr D operator*(Scalar scale, D const &disp)
Definition: displacement_generic.h:148
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
constexpr S::DisplacementType as_displacement(S const &size)
Definition: displacement_generic.h:160
Definition: splash_session.h:22
Definition: point.h:30
Definition: size.h:30
Used for determining if a type is a displacement.
Definition: displacement_generic.h:31
Definition: displacement_generic.h:43
constexpr Displacement(D const &other) noexcept
Definition: displacement_generic.h:55
T< DeltaXTag > dx
Definition: displacement_generic.h:64
constexpr Displacement(Displacement const &)=default
T< DeltaYTag > dy
Definition: displacement_generic.h:65
T< Tag > Corresponding
Definition: displacement_generic.h:45
constexpr Displacement()
Definition: displacement_generic.h:50
Displacement & operator=(Displacement const &)=default
constexpr Displacement(DeltaXType &&dx, DeltaYType &&dy)
Definition: displacement_generic.h:62
Definition: point_generic.h:40
Definition: size_generic.h:41

Copyright © 2012-2022 Canonical Ltd.
Generated on Tue May 24 14:35:34 UTC 2022
This documentation is licensed under the GPL version 2 or 3.