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 // Adding Widths and Heights is fine
147 inline constexpr Width operator+(Width lhs, Width rhs) { return Width(lhs.as_int() + rhs.as_int()); }
148 inline constexpr Height operator+(Height lhs, Height rhs) { return Height(lhs.as_int() + rhs.as_int()); }
149 inline Width& operator+=(Width& lhs, Width rhs) { return lhs = lhs + rhs; }
150 inline Height& operator+=(Height& lhs, Height rhs) { return lhs = lhs + rhs; }
151 
152 // Subtracting coordinates is fine
153 inline constexpr DeltaX operator-(X lhs, X rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
154 inline constexpr DeltaY operator-(Y lhs, Y rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
155 
156 //Subtracting Width and Height is fine
157 inline constexpr DeltaX operator-(Width lhs, Width rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
158 inline constexpr DeltaY operator-(Height lhs, Height rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
159 
160 // Multiplying by a scalar value is fine
161 template<typename Scalar>
162 inline constexpr Width operator*(Scalar scale, Width const& w) { return Width{scale*w.as_int()}; }
163 template<typename Scalar>
164 inline constexpr Height operator*(Scalar scale, Height const& h) { return Height{scale*h.as_int()}; }
165 template<typename Scalar>
166 inline constexpr DeltaX operator*(Scalar scale, DeltaX const& dx) { return DeltaX{scale*dx.as_int()}; }
167 template<typename Scalar>
168 inline constexpr DeltaY operator*(Scalar scale, DeltaY const& dy) { return DeltaY{scale*dy.as_int()}; }
169 template<typename Scalar>
170 inline constexpr Width operator*(Width const& w, Scalar scale) { return scale*w; }
171 template<typename Scalar>
172 inline constexpr Height operator*(Height const& h, Scalar scale) { return scale*h; }
173 template<typename Scalar>
174 inline constexpr DeltaX operator*(DeltaX const& dx, Scalar scale) { return scale*dx; }
175 template<typename Scalar>
176 inline constexpr DeltaY operator*(DeltaY const& dy, Scalar scale) { return scale*dy; }
177 
178 // Dividing by a scaler value is fine
179 template<typename Scalar>
180 inline constexpr Width operator/(Width const& w, Scalar scale) { return Width{w.as_int() / scale}; }
181 template<typename Scalar>
182 inline constexpr Height operator/(Height const& h, Scalar scale) { return Height{h.as_int() / scale}; }
183 template<typename Scalar>
184 inline constexpr DeltaX operator/(DeltaX const& dx, Scalar scale) { return DeltaX{dx.as_int() / scale}; }
185 template<typename Scalar>
186 inline constexpr DeltaY operator/(DeltaY const& dy, Scalar scale) { return DeltaY{dy.as_int() / scale}; }
187 
188 // Converting between types is fine, as long as they are along the same axis
189 inline constexpr Width as_width(DeltaX const& dx) { return Width{dx.as_int()}; }
190 inline constexpr Height as_height(DeltaY const& dy) { return Height{dy.as_int()}; }
191 inline constexpr X as_x(DeltaX const& dx) { return X{dx.as_int()}; }
192 inline constexpr Y as_y(DeltaY const& dy) { return Y{dy.as_int()}; }
193 inline constexpr DeltaX as_delta(X const& x) { return DeltaX{x.as_int()}; }
194 inline constexpr DeltaY as_delta(Y const& y) { return DeltaY{y.as_int()}; }
195 inline constexpr X as_x(Width const& w) { return X{w.as_int()}; }
196 inline constexpr Y as_y(Height const& h) { return Y{h.as_int()}; }
197 inline constexpr Width as_width(X const& x) { return Width{x.as_int()}; }
198 inline constexpr Height as_height(Y const& y) { return Height{y.as_int()}; }
199 inline constexpr DeltaX as_delta(Width const& w) { return DeltaX{w.as_int()}; }
200 inline constexpr DeltaY as_delta(Height const& h) { return DeltaY{h.as_int()}; }
201 
202 template<typename Target, typename Source>
203 inline constexpr Target dim_cast(Source s) { return Target(s.as_int()); }
204 }
205 }
206 
207 #endif /* MIR_GEOMETRY_DIMENSIONS_H_ */
mir::geometry::operator+=
DeltaX & operator+=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:121
mir::geometry::dim_cast
constexpr Target dim_cast(Source s)
Definition: dimensions.h:203
mir::geometry::Width
detail::IntWrapper< struct WidthTag > Width
Definition: dimensions.h:106
mir::geometry::as_width
constexpr Width as_width(DeltaX const &dx)
Definition: dimensions.h:189
mir::geometry::operator-
constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
mir::geometry::detail::IntWrapper
Definition: dimensions.h:36
mir::geometry::detail::operator<
constexpr bool operator<(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:94
mir::geometry::detail::IntWrapper::IntWrapper
constexpr IntWrapper(AnyInteger value)
Definition: dimensions.h:46
mir::geometry::operator-=
DeltaX & operator-=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:123
mir::geometry::detail::IntWrapper::IntWrapper
constexpr IntWrapper()
Definition: dimensions.h:41
mir::geometry::Y
detail::IntWrapper< struct YTag > Y
Definition: dimensions.h:112
mir::geometry::as_x
constexpr X as_x(DeltaX const &dx)
Definition: dimensions.h:191
mir::geometry::operator+
constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
mir::geometry::detail::operator==
constexpr bool operator==(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:70
mir::geometry::DeltaX
detail::IntWrapper< struct DeltaXTag > DeltaX
Definition: dimensions.h:113
mir::geometry::as_delta
constexpr DeltaX as_delta(X const &x)
Definition: dimensions.h:193
mir::geometry::Stride
detail::IntWrapper< struct StrideTag > Stride
Definition: dimensions.h:109
mir
Definition: splash_session.h:24
mir::geometry::operator/
constexpr Width operator/(Width const &w, Scalar scale)
Definition: dimensions.h:180
mir::geometry::as_y
constexpr Y as_y(DeltaY const &dy)
Definition: dimensions.h:192
mir::geometry::detail::operator>
constexpr bool operator>(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:100
mir::geometry::X
detail::IntWrapper< struct XTag > X
Definition: dimensions.h:111
mir::geometry::Height
detail::IntWrapper< struct HeightTag > Height
Definition: dimensions.h:107
mir::geometry::detail::IntWrapper::as_uint32_t
constexpr uint32_t as_uint32_t() const
Definition: dimensions.h:48
mir::geometry::operator*
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:162
mir::geometry::detail::operator<<
std::ostream & operator<<(std::ostream &out, IntWrapper< Tag > const &value)
Definition: dimensions.h:63
mir::geometry::as_height
constexpr Height as_height(DeltaY const &dy)
Definition: dimensions.h:190
mir::geometry::DeltaY
detail::IntWrapper< struct DeltaYTag > DeltaY
Definition: dimensions.h:114
mir::geometry::detail::operator!=
constexpr bool operator!=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:76
mir::geometry::detail::operator<=
constexpr bool operator<=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:82
mir::geometry::detail::IntWrapper::operator=
IntWrapper & operator=(IntWrapper const &that)=default
mir::geometry::detail::operator>=
constexpr bool operator>=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:88
mir::geometry::detail::IntWrapper::as_int
constexpr int as_int() const
Definition: dimensions.h:53
mir::geometry::detail::IntWrapper::ValueType
int ValueType
Definition: dimensions.h:39

Copyright © 2012-2022 Canonical Ltd.
Generated on Mon Jun 20 18:21:25 UTC 2022
This documentation is licensed under the GPL version 2 or 3.