Mir
size.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_H_
18 #define MIR_GEOMETRY_SIZE_H_
19 
20 #include "forward.h"
21 #include "dimensions.h"
22 #include <ostream>
23 
24 namespace mir
25 {
26 namespace geometry
27 {
28 namespace generic
29 {
30 template<typename T>
31 struct Point;
32 template<typename T>
33 struct Displacement;
34 
35 template<typename T>
36 struct Size
37 {
38  using ValueType = T;
39 
40  constexpr Size() noexcept {}
41  constexpr Size(Size const&) noexcept = default;
42  Size& operator=(Size const&) noexcept = default;
43 
44  template<typename U>
45  explicit constexpr Size(Size<U> const& other) noexcept
46  : width{Width<T>{other.width}},
48  {
49  }
50 
51  template<typename WidthType, typename HeightType>
52  constexpr Size(WidthType&& width, HeightType&& height) noexcept : width(width), height(height) {}
53 
56 };
57 
58 template<typename T>
59 inline constexpr bool operator == (Size<T> const& lhs, Size<T> const& rhs)
60 {
61  return lhs.width == rhs.width && lhs.height == rhs.height;
62 }
63 
64 template<typename T>
65 inline constexpr bool operator != (Size<T> const& lhs, Size<T> const& rhs)
66 {
67  return lhs.width != rhs.width || lhs.height != rhs.height;
68 }
69 
70 template<typename T>
71 std::ostream& operator<<(std::ostream& out, Size<T> const& value)
72 {
73  out << '(' << value.width << ", " << value.height << ')';
74  return out;
75 }
76 
77 template<typename T, typename Scalar>
78 inline constexpr Size<T> operator*(Scalar scale, Size<T> const& size)
79 {
80  return Size<T>{scale*size.width, scale*size.height};
81 }
82 
83 template<typename T, typename Scalar>
84 inline constexpr Size<T> operator*(Size<T> const& size, Scalar scale)
85 {
86  return scale*size;
87 }
88 
89 template<typename T, typename Scalar>
90 inline constexpr Size<T> operator/(Size<T> const& size, Scalar scale)
91 {
92  return Size<T>{size.width / scale, size.height / scale};
93 }
94 
95 template<typename T>
96 inline constexpr Size<T> as_size(Point<T> const& point)
97 {
98  return Size<T>{point.x.as_value(), point.y.as_value()};
99 }
100 
101 template<typename T>
102 inline constexpr Point<T> as_point(Size<T> const& size)
103 {
104  return Point<T>{size.width.as_value(), size.height.as_value()};
105 }
106 }
107 }
108 }
109 
110 #endif // MIR_GEOMETRY_SIZE_H_

Copyright © 2012-2022 Canonical Ltd.
Generated on Tue Sep 13 03:20:30 UTC 2022
This documentation is licensed under the GPL version 2 or 3.