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 
122 // Adding deltas to co-ordinates is fine
123 inline constexpr X operator+(X lhs, DeltaX rhs) { return X(lhs.as_int() + rhs.as_int()); }
124 inline constexpr Y operator+(Y lhs, DeltaY rhs) { return Y(lhs.as_int() + rhs.as_int()); }
125 inline constexpr X operator-(X lhs, DeltaX rhs) { return X(lhs.as_int() - rhs.as_int()); }
126 inline constexpr Y operator-(Y lhs, DeltaY rhs) { return Y(lhs.as_int() - rhs.as_int()); }
127 inline X& operator+=(X& lhs, DeltaX rhs) { return lhs = X(lhs.as_int() + rhs.as_int()); }
128 inline Y& operator+=(Y& lhs, DeltaY rhs) { return lhs = Y(lhs.as_int() + rhs.as_int()); }
129 inline X& operator-=(X& lhs, DeltaX rhs) { return lhs = X(lhs.as_int() - rhs.as_int()); }
130 inline Y& operator-=(Y& lhs, DeltaY rhs) { return lhs = Y(lhs.as_int() - rhs.as_int()); }
131 
132 // Adding deltas to Width and Height is fine
133 inline constexpr Width operator+(Width lhs, DeltaX rhs) { return Width(lhs.as_int() + rhs.as_int()); }
134 inline constexpr Height operator+(Height lhs, DeltaY rhs) { return Height(lhs.as_int() + rhs.as_int()); }
135 inline constexpr Width operator-(Width lhs, DeltaX rhs) { return Width(lhs.as_int() - rhs.as_int()); }
136 inline constexpr Height operator-(Height lhs, DeltaY rhs) { return Height(lhs.as_int() - rhs.as_int()); }
137 
138 // Subtracting coordinates is fine
139 inline constexpr DeltaX operator-(X lhs, X rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
140 inline constexpr DeltaY operator-(Y lhs, Y rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
141 
142 //Subtracting Width and Height is fine
143 inline constexpr DeltaX operator-(Width lhs, Width rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
144 inline constexpr DeltaY operator-(Height lhs, Height rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
145 
146 // Multiplying by a scalar value is fine
147 template<typename Scalar>
148 inline constexpr Width operator*(Scalar scale, Width const& w) { return Width{scale*w.as_int()}; }
149 template<typename Scalar>
150 inline constexpr Height operator*(Scalar scale, Height const& h) { return Height{scale*h.as_int()}; }
151 template<typename Scalar>
152 inline constexpr DeltaX operator*(Scalar scale, DeltaX const& dx) { return DeltaX{scale*dx.as_int()}; }
153 template<typename Scalar>
154 inline constexpr DeltaY operator*(Scalar scale, DeltaY const& dy) { return DeltaY{scale*dy.as_int()}; }
155 template<typename Scalar>
156 inline constexpr Width operator*(Width const& w, Scalar scale) { return scale*w; }
157 template<typename Scalar>
158 inline constexpr Height operator*(Height const& h, Scalar scale) { return scale*h; }
159 template<typename Scalar>
160 inline constexpr DeltaX operator*(DeltaX const& dx, Scalar scale) { return scale*dx; }
161 template<typename Scalar>
162 inline constexpr DeltaY operator*(DeltaY const& dy, Scalar scale) { return scale*dy; }
163 
164 template<typename Target, typename Source>
165 inline constexpr Target dim_cast(Source s) { return Target(s.as_int()); }
166 }
167 }
168 
169 #endif /* MIR_GEOMETRY_DIMENSIONS_H_ */
AutoUnblockThread is a helper thread class that can gracefully shutdown at destruction time...
Definition: sw_splash.h:26
int ValueType
Definition: dimensions.h:39
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 Target dim_cast(Source s)
Definition: dimensions.h:165
constexpr int as_int() const
Definition: dimensions.h:53
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 operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:117
constexpr Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:148
constexpr bool operator!=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:76
constexpr IntWrapper(AnyInteger value)
Definition: dimensions.h:46
constexpr IntWrapper()
Definition: dimensions.h:41
X & operator-=(X &lhs, DeltaX rhs)
Definition: dimensions.h:129
detail::IntWrapper< struct StrideTag > Stride
Definition: dimensions.h:109
X & operator+=(X &lhs, DeltaX rhs)
Definition: dimensions.h:127
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-2017 Canonical Ltd.
Generated on Mon Oct 30 19:44:55 UTC 2017