Mir
dimensions.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, 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: 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 {
35 template<typename Tag>
37 {
38 public:
39  typedef int ValueType;
40 
41  constexpr IntWrapper() : value(0) {}
42  constexpr IntWrapper(IntWrapper const& that) = default;
43  IntWrapper& operator=(IntWrapper const& that) = default;
44 
45  template<typename AnyInteger>
46  explicit constexpr IntWrapper(AnyInteger value) : value(static_cast<ValueType>(value)) {}
47 
48  constexpr uint32_t as_uint32_t() const // TODO: Deprecate this later
49  {
50  return (uint32_t)value;
51  }
52 
53  constexpr int as_int() const
54  {
55  return value;
56  }
57 
58 private:
59  ValueType value;
60 };
61 
62 template<typename Tag>
63 std::ostream& operator<<(std::ostream& out, IntWrapper<Tag> const& value)
64 {
65  out << value.as_int();
66  return out;
67 }
68 
69 template<typename Tag>
70 inline constexpr bool operator == (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
71 {
72  return lhs.as_int() == rhs.as_int();
73 }
74 
75 template<typename Tag>
76 inline constexpr bool operator != (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
77 {
78  return lhs.as_int() != rhs.as_int();
79 }
80 
81 template<typename Tag>
82 inline constexpr bool operator <= (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
83 {
84  return lhs.as_int() <= rhs.as_int();
85 }
86 
87 template<typename Tag>
88 inline constexpr bool operator >= (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
89 {
90  return lhs.as_int() >= rhs.as_int();
91 }
92 
93 template<typename Tag>
94 inline constexpr bool operator < (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
95 {
96  return lhs.as_int() < rhs.as_int();
97 }
98 
99 template<typename Tag>
100 inline constexpr bool operator > (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
101 {
102  return lhs.as_int() > rhs.as_int();
103 }
104 } // namespace detail
105 
108 // Just to be clear, mir::geometry::Stride is the stride of the buffer in bytes
110 
115 
116 // Adding deltas is fine
117 inline constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() + rhs.as_int()); }
118 inline constexpr DeltaY operator+(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() + rhs.as_int()); }
119 inline constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
120 inline constexpr DeltaY operator-(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
121 inline DeltaX& operator+=(DeltaX& lhs, DeltaX rhs) { return lhs = lhs + rhs; }
122 inline DeltaY& operator+=(DeltaY& lhs, DeltaY rhs) { return lhs = lhs + rhs; }
123 inline DeltaX& operator-=(DeltaX& lhs, DeltaX rhs) { return lhs = lhs - rhs; }
124 inline DeltaY& operator-=(DeltaY& lhs, DeltaY rhs) { return lhs = lhs - rhs; }
125 
126 // Adding deltas to co-ordinates is fine
127 inline constexpr X operator+(X lhs, DeltaX rhs) { return X(lhs.as_int() + rhs.as_int()); }
128 inline constexpr Y operator+(Y lhs, DeltaY rhs) { return Y(lhs.as_int() + rhs.as_int()); }
129 inline constexpr X operator-(X lhs, DeltaX rhs) { return X(lhs.as_int() - rhs.as_int()); }
130 inline constexpr Y operator-(Y lhs, DeltaY rhs) { return Y(lhs.as_int() - rhs.as_int()); }
131 inline X& operator+=(X& lhs, DeltaX rhs) { return lhs = lhs + rhs; }
132 inline Y& operator+=(Y& lhs, DeltaY rhs) { return lhs = lhs + rhs; }
133 inline X& operator-=(X& lhs, DeltaX rhs) { return lhs = lhs - rhs; }
134 inline Y& operator-=(Y& lhs, DeltaY rhs) { return lhs = lhs - rhs; }
135 
136 // Adding deltas to Width and Height is fine
137 inline constexpr Width operator+(Width lhs, DeltaX rhs) { return Width(lhs.as_int() + rhs.as_int()); }
138 inline constexpr Height operator+(Height lhs, DeltaY rhs) { return Height(lhs.as_int() + rhs.as_int()); }
139 inline constexpr Width operator-(Width lhs, DeltaX rhs) { return Width(lhs.as_int() - rhs.as_int()); }
140 inline constexpr Height operator-(Height lhs, DeltaY rhs) { return Height(lhs.as_int() - rhs.as_int()); }
141 inline Width& operator+=(Width& lhs, DeltaX rhs) { return lhs = lhs + rhs; }
142 inline Height& operator+=(Height& lhs, DeltaY rhs) { return lhs = lhs + rhs; }
143 inline Width& operator-=(Width& lhs, DeltaX rhs) { return lhs = lhs - rhs; }
144 inline Height& operator-=(Height& lhs, DeltaY rhs) { return lhs = lhs - rhs; }
145 
146 // Subtracting coordinates is fine
147 inline constexpr DeltaX operator-(X lhs, X rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
148 inline constexpr DeltaY operator-(Y lhs, Y rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
149 
150 //Subtracting Width and Height is fine
151 inline constexpr DeltaX operator-(Width lhs, Width rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
152 inline constexpr DeltaY operator-(Height lhs, Height rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
153 
154 // Multiplying by a scalar value is fine
155 template<typename Scalar>
156 inline constexpr Width operator*(Scalar scale, Width const& w) { return Width{scale*w.as_int()}; }
157 template<typename Scalar>
158 inline constexpr Height operator*(Scalar scale, Height const& h) { return Height{scale*h.as_int()}; }
159 template<typename Scalar>
160 inline constexpr DeltaX operator*(Scalar scale, DeltaX const& dx) { return DeltaX{scale*dx.as_int()}; }
161 template<typename Scalar>
162 inline constexpr DeltaY operator*(Scalar scale, DeltaY const& dy) { return DeltaY{scale*dy.as_int()}; }
163 template<typename Scalar>
164 inline constexpr Width operator*(Width const& w, Scalar scale) { return scale*w; }
165 template<typename Scalar>
166 inline constexpr Height operator*(Height const& h, Scalar scale) { return scale*h; }
167 template<typename Scalar>
168 inline constexpr DeltaX operator*(DeltaX const& dx, Scalar scale) { return scale*dx; }
169 template<typename Scalar>
170 inline constexpr DeltaY operator*(DeltaY const& dy, Scalar scale) { return scale*dy; }
171 
172 // Converting between types is fine, as long as they are along the same axis
173 inline constexpr Width as_width(DeltaX const& dx) { return Width{dx.as_int()}; }
174 inline constexpr Height as_height(DeltaY const& dy) { return Height{dy.as_int()}; }
175 inline constexpr X as_x(DeltaX const& dx) { return X{dx.as_int()}; }
176 inline constexpr Y as_y(DeltaY const& dy) { return Y{dy.as_int()}; }
177 inline constexpr DeltaX as_delta(X const& x) { return DeltaX{x.as_int()}; }
178 inline constexpr DeltaY as_delta(Y const& y) { return DeltaY{y.as_int()}; }
179 inline constexpr X as_x(Width const& w) { return X{w.as_int()}; }
180 inline constexpr Y as_y(Height const& h) { return Y{h.as_int()}; }
181 inline constexpr Width as_width(X const& x) { return Width{x.as_int()}; }
182 inline constexpr Height as_height(Y const& y) { return Height{y.as_int()}; }
183 inline constexpr DeltaX as_delta(Width const& w) { return DeltaX{w.as_int()}; }
184 inline constexpr DeltaY as_delta(Height const& h) { return DeltaY{h.as_int()}; }
185 
186 template<typename Target, typename Source>
187 inline constexpr Target dim_cast(Source s) { return Target(s.as_int()); }
188 }
189 }
190 
191 #endif /* MIR_GEOMETRY_DIMENSIONS_H_ */
constexpr bool operator<(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:94
Definition: splash_session.h:24
constexpr Y as_y(DeltaY const &dy)
Definition: dimensions.h:176
int ValueType
Definition: dimensions.h:39
DeltaX & operator-=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:123
DeltaX & operator+=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:121
detail::IntWrapper< struct DeltaXTag > DeltaX
Definition: dimensions.h:113
detail::IntWrapper< struct YTag > Y
Definition: dimensions.h:112
detail::IntWrapper< struct WidthTag > Width
Definition: dimensions.h:106
constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
constexpr Height as_height(DeltaY const &dy)
Definition: dimensions.h:174
constexpr Target dim_cast(Source s)
Definition: dimensions.h:187
constexpr int as_int() const
Definition: dimensions.h:53
constexpr X as_x(DeltaX const &dx)
Definition: dimensions.h:175
constexpr bool operator>(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:100
constexpr bool operator==(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:70
constexpr DeltaX as_delta(X const &x)
Definition: dimensions.h:177
constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
constexpr Width as_width(DeltaX const &dx)
Definition: dimensions.h:173
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:156
constexpr bool operator!=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:76
constexpr bool operator<=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:82
std::ostream & operator<<(std::ostream &out, IntWrapper< Tag > const &value)
Definition: dimensions.h:63
constexpr IntWrapper(AnyInteger value)
Definition: dimensions.h:46
constexpr IntWrapper()
Definition: dimensions.h:41
detail::IntWrapper< struct StrideTag > Stride
Definition: dimensions.h:109
IntWrapper & operator=(IntWrapper const &that)=default
constexpr uint32_t as_uint32_t() const
Definition: dimensions.h:48
detail::IntWrapper< struct XTag > X
Definition: dimensions.h:111
Definition: dimensions.h:36
constexpr bool operator>=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:88
detail::IntWrapper< struct HeightTag > Height
Definition: dimensions.h:107
detail::IntWrapper< struct DeltaYTag > DeltaY
Definition: dimensions.h:114

Copyright © 2012-2019 Canonical Ltd.
Generated on Wed Aug 28 00:46:47 UTC 2019
This documentation is licensed under the GPL version 2 or 3.