Mir
dimensions_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_DIMENSIONS_GENERIC_H_
18 #define MIR_GEOMETRY_DIMENSIONS_GENERIC_H_
19 
20 #include <iosfwd>
21 #include <type_traits>
22 
23 namespace mir
24 {
27 namespace geometry
28 {
29 
34 struct WidthTag{};
35 struct HeightTag{};
36 struct XTag{};
37 struct YTag{};
38 struct DeltaXTag{};
39 struct DeltaYTag{};
41 
42 namespace detail
43 {
44 struct ValueWrapperBase{}; // Used for determining if a type is a wrapper
45 }
46 
47 namespace generic
48 {
49 template<typename T>
50 struct Value
51 {
56  template<typename Tag>
58  {
59  using ValueType = T;
60  using TagType = Tag;
61  template<typename OtherTag>
63 
64  template <typename Q = T>
65  constexpr typename std::enable_if<std::is_integral<Q>::value, int>::type as_int() const
66  {
67  return this->value;
68  }
69 
70  constexpr T as_value() const noexcept
71  {
72  return value;
73  }
74 
75  constexpr Wrapper() noexcept : value{} {}
76 
77  Wrapper& operator=(Wrapper const& that) noexcept
78  {
79  value = that.value;
80  return *this;
81  }
82 
83  constexpr Wrapper(Wrapper const& that) noexcept
84  : value{that.value}
85  {
86  }
87 
88  template<typename W, typename std::enable_if<std::is_same<typename W::TagType, Tag>::value, bool>::type = true>
89  explicit constexpr Wrapper(W const& value) noexcept
90  : value{static_cast<T>(value.as_value())}
91  {
92  }
93 
94  template<typename U, typename std::enable_if<std::is_scalar<U>::value, bool>::type = true>
95  explicit constexpr Wrapper(U const& value) noexcept
96  : value{static_cast<T>(value)}
97  {
98  }
99 
100  inline constexpr auto operator == (Wrapper<Tag> const& rhs) const -> bool
101  {
102  return value == rhs.as_value();
103  }
104 
105  inline constexpr auto operator != (Wrapper<Tag> const& rhs) const -> bool
106  {
107  return value != rhs.as_value();
108  }
109 
110  inline constexpr auto operator <= (Wrapper<Tag> const& rhs) const -> bool
111  {
112  return value <= rhs.as_value();
113  }
114 
115  inline constexpr auto operator >= (Wrapper<Tag> const& rhs) const -> bool
116  {
117  return value >= rhs.as_value();
118  }
119 
120  inline constexpr auto operator < (Wrapper<Tag> const& rhs) const -> bool
121  {
122  return value < rhs.as_value();
123  }
124 
125  inline constexpr auto operator > (Wrapper<Tag> const& rhs) const -> bool
126  {
127  return value > rhs.as_value();
128  }
129 
130  protected:
131  T value;
132  };
133 
134 private:
135  Value();
136 };
137 
138 template<class GeometricType, typename Tag>
139 using Corresponding = typename GeometricType::template Corresponding<Tag>;
140 
141 template<typename W, typename std::enable_if<std::is_base_of<detail::ValueWrapperBase, W>::value, bool>::type = true>
142 std::ostream& operator<<(std::ostream& out, W const& value)
143 {
144  out << value.as_value();
145  return out;
146 }
147 
148 template<typename T> using Width = typename Value<T>::template Wrapper<WidthTag>;
149 template<typename T> using Height = typename Value<T>::template Wrapper<HeightTag>;
150 template<typename T> using X = typename Value<T>::template Wrapper<XTag>;
151 template<typename T> using Y = typename Value<T>::template Wrapper<YTag>;
152 template<typename T> using DeltaX = typename Value<T>::template Wrapper<DeltaXTag>;
153 template<typename T> using DeltaY = typename Value<T>::template Wrapper<DeltaYTag>;
154 } // namespace generic
155 
156 // Adding deltas is fine
157 template<template<typename> typename T>
158 inline constexpr T<DeltaXTag> operator+(T<DeltaXTag> lhs, T<DeltaXTag> rhs){ return T<DeltaXTag>(lhs.as_value() + rhs.as_value()); }
159 template<template<typename> typename T>
160 inline constexpr T<DeltaYTag> operator+(T<DeltaYTag> lhs, T<DeltaYTag> rhs) { return T<DeltaYTag>(lhs.as_value() + rhs.as_value()); }
161 template<template<typename> typename T>
162 inline constexpr T<DeltaXTag> operator-(T<DeltaXTag> lhs, T<DeltaXTag> rhs) { return T<DeltaXTag>(lhs.as_value() - rhs.as_value()); }
163 template<template<typename> typename T>
164 inline constexpr T<DeltaYTag> operator-(T<DeltaYTag> lhs, T<DeltaYTag> rhs) { return T<DeltaYTag>(lhs.as_value() - rhs.as_value()); }
165 template<template<typename> typename T>
166 inline constexpr T<DeltaXTag> operator-(T<DeltaXTag> rhs) { return T<DeltaXTag>(-rhs.as_value()); }
167 template<template<typename> typename T>
168 inline constexpr T<DeltaYTag> operator-(T<DeltaYTag> rhs) { return T<DeltaYTag>(-rhs.as_value()); }
169 template<template<typename> typename T>
170 inline T<DeltaXTag>& operator+=(T<DeltaXTag>& lhs, T<DeltaXTag> rhs) { return lhs = lhs + rhs; }
171 template<template<typename> typename T>
172 inline T<DeltaYTag>& operator+=(T<DeltaYTag>& lhs, T<DeltaYTag> rhs) { return lhs = lhs + rhs; }
173 template<template<typename> typename T>
174 inline T<DeltaXTag>& operator-=(T<DeltaXTag>& lhs, T<DeltaXTag> rhs) { return lhs = lhs - rhs; }
175 template<template<typename> typename T>
176 inline T<DeltaYTag>& operator-=(T<DeltaYTag>& lhs, T<DeltaYTag> rhs) { return lhs = lhs - rhs; }
177 
178 // Adding deltas to co-ordinates is fine
179 template<template<typename> typename T>
180 inline constexpr T<XTag> operator+(T<XTag> lhs, T<DeltaXTag> rhs) { return T<XTag>(lhs.as_value() + rhs.as_value()); }
181 template<template<typename> typename T>
182 inline constexpr T<YTag> operator+(T<YTag> lhs, T<DeltaYTag> rhs) { return T<YTag>(lhs.as_value() + rhs.as_value()); }
183 template<template<typename> typename T>
184 inline constexpr T<XTag> operator-(T<XTag> lhs, T<DeltaXTag> rhs) { return T<XTag>(lhs.as_value() - rhs.as_value()); }
185 template<template<typename> typename T>
186 inline constexpr T<YTag> operator-(T<YTag> lhs, T<DeltaYTag> rhs) { return T<YTag>(lhs.as_value() - rhs.as_value()); }
187 template<template<typename> typename T>
188 inline T<XTag>& operator+=(T<XTag>& lhs, T<DeltaXTag> rhs) { return lhs = lhs + rhs; }
189 template<template<typename> typename T>
190 inline T<YTag>& operator+=(T<YTag>& lhs, T<DeltaYTag> rhs) { return lhs = lhs + rhs; }
191 template<template<typename> typename T>
192 inline T<XTag>& operator-=(T<XTag>& lhs, T<DeltaXTag> rhs) { return lhs = lhs - rhs; }
193 template<template<typename> typename T>
194 inline T<YTag>& operator-=(T<YTag>& lhs, T<DeltaYTag> rhs) { return lhs = lhs - rhs; }
195 
196 // Adding deltas to generic::Width and generic::Height is fine
197 template<template<typename> typename T>
198 inline constexpr T<WidthTag> operator+(T<WidthTag> lhs, T<DeltaXTag> rhs) { return T<WidthTag>(lhs.as_value() + rhs.as_value()); }
199 template<template<typename> typename T>
200 inline constexpr T<HeightTag> operator+(T<HeightTag> lhs, T<DeltaYTag> rhs) { return T<HeightTag>(lhs.as_value() + rhs.as_value()); }
201 template<template<typename> typename T>
202 inline constexpr T<WidthTag> operator-(T<WidthTag> lhs, T<DeltaXTag> rhs) { return T<WidthTag>(lhs.as_value() - rhs.as_value()); }
203 template<template<typename> typename T>
204 inline constexpr T<HeightTag> operator-(T<HeightTag> lhs, T<DeltaYTag> rhs) { return T<HeightTag>(lhs.as_value() - rhs.as_value()); }
205 template<template<typename> typename T>
206 inline T<WidthTag>& operator+=(T<WidthTag>& lhs, T<DeltaXTag> rhs) { return lhs = lhs + rhs; }
207 template<template<typename> typename T>
208 inline T<HeightTag>& operator+=(T<HeightTag>& lhs, T<DeltaYTag> rhs) { return lhs = lhs + rhs; }
209 template<template<typename> typename T>
210 inline T<WidthTag>& operator-=(T<WidthTag>& lhs, T<DeltaXTag> rhs) { return lhs = lhs - rhs; }
211 template<template<typename> typename T>
212 inline T<HeightTag>& operator-=(T<HeightTag>& lhs, T<DeltaYTag> rhs) { return lhs = lhs - rhs; }
213 
214 // Adding Widths and Heights is fine
215 template<template<typename> typename T>
216 inline constexpr T<WidthTag> operator+(T<WidthTag> lhs, T<WidthTag> rhs) { return T<WidthTag>(lhs.as_value() + rhs.as_value()); }
217 template<template<typename> typename T>
218 inline constexpr T<HeightTag> operator+(T<HeightTag> lhs, T<HeightTag> rhs) { return T<HeightTag>(lhs.as_value() + rhs.as_value()); }
219 template<template<typename> typename T>
220 inline T<WidthTag>& operator+=(T<WidthTag>& lhs, T<WidthTag> rhs) { return lhs = lhs + rhs; }
221 template<template<typename> typename T>
222 inline T<HeightTag>& operator+=(T<HeightTag>& lhs, T<HeightTag> rhs) { return lhs = lhs + rhs; }
223 
224 // Subtracting coordinates is fine
225 template<template<typename> typename T>
226 inline constexpr T<DeltaXTag> operator-(T<XTag> lhs, T<XTag> rhs) { return T<DeltaXTag>(lhs.as_value() - rhs.as_value()); }
227 template<template<typename> typename T>
228 inline constexpr T<DeltaYTag> operator-(T<YTag> lhs, T<YTag> rhs) { return T<DeltaYTag>(lhs.as_value() - rhs.as_value()); }
229 
230 //Subtracting T<WidthTag> and T<HeightTag> is fine
231 template<template<typename> typename T>
232 inline constexpr T<DeltaXTag> operator-(T<WidthTag> lhs, T<WidthTag> rhs) { return T<DeltaXTag>(lhs.as_value() - rhs.as_value()); }
233 template<template<typename> typename T>
234 inline constexpr T<DeltaYTag> operator-(T<HeightTag> lhs, T<HeightTag> rhs) { return T<DeltaYTag>(lhs.as_value() - rhs.as_value()); }
235 
236 // Multiplying by a scalar value is fine
237 template<template<typename> typename T, typename Scalar>
238 inline constexpr T<WidthTag> operator*(Scalar scale, T<WidthTag> const& w) { return T<WidthTag>{scale*w.as_value()}; }
239 template<template<typename> typename T, typename Scalar>
240 inline constexpr T<HeightTag> operator*(Scalar scale, T<HeightTag> const& h) { return T<HeightTag>{scale*h.as_value()}; }
241 template<template<typename> typename T, typename Scalar>
242 inline constexpr T<DeltaXTag> operator*(Scalar scale, T<DeltaXTag> const& dx) { return T<DeltaXTag>{scale*dx.as_value()}; }
243 template<template<typename> typename T, typename Scalar>
244 inline constexpr T<DeltaYTag> operator*(Scalar scale, T<DeltaYTag> const& dy) { return T<DeltaYTag>{scale*dy.as_value()}; }
245 template<template<typename> typename T, typename Scalar>
246 inline constexpr T<WidthTag> operator*(T<WidthTag> const& w, Scalar scale) { return scale*w; }
247 template<template<typename> typename T, typename Scalar>
248 inline constexpr T<HeightTag> operator*(T<HeightTag> const& h, Scalar scale) { return scale*h; }
249 template<template<typename> typename T, typename Scalar>
250 inline constexpr T<DeltaXTag> operator*(T<DeltaXTag> const& dx, Scalar scale) { return scale*dx; }
251 template<template<typename> typename T, typename Scalar>
252 inline constexpr T<DeltaYTag> operator*(T<DeltaYTag> const& dy, Scalar scale) { return scale*dy; }
253 
254 // Dividing by a scaler value is fine
255 template<template<typename> typename T, typename Scalar>
256 inline constexpr T<WidthTag> operator/(T<WidthTag> const& w, Scalar scale) { return T<WidthTag>{w.as_value() / scale}; }
257 template<template<typename> typename T, typename Scalar>
258 inline constexpr T<HeightTag> operator/(T<HeightTag> const& h, Scalar scale) { return T<HeightTag>{h.as_value() / scale}; }
259 template<template<typename> typename T, typename Scalar>
260 inline constexpr T<DeltaXTag> operator/(T<DeltaXTag> const& dx, Scalar scale) { return T<DeltaXTag>{dx.as_value() / scale}; }
261 template<template<typename> typename T, typename Scalar>
262 inline constexpr T<DeltaYTag> operator/(T<DeltaYTag> const& dy, Scalar scale) { return T<DeltaYTag>{dy.as_value() / scale}; }
263 
264 // Converting between types is fine, as long as they are along the same axis
265 template<template<typename> typename T>
266 inline constexpr T<WidthTag> as_width(T<DeltaXTag> const& dx) { return T<WidthTag>{dx.as_value()}; }
267 template<template<typename> typename T>
268 inline constexpr T<HeightTag> as_height(T<DeltaYTag> const& dy) { return T<HeightTag>{dy.as_value()}; }
269 template<template<typename> typename T>
270 inline constexpr T<XTag> as_x(T<DeltaXTag> const& dx) { return T<XTag>{dx.as_value()}; }
271 template<template<typename> typename T>
272 inline constexpr T<YTag> as_y(T<DeltaYTag> const& dy) { return T<YTag>{dy.as_value()}; }
273 template<template<typename> typename T>
274 inline constexpr T<DeltaXTag> as_delta(T<XTag> const& x) { return T<DeltaXTag>{x.as_value()}; }
275 template<template<typename> typename T>
276 inline constexpr T<DeltaYTag> as_delta(T<YTag> const& y) { return T<DeltaYTag>{y.as_value()}; }
277 template<template<typename> typename T>
278 inline constexpr T<XTag> as_x(T<WidthTag> const& w) { return T<XTag>{w.as_value()}; }
279 template<template<typename> typename T>
280 inline constexpr T<YTag> as_y(T<HeightTag> const& h) { return T<YTag>{h.as_value()}; }
281 template<template<typename> typename T>
282 inline constexpr T<WidthTag> as_width(T<XTag> const& x) { return T<WidthTag>{x.as_value()}; }
283 template<template<typename> typename T>
284 inline constexpr T<HeightTag> as_height(T<YTag> const& y) { return T<HeightTag>{y.as_value()}; }
285 template<template<typename> typename T>
286 inline constexpr T<DeltaXTag> as_delta(T<WidthTag> const& w) { return T<DeltaXTag>{w.as_value()}; }
287 template<template<typename> typename T>
288 inline constexpr T<DeltaYTag> as_delta(T<HeightTag> const& h) { return T<DeltaYTag>{h.as_value()}; }
289 
290 template<typename Target, typename Source>
291 inline constexpr Target dim_cast(Source s) { return Target(s.as_value()); }
292 } // namespace geometry
293 } // namespace mir
294 
295 #endif // MIR_GEOMETRY_DIMENSIONS_GENERIC_H_
typename Value< T >::template Wrapper< WidthTag > Width
Definition: dimensions_generic.h:148
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
typename Value< T >::template Wrapper< DeltaYTag > DeltaY
Definition: dimensions_generic.h:153
typename GeometricType::template Corresponding< Tag > Corresponding
Definition: dimensions_generic.h:139
typename Value< T >::template Wrapper< YTag > Y
Definition: dimensions_generic.h:151
typename Value< T >::template Wrapper< DeltaXTag > DeltaX
Definition: dimensions_generic.h:152
typename Value< T >::template Wrapper< XTag > X
Definition: dimensions_generic.h:150
typename Value< T >::template Wrapper< HeightTag > Height
Definition: dimensions_generic.h:149
constexpr T< WidthTag > operator*(Scalar scale, T< WidthTag > const &w)
Definition: dimensions_generic.h:238
constexpr T< HeightTag > as_height(T< DeltaYTag > const &dy)
Definition: dimensions_generic.h:268
constexpr T< XTag > as_x(T< DeltaXTag > const &dx)
Definition: dimensions_generic.h:270
constexpr T< DeltaXTag > as_delta(T< XTag > const &x)
Definition: dimensions_generic.h:274
constexpr T< DeltaXTag > operator+(T< DeltaXTag > lhs, T< DeltaXTag > rhs)
Definition: dimensions_generic.h:158
constexpr T< WidthTag > as_width(T< DeltaXTag > const &dx)
Definition: dimensions_generic.h:266
constexpr T< YTag > as_y(T< DeltaYTag > const &dy)
Definition: dimensions_generic.h:272
constexpr T< WidthTag > operator/(T< WidthTag > const &w, Scalar scale)
Definition: dimensions_generic.h:256
T< DeltaXTag > & operator-=(T< DeltaXTag > &lhs, T< DeltaXTag > rhs)
Definition: dimensions_generic.h:174
constexpr Target dim_cast(Source s)
Definition: dimensions_generic.h:291
constexpr T< DeltaXTag > operator-(T< DeltaXTag > lhs, T< DeltaXTag > rhs)
Definition: dimensions_generic.h:162
T< DeltaXTag > & operator+=(T< DeltaXTag > &lhs, T< DeltaXTag > rhs)
Definition: dimensions_generic.h:170
Definition: splash_session.h:22
Definition: dimensions_generic.h:38
Definition: dimensions_generic.h:39
Definition: dimensions_generic.h:35
These tag types determine what type of dimension a value holds and what operations are possible with ...
Definition: dimensions_generic.h:34
Definition: dimensions_generic.h:36
Definition: dimensions_generic.h:37
Definition: dimensions_generic.h:44
Wraps a geometry value and prevents it from being accidentally used for invalid operations (such as s...
Definition: dimensions_generic.h:58
constexpr std::enable_if< std::is_integral< Q >::value, int >::type as_int() const
Definition: dimensions_generic.h:65
constexpr auto operator<(Wrapper< Tag > const &rhs) const -> bool
Definition: dimensions_generic.h:120
constexpr auto operator!=(Wrapper< Tag > const &rhs) const -> bool
Definition: dimensions_generic.h:105
constexpr Wrapper(W const &value) noexcept
Definition: dimensions_generic.h:89
constexpr Wrapper(U const &value) noexcept
Definition: dimensions_generic.h:95
constexpr Wrapper(Wrapper const &that) noexcept
Definition: dimensions_generic.h:83
Wrapper & operator=(Wrapper const &that) noexcept
Definition: dimensions_generic.h:77
constexpr T as_value() const noexcept
Definition: dimensions_generic.h:70
T value
Definition: dimensions_generic.h:131
constexpr auto operator>(Wrapper< Tag > const &rhs) const -> bool
Definition: dimensions_generic.h:125
constexpr auto operator==(Wrapper< Tag > const &rhs) const -> bool
Definition: dimensions_generic.h:100
T ValueType
Definition: dimensions_generic.h:59
Tag TagType
Definition: dimensions_generic.h:60
constexpr auto operator<=(Wrapper< Tag > const &rhs) const -> bool
Definition: dimensions_generic.h:110
constexpr auto operator>=(Wrapper< Tag > const &rhs) const -> bool
Definition: dimensions_generic.h:115
constexpr Wrapper() noexcept
Definition: dimensions_generic.h:75
Definition: dimensions_generic.h:51

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.