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

Copyright © 2012-2021 Canonical Ltd.
Generated on Fri Jun 11 07:44:02 UTC 2021
This documentation is licensed under the GPL version 2 or 3.