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
25namespace mir
26{
27
30namespace geometry
31{
32
33namespace detail
34{
35template<typename Tag>
37{
38public:
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
58private:
59 ValueType value;
60};
61
62template<typename Tag>
63std::ostream& operator<<(std::ostream& out, IntWrapper<Tag> const& value)
64{
65 out << value.as_int();
66 return out;
67}
68
69template<typename Tag>
70inline constexpr bool operator == (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
71{
72 return lhs.as_int() == rhs.as_int();
73}
74
75template<typename Tag>
76inline constexpr bool operator != (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
77{
78 return lhs.as_int() != rhs.as_int();
79}
80
81template<typename Tag>
82inline constexpr bool operator <= (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
83{
84 return lhs.as_int() <= rhs.as_int();
85}
86
87template<typename Tag>
88inline constexpr bool operator >= (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
89{
90 return lhs.as_int() >= rhs.as_int();
91}
92
93template<typename Tag>
94inline constexpr bool operator < (IntWrapper<Tag> const& lhs, IntWrapper<Tag> const& rhs)
95{
96 return lhs.as_int() < rhs.as_int();
97}
98
99template<typename Tag>
100inline 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
117inline constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() + rhs.as_int()); }
118inline constexpr DeltaY operator+(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() + rhs.as_int()); }
119inline constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
120inline constexpr DeltaY operator-(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
121inline DeltaX& operator+=(DeltaX& lhs, DeltaX rhs) { return lhs = lhs + rhs; }
122inline DeltaY& operator+=(DeltaY& lhs, DeltaY rhs) { return lhs = lhs + rhs; }
123inline DeltaX& operator-=(DeltaX& lhs, DeltaX rhs) { return lhs = lhs - rhs; }
124inline DeltaY& operator-=(DeltaY& lhs, DeltaY rhs) { return lhs = lhs - rhs; }
125
126// Adding deltas to co-ordinates is fine
127inline constexpr X operator+(X lhs, DeltaX rhs) { return X(lhs.as_int() + rhs.as_int()); }
128inline constexpr Y operator+(Y lhs, DeltaY rhs) { return Y(lhs.as_int() + rhs.as_int()); }
129inline constexpr X operator-(X lhs, DeltaX rhs) { return X(lhs.as_int() - rhs.as_int()); }
130inline constexpr Y operator-(Y lhs, DeltaY rhs) { return Y(lhs.as_int() - rhs.as_int()); }
131inline X& operator+=(X& lhs, DeltaX rhs) { return lhs = lhs + rhs; }
132inline Y& operator+=(Y& lhs, DeltaY rhs) { return lhs = lhs + rhs; }
133inline X& operator-=(X& lhs, DeltaX rhs) { return lhs = lhs - rhs; }
134inline Y& operator-=(Y& lhs, DeltaY rhs) { return lhs = lhs - rhs; }
135
136// Adding deltas to Width and Height is fine
137inline constexpr Width operator+(Width lhs, DeltaX rhs) { return Width(lhs.as_int() + rhs.as_int()); }
138inline constexpr Height operator+(Height lhs, DeltaY rhs) { return Height(lhs.as_int() + rhs.as_int()); }
139inline constexpr Width operator-(Width lhs, DeltaX rhs) { return Width(lhs.as_int() - rhs.as_int()); }
140inline constexpr Height operator-(Height lhs, DeltaY rhs) { return Height(lhs.as_int() - rhs.as_int()); }
141inline Width& operator+=(Width& lhs, DeltaX rhs) { return lhs = lhs + rhs; }
142inline Height& operator+=(Height& lhs, DeltaY rhs) { return lhs = lhs + rhs; }
143inline Width& operator-=(Width& lhs, DeltaX rhs) { return lhs = lhs - rhs; }
144inline Height& operator-=(Height& lhs, DeltaY rhs) { return lhs = lhs - rhs; }
145
146// Adding Widths and Heights is fine
147inline constexpr Width operator+(Width lhs, Width rhs) { return Width(lhs.as_int() + rhs.as_int()); }
148inline constexpr Height operator+(Height lhs, Height rhs) { return Height(lhs.as_int() + rhs.as_int()); }
149inline Width& operator+=(Width& lhs, Width rhs) { return lhs = lhs + rhs; }
150inline Height& operator+=(Height& lhs, Height rhs) { return lhs = lhs + rhs; }
151
152// Subtracting coordinates is fine
153inline constexpr DeltaX operator-(X lhs, X rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
154inline constexpr DeltaY operator-(Y lhs, Y rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
155
156//Subtracting Width and Height is fine
157inline constexpr DeltaX operator-(Width lhs, Width rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
158inline constexpr DeltaY operator-(Height lhs, Height rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
159
160// Multiplying by a scalar value is fine
161template<typename Scalar>
162inline constexpr Width operator*(Scalar scale, Width const& w) { return Width{scale*w.as_int()}; }
163template<typename Scalar>
164inline constexpr Height operator*(Scalar scale, Height const& h) { return Height{scale*h.as_int()}; }
165template<typename Scalar>
166inline constexpr DeltaX operator*(Scalar scale, DeltaX const& dx) { return DeltaX{scale*dx.as_int()}; }
167template<typename Scalar>
168inline constexpr DeltaY operator*(Scalar scale, DeltaY const& dy) { return DeltaY{scale*dy.as_int()}; }
169template<typename Scalar>
170inline constexpr Width operator*(Width const& w, Scalar scale) { return scale*w; }
171template<typename Scalar>
172inline constexpr Height operator*(Height const& h, Scalar scale) { return scale*h; }
173template<typename Scalar>
174inline constexpr DeltaX operator*(DeltaX const& dx, Scalar scale) { return scale*dx; }
175template<typename Scalar>
176inline constexpr DeltaY operator*(DeltaY const& dy, Scalar scale) { return scale*dy; }
177
178// Dividing by a scaler value is fine
179template<typename Scalar>
180inline constexpr Width operator/(Width const& w, Scalar scale) { return Width{w.as_int() / scale}; }
181template<typename Scalar>
182inline constexpr Height operator/(Height const& h, Scalar scale) { return Height{h.as_int() / scale}; }
183template<typename Scalar>
184inline constexpr DeltaX operator/(DeltaX const& dx, Scalar scale) { return DeltaX{dx.as_int() / scale}; }
185template<typename Scalar>
186inline 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
189inline constexpr Width as_width(DeltaX const& dx) { return Width{dx.as_int()}; }
190inline constexpr Height as_height(DeltaY const& dy) { return Height{dy.as_int()}; }
191inline constexpr X as_x(DeltaX const& dx) { return X{dx.as_int()}; }
192inline constexpr Y as_y(DeltaY const& dy) { return Y{dy.as_int()}; }
193inline constexpr DeltaX as_delta(X const& x) { return DeltaX{x.as_int()}; }
194inline constexpr DeltaY as_delta(Y const& y) { return DeltaY{y.as_int()}; }
195inline constexpr X as_x(Width const& w) { return X{w.as_int()}; }
196inline constexpr Y as_y(Height const& h) { return Y{h.as_int()}; }
197inline constexpr Width as_width(X const& x) { return Width{x.as_int()}; }
198inline constexpr Height as_height(Y const& y) { return Height{y.as_int()}; }
199inline constexpr DeltaX as_delta(Width const& w) { return DeltaX{w.as_int()}; }
200inline constexpr DeltaY as_delta(Height const& h) { return DeltaY{h.as_int()}; }
201
202template<typename Target, typename Source>
203inline constexpr Target dim_cast(Source s) { return Target(s.as_int()); }
204}
205}
206
207#endif /* MIR_GEOMETRY_DIMENSIONS_H_ */
Definition: dimensions.h:37
constexpr IntWrapper(IntWrapper const &that)=default
constexpr IntWrapper()
Definition: dimensions.h:41
constexpr IntWrapper(AnyInteger value)
Definition: dimensions.h:46
IntWrapper & operator=(IntWrapper const &that)=default
constexpr uint32_t as_uint32_t() const
Definition: dimensions.h:48
int ValueType
Definition: dimensions.h:39
constexpr int as_int() const
Definition: dimensions.h:53
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:88
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 bool operator<(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:94
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
detail::IntWrapper< struct DeltaXTag > DeltaX
Definition: dimensions.h:113
constexpr DeltaX as_delta(X const &x)
Definition: dimensions.h:193
detail::IntWrapper< struct HeightTag > Height
Definition: dimensions.h:107
detail::IntWrapper< struct WidthTag > Width
Definition: dimensions.h:106
detail::IntWrapper< struct StrideTag > Stride
Definition: dimensions.h:109
constexpr Height as_height(DeltaY const &dy)
Definition: dimensions.h:190
DeltaX & operator-=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:123
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:162
constexpr Width operator/(Width const &w, Scalar scale)
Definition: dimensions.h:180
DeltaX & operator+=(DeltaX &lhs, DeltaX rhs)
Definition: dimensions.h:121
constexpr X as_x(DeltaX const &dx)
Definition: dimensions.h:191
constexpr DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
constexpr DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
constexpr Y as_y(DeltaY const &dy)
Definition: dimensions.h:192
constexpr Target dim_cast(Source s)
Definition: dimensions.h:203
detail::IntWrapper< struct YTag > Y
Definition: dimensions.h:112
constexpr Width as_width(DeltaX const &dx)
Definition: dimensions.h:189
detail::IntWrapper< struct DeltaYTag > DeltaY
Definition: dimensions.h:114
detail::IntWrapper< struct XTag > X
Definition: dimensions.h:111
Definition: splash_session.h:24

Copyright © 2012-2022 Canonical Ltd.
Generated on Wed Dec 28 21:01:08 UTC 2022
This documentation is licensed under the GPL version 2 or 3.