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
23namespace mir
24{
25namespace geometry
26{
27namespace detail
28{
29struct PointBase;
30struct SizeBase;
32}
33namespace generic
34{
35template<template<typename> typename T>
36struct Point;
37
38template<template<typename> typename T>
39struct Size;
40
41template<template<typename> typename T>
43{
44 template<typename Tag>
45 using Corresponding = T<Tag>;
46
49
50 constexpr Displacement() {}
51 constexpr Displacement(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
68template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
69inline constexpr bool operator==(D const& lhs, D const& rhs)
70{
71 return lhs.dx == rhs.dx && lhs.dy == rhs.dy;
72}
73
74template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
75inline constexpr bool operator!=(D const& lhs, D const& rhs)
76{
77 return lhs.dx != rhs.dx || lhs.dy != rhs.dy;
78}
79
80template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
81std::ostream& operator<<(std::ostream& out, D const& value)
82{
83 out << '(' << value.dx << ", " << value.dy << ')';
84 return out;
85}
86
87template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
88inline constexpr D operator+(D const& lhs, D const& rhs)
89{
90 return D{lhs.dx + rhs.dx, lhs.dy + rhs.dy};
91}
92
93template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
94inline constexpr D operator-(D const& lhs, D const& rhs)
95{
96 return D{lhs.dx - rhs.dx, lhs.dy - rhs.dy};
97}
98
99template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
100inline constexpr D operator-(D const& rhs)
101{
102 return D{-rhs.dx, -rhs.dy};
103}
104
105template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
106inline 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
111template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
112inline 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
117template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
118inline 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
123template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
124inline 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
129template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
130inline constexpr typename D::PointType& operator+=(typename D::PointType& lhs, D const& rhs)
131{
132 return lhs = lhs + rhs;
133}
134
135template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
136inline constexpr typename D::PointType& operator-=(typename D::PointType& lhs, D const& rhs)
137{
138 return lhs = lhs - rhs;
139}
140
141template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
142inline bool operator<(D const& lhs, D const& rhs)
143{
144 return lhs.length_squared() < rhs.length_squared();
145}
146
147template<typename Scalar, typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
148inline constexpr D operator*(Scalar scale, D const& disp)
149{
150 return D{scale*disp.dx, scale*disp.dy};
151}
152
153template<typename Scalar, typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
154inline constexpr D operator*(D const& disp, Scalar scale)
155{
156 return scale*disp;
157}
158
159template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
160inline 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
165template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
166inline 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
171template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
172inline 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
177template<typename D, typename std::enable_if<std::is_base_of<detail::DisplacementBase, D>::value, bool>::type = true>
178inline 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_
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
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
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 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 D::PointType & operator-=(typename D::PointType &lhs, D const &rhs)
Definition: displacement_generic.h:136
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
Displacement & operator=(Displacement const &)=default
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
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 Thu Jul 21 23:32:06 UTC 2022
This documentation is licensed under the GPL version 2 or 3.