Mir
size_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_SIZE_GENERIC_H_
18 #define MIR_GEOMETRY_SIZE_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 }
32 namespace generic
33 {
34 template<template<typename> typename T>
35 struct Point;
36 template<template<typename> typename T>
37 struct Displacement;
38 
39 template<template<typename> typename T>
41 {
42  template<typename Tag>
43  using Corresponding = T<Tag>;
44 
47 
48  constexpr Size() noexcept {}
49  constexpr Size(Size const&) noexcept = default;
50  Size& operator=(Size const&) noexcept = default;
51 
52  template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
53  explicit constexpr Size(S const& other) noexcept
54  : width{T<WidthTag>{other.width}},
55  height{T<HeightTag>{other.height}}
56  {
57  }
58 
59  template<typename WidthType, typename HeightType>
60  constexpr Size(WidthType&& width, HeightType&& height) noexcept : width(width), height(height) {}
61 
62  T<WidthTag> width;
63  T<HeightTag> height;
64 };
65 
66 template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
67 inline constexpr bool operator == (S const& lhs, S const& rhs)
68 {
69  return lhs.width == rhs.width && lhs.height == rhs.height;
70 }
71 
72 template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
73 inline constexpr bool operator != (S const& lhs, S const& rhs)
74 {
75  return lhs.width != rhs.width || lhs.height != rhs.height;
76 }
77 
78 template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
79 std::ostream& operator<<(std::ostream& out, S const& value)
80 {
81  out << '(' << value.width << ", " << value.height << ')';
82  return out;
83 }
84 
85 template<typename Scalar, typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
86 inline constexpr S operator*(Scalar scale, S const& size)
87 {
88  return S{scale*size.width, scale*size.height};
89 }
90 
91 template<typename Scalar, typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
92 inline constexpr S operator*(S const& size, Scalar scale)
93 {
94  return scale*size;
95 }
96 
97 template<typename Scalar, typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
98 inline constexpr S operator/(S const& size, Scalar scale)
99 {
100  return S{size.width / scale, size.height / scale};
101 }
102 
103 template<typename P, typename std::enable_if<std::is_base_of<detail::PointBase, P>::value, bool>::type = true>
104 inline constexpr typename P::SizeType as_size(P const& point)
105 {
106  return typename P::SizeType{point.x.as_value(), point.y.as_value()};
107 }
108 
109 template<typename S, typename std::enable_if<std::is_base_of<detail::SizeBase, S>::value, bool>::type = true>
110 inline constexpr typename S::PointType as_point(S const& size)
111 {
112  return typename S::PointType{size.width.as_value(), size.height.as_value()};
113 }
114 }
115 }
116 }
117 
118 #endif // MIR_GEOMETRY_SIZE_GENERIC_H_
std::ostream & operator<<(std::ostream &out, W const &value)
Definition: dimensions_generic.h:142
constexpr S operator/(S const &size, Scalar scale)
Definition: size_generic.h:98
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::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
Definition: splash_session.h:22
Definition: displacement.h:30
Definition: point.h:30
Used for determining if a type is a size.
Definition: size_generic.h:30
Definition: displacement_generic.h:43
Definition: point_generic.h:40
Definition: size_generic.h:41
Size & operator=(Size const &) noexcept=default
T< Tag > Corresponding
Definition: size_generic.h:43
T< WidthTag > width
Definition: size_generic.h:62
constexpr Size() noexcept
Definition: size_generic.h:48
constexpr Size(S const &other) noexcept
Definition: size_generic.h:53
constexpr Size(WidthType &&width, HeightType &&height) noexcept
Definition: size_generic.h:60
T< HeightTag > height
Definition: size_generic.h:63
constexpr Size(Size const &) noexcept=default

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.