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 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 
22 namespace mir
23 {
24 namespace geometry
25 {
26 
29 class Length
30 {
31 public:
32  enum Units
33  {
35  millimetres = 1000,
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 
65 private:
66  float magnitude;
67 };
68 
69 inline constexpr Length operator"" _mm(long double mag)
70 {
71  return Length(mag, Length::millimetres);
72 }
73 
74 inline constexpr Length operator"" _mm(unsigned long long mag)
75 {
76  return Length(mag, Length::millimetres);
77 }
78 
79 inline constexpr Length operator"" _cm(long double mag)
80 {
81  return Length(mag, Length::centimetres);
82 }
83 
84 inline constexpr Length operator"" _cm(unsigned long long mag)
85 {
86  return Length(mag, Length::centimetres);
87 }
88 
89 inline constexpr Length operator"" _in(long double mag)
90 {
91  return Length(mag, Length::inches);
92 }
93 
94 inline 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_
Definition: as_render_target.h:27
Length & operator=(Length const &)=default
constexpr Length(float mag, Units units)
Definition: length.h:42
Definition: length.h:37
Units
Definition: length.h:32
Definition: length.h:35
Definition: length.h:34
Length represents a physical length in the real world. The number of pixels this equates to can then ...
Definition: length.h:29
constexpr bool operator==(Length const &rhs) const
Definition: length.h:55
constexpr float as_pixels(float dpi) const
Definition: length.h:50
constexpr bool operator!=(Length const &rhs) const
Definition: length.h:60
Definition: length.h:36
constexpr float as(Units units) const
Definition: length.h:45
constexpr Length()
Definition: length.h:40

Copyright © 2012-2016 Canonical Ltd.
Generated on Tue Oct 31 05:57:28 UTC 2017