Mir
length.h
Go to the documentation of this file.
1/*
2 * Copyright © 2014, 2016 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: Daniel van Vugt <daniel.van.vugt@canonical.com>
17 */
18
19#ifndef MIR_GEOMETRY_LENGTH_H_
20#define MIR_GEOMETRY_LENGTH_H_
21
22namespace mir
23{
24namespace geometry
25{
26
29class Length
30{
31public:
32 enum Units
33 {
36 centimetres = 10000,
37 inches = 25400
38 };
39
40 constexpr Length() : magnitude(0) {}
41 constexpr Length(Length const&) = default;
42 constexpr Length(float mag, Units units) : magnitude(mag * units) {}
43 Length& operator=(Length const&) = default;
44
45 constexpr float as(Units units) const
46 {
47 return static_cast<float>(magnitude) / units;
48 }
49
50 constexpr float as_pixels(float dpi) const
51 {
52 return as(inches) * dpi;
53 }
54
55 constexpr bool operator==(Length const& rhs) const
56 {
57 return magnitude == rhs.magnitude;
58 }
59
60 constexpr bool operator!=(Length const& rhs) const
61 {
62 return magnitude != rhs.magnitude;
63 }
64
65private:
66 float magnitude;
67};
68
69inline constexpr Length operator"" _mm(long double mag)
70{
71 return Length(mag, Length::millimetres);
72}
73
74inline constexpr Length operator"" _mm(unsigned long long mag)
75{
76 return Length(mag, Length::millimetres);
77}
78
79inline constexpr Length operator"" _cm(long double mag)
80{
81 return Length(mag, Length::centimetres);
82}
83
84inline constexpr Length operator"" _cm(unsigned long long mag)
85{
86 return Length(mag, Length::centimetres);
87}
88
89inline constexpr Length operator"" _in(long double mag)
90{
91 return Length(mag, Length::inches);
92}
93
94inline constexpr Length operator"" _in(unsigned long long mag)
95{
96 return Length(mag, Length::inches);
97}
98
99} // namespace geometry
100} // namespace mir
101
102#endif // MIR_GEOMETRY_LENGTH_H_
Length represents a physical length in the real world. The number of pixels this equates to can then ...
Definition: length.h:30
Length & operator=(Length const &)=default
constexpr Length()
Definition: length.h:40
constexpr bool operator==(Length const &rhs) const
Definition: length.h:55
Units
Definition: length.h:33
@ inches
Definition: length.h:37
@ centimetres
Definition: length.h:36
@ micrometres
Definition: length.h:34
@ millimetres
Definition: length.h:35
constexpr bool operator!=(Length const &rhs) const
Definition: length.h:60
constexpr Length(float mag, Units units)
Definition: length.h:42
constexpr float as_pixels(float dpi) const
Definition: length.h:50
constexpr Length(Length const &)=default
constexpr float as(Units units) const
Definition: length.h:45
Definition: splash_session.h:24

Copyright © 2012-2022 Canonical Ltd.
Generated on Sun Oct 9 06:13:38 UTC 2022
This documentation is licensed under the GPL version 2 or 3.