Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
length.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 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  Length() : magnitude(0) {}
41  Length(Length const&) = default;
42  Length& operator=(Length const&) = default;
43  Length(float mag, Units units) : magnitude(mag * units) {}
44 
45  float as(Units units) const
46  {
47  return static_cast<float>(magnitude) / units;
48  }
49 
50  float as_pixels(float dpi) const
51  {
52  return as(inches) * dpi;
53  }
54 
55  bool operator==(Length const& rhs) const
56  {
57  return magnitude == rhs.magnitude;
58  }
59 
60  bool operator!=(Length const& rhs) const
61  {
62  return magnitude != rhs.magnitude;
63  }
64 
65 private:
66  float magnitude;
67 };
68 
69 inline Length operator"" _mm(long double mag)
70 {
71  return Length(mag, Length::millimetres);
72 }
73 
74 inline Length operator"" _mm(unsigned long long mag)
75 {
76  return Length(mag, Length::millimetres);
77 }
78 
79 inline Length operator"" _cm(long double mag)
80 {
81  return Length(mag, Length::centimetres);
82 }
83 
84 inline Length operator"" _cm(unsigned long long mag)
85 {
86  return Length(mag, Length::centimetres);
87 }
88 
89 inline Length operator"" _in(long double mag)
90 {
91  return Length(mag, Length::inches);
92 }
93 
94 inline 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 & operator=(Length const &)=default
Definition: length.h:37
Units
Definition: length.h:32
Definition: length.h:35
float as(Units units) const
Definition: length.h:45
Definition: length.h:34
Length(float mag, Units units)
Definition: length.h:43
Length represents a physical length in the real world. The number of pixels this equates to can then ...
Definition: length.h:29
bool operator==(Length const &rhs) const
Definition: length.h:55
Length()
Definition: length.h:40
float as_pixels(float dpi) const
Definition: length.h:50
bool operator!=(Length const &rhs) const
Definition: length.h:60
Definition: length.h:36

Copyright © 2012,2013 Canonical Ltd.
Generated on Fri Apr 11 21:14:53 UTC 2014