Mir
Loading...
Searching...
No Matches
size.h
Go to the documentation of this file.
1/*
2 * Copyright © 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
24namespace mir
25{
26namespace geometry
27{
28namespace generic
29{
30template<typename T>
31struct Point;
32template<typename T>
33struct Displacement;
34
35template<typename T>
36struct 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}},
47 height{Height<T>{other.height}}
48 {
49 }
50
51 template<typename WidthType, typename HeightType>
52 constexpr Size(WidthType&& width, HeightType&& height) noexcept : width(width), height(height) {}
53
56};
57
58template<typename T>
59inline constexpr bool operator == (Size<T> const& lhs, Size<T> const& rhs)
60{
61 return lhs.width == rhs.width && lhs.height == rhs.height;
62}
63
64template<typename T>
65inline constexpr bool operator != (Size<T> const& lhs, Size<T> const& rhs)
66{
67 return lhs.width != rhs.width || lhs.height != rhs.height;
68}
69
70template<typename T>
71std::ostream& operator<<(std::ostream& out, Size<T> const& value)
72{
73 out << '(' << value.width << ", " << value.height << ')';
74 return out;
75}
76
77template<typename T, typename Scalar>
78inline constexpr Size<T> operator*(Scalar scale, Size<T> const& size)
79{
80 return Size<T>{scale*size.width, scale*size.height};
81}
82
83template<typename T, typename Scalar>
84inline constexpr Size<T> operator*(Size<T> const& size, Scalar scale)
85{
86 return scale*size;
87}
88
89template<typename T, typename Scalar>
90inline constexpr Size<T> operator/(Size<T> const& size, Scalar scale)
91{
92 return Size<T>{size.width / scale, size.height / scale};
93}
94
95template<typename T>
96inline constexpr Size<T> as_size(Point<T> const& point)
97{
98 return Size<T>{point.x.as_value(), point.y.as_value()};
99}
100
101template<typename T>
102inline 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_
std::ostream & operator<<(std::ostream &out, Value< T, Tag > const &value)
Definition dimensions.h:118
constexpr bool operator==(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition displacement.h:75
constexpr Width< T > operator/(Width< T > const &w, Scalar scale)
Definition dimensions.h:224
constexpr Width< T > operator*(Scalar scale, Width< T > const &w)
Definition dimensions.h:206
constexpr Size< T > as_size(Displacement< T > const &disp)
Definition displacement.h:172
constexpr bool operator!=(Displacement< T > const &lhs, Displacement< T > const &rhs)
Definition displacement.h:81
constexpr Point< T > as_point(Displacement< T > const &disp)
Definition displacement.h:184
generic::Point< int > Point
Definition forward.h:81
generic::Displacement< int > Displacement
Definition forward.h:83
Definition splash_session.h:22
Definition point.h:37
X< T > x
Definition point.h:54
Y< T > y
Definition point.h:55
Definition size.h:37
Size & operator=(Size const &) noexcept=default
constexpr Size() noexcept
Definition size.h:40
constexpr Size(Size< U > const &other) noexcept
Definition size.h:45
constexpr Size(WidthType &&width, HeightType &&height) noexcept
Definition size.h:52
T ValueType
Definition size.h:38
Height< T > height
Definition size.h:55
constexpr Size(Size const &) noexcept=default
Width< T > width
Definition size.h:54
Wraps a geometry value and prevents it from being accidentally used for invalid operations (such as s...
Definition dimensions.h:37

Copyright © 2012-2025 Canonical Ltd.
Generated on Mon Jan 20 23:24:07 UTC 2025
This documentation is licensed under the GPL version 2 or 3.