Mir
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
54 Width<T> width;
55 Height<T> height;
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_
Definition: size.h:29
constexpr Size< T > as_size(Point< T > const &point)
Definition: size.h:96
constexpr Point< T > as_point(Size< T > const &size)
Definition: size.h:102
constexpr Size< T > operator*(Size< T > const &size, Scalar scale)
Definition: size.h:84
constexpr Size< T > operator*(Scalar scale, Size< T > const &size)
Definition: size.h:78
constexpr Size< T > operator/(Size< T > const &size, Scalar scale)
Definition: size.h:90
constexpr bool operator!=(Size< T > const &lhs, Size< T > const &rhs)
Definition: size.h:65
constexpr bool operator==(Size< T > const &lhs, Size< T > const &rhs)
Definition: size.h:59
Basic geometry types. Types for dimensions, displacements, etc. and the operations that they support.
Definition: size.h:27
Definition: runner.h:27
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
Height< T > height
Definition: size.h:55
constexpr Size(Size const &) noexcept=default
Width< T > width
Definition: size.h:54

Copyright © 2012-2023 Canonical Ltd.
Generated on Tue 2 May 10:01:24 UTC 2023
This documentation is licensed under the GPL version 2 or 3.