Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dimensions.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012 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: Alan Griffiths <alan@octopull.co.uk>
17  */
18 
19 #ifndef MIR_GEOMETRY_DIMENSIONS_H_
20 #define MIR_GEOMETRY_DIMENSIONS_H_
21 
22 #include <cstdint>
23 #include <iosfwd>
24 
25 namespace mir
26 {
27 
30 namespace geometry
31 {
32 
33 namespace detail
34 {
36 
37 template<DimensionTag Tag>
39 {
40 public:
41  typedef int ValueType;
42 
43  IntWrapper() : value(0) {}
44  template<typename AnyInteger>
45  explicit IntWrapper(AnyInteger value) : value(static_cast<ValueType>(value)) {}
46 
47  uint32_t as_uint32_t() const // TODO: Deprecate this later
48  {
49  return (uint32_t)value;
50  }
51  int as_int() const
52  {
53  return value;
54  }
55  float as_float() const
56  {
57  return value;
58  }
59 
60 private:
61  ValueType value;
62 };
63 
64 template<DimensionTag Tag>
65 std::ostream& operator<<(std::ostream& out, IntWrapper<Tag> const& value)
66 {
67  out << value.as_int();
68  return out;
69 }
70 
71 template<DimensionTag Tag>
72 inline bool operator == (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
73 {
74  return lhs.as_int() == rhs.as_int();
75 }
76 
77 template<DimensionTag Tag>
78 inline bool operator != (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
79 {
80  return lhs.as_int() != rhs.as_int();
81 }
82 
83 template<DimensionTag Tag>
84 inline bool operator <= (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
85 {
86  return lhs.as_int() <= rhs.as_int();
87 }
88 
89 template<DimensionTag Tag>
90 inline bool operator >= (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
91 {
92  return lhs.as_int() >= rhs.as_int();
93 }
94 
95 template<DimensionTag Tag>
96 inline bool operator < (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
97 {
98  return lhs.as_int() < rhs.as_int();
99 }
100 
101 template<DimensionTag Tag>
102 inline bool operator > (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
103 {
104  return lhs.as_int() > rhs.as_int();
105 }
106 } // namespace detail
107 
111 
116 
117 // Adding deltas is fine
118 inline DeltaX operator+(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() + rhs.as_int()); }
119 inline DeltaY operator+(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() + rhs.as_int()); }
120 inline DeltaX operator-(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
121 inline DeltaY operator-(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
122 
123 // Adding deltas to co-ordinates is fine
124 inline X operator+(X lhs, DeltaX rhs) { return X(lhs.as_int() + rhs.as_int()); }
125 inline Y operator+(Y lhs, DeltaY rhs) { return Y(lhs.as_int() + rhs.as_int()); }
126 inline X operator-(X lhs, DeltaX rhs) { return X(lhs.as_int() - rhs.as_int()); }
127 inline Y operator-(Y lhs, DeltaY rhs) { return Y(lhs.as_int() - rhs.as_int()); }
128 
129 // Subtracting coordinates is fine
130 inline DeltaX operator-(X lhs, X rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
131 inline DeltaY operator-(Y lhs, Y rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
132 
133 template<typename Target, typename Source>
134 inline Target dim_cast(Source s) { return Target(s.as_int()); }
135 }
136 }
137 
138 #endif /* MIR_GEOMETRY_DIMENSIONS_H_ */

Copyright © 2012,2013 Canonical Ltd.
Generated on Wed Oct 30 18:52:19 UTC 2013