Mir
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 
110 // Just to be clear, mir::geometry::Stride is the stride of the buffer in bytes
112 
117 
118 // Adding deltas is fine
119 inline DeltaX operator+(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() + rhs.as_int()); }
120 inline DeltaY operator+(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() + rhs.as_int()); }
121 inline DeltaX operator-(DeltaX lhs, DeltaX rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
122 inline DeltaY operator-(DeltaY lhs, DeltaY rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
123 
124 // Adding deltas to co-ordinates is fine
125 inline X operator+(X lhs, DeltaX rhs) { return X(lhs.as_int() + rhs.as_int()); }
126 inline Y operator+(Y lhs, DeltaY rhs) { return Y(lhs.as_int() + rhs.as_int()); }
127 inline X operator-(X lhs, DeltaX rhs) { return X(lhs.as_int() - rhs.as_int()); }
128 inline Y operator-(Y lhs, DeltaY rhs) { return Y(lhs.as_int() - rhs.as_int()); }
129 
130 // Subtracting coordinates is fine
131 inline DeltaX operator-(X lhs, X rhs) { return DeltaX(lhs.as_int() - rhs.as_int()); }
132 inline DeltaY operator-(Y lhs, Y rhs) { return DeltaY(lhs.as_int() - rhs.as_int()); }
133 
134 // Multiplying by a scalar value is fine
135 template<typename Scalar>
136 inline Width operator*(Scalar scale, Width const& w) { return Width{scale*w.as_int()}; }
137 template<typename Scalar>
138 inline Height operator*(Scalar scale, Height const& h) { return Height{scale*h.as_int()}; }
139 template<typename Scalar>
140 inline DeltaX operator*(Scalar scale, DeltaX const& dx) { return DeltaX{scale*dx.as_int()}; }
141 template<typename Scalar>
142 inline DeltaY operator*(Scalar scale, DeltaY const& dy) { return DeltaY{scale*dy.as_int()}; }
143 template<typename Scalar>
144 inline Width operator*(Width const& w, Scalar scale) { return scale*w; }
145 template<typename Scalar>
146 inline Height operator*(Height const& h, Scalar scale) { return scale*h; }
147 template<typename Scalar>
148 inline DeltaX operator*(DeltaX const& dx, Scalar scale) { return scale*dx; }
149 template<typename Scalar>
150 inline DeltaY operator*(DeltaY const& dy, Scalar scale) { return scale*dy; }
151 
152 template<typename Target, typename Source>
153 inline Target dim_cast(Source s) { return Target(s.as_int()); }
154 }
155 }
156 
157 #endif /* MIR_GEOMETRY_DIMENSIONS_H_ */
All things Mir.
Definition: buffer_stream.h:37
bool operator==(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:72
Width operator*(Scalar scale, Width const &w)
Definition: dimensions.h:136
detail::IntWrapper< detail::dx > DeltaX
Definition: dimensions.h:115
float as_float() const
Definition: dimensions.h:55
DeltaX operator-(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:121
detail::IntWrapper< detail::height > Height
Definition: dimensions.h:109
int ValueType
Definition: dimensions.h:41
Definition: dimensions.h:35
detail::IntWrapper< detail::stride > Stride
Definition: dimensions.h:111
Definition: dimensions.h:35
DimensionTag
Definition: dimensions.h:35
Definition: dimensions.h:35
detail::IntWrapper< detail::width > Width
Definition: dimensions.h:108
uint32_t as_uint32_t() const
Definition: dimensions.h:47
bool operator>=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:90
Definition: dimensions.h:35
detail::IntWrapper< detail::x > X
Definition: dimensions.h:113
detail::IntWrapper< detail::dy > DeltaY
Definition: dimensions.h:116
bool operator>(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:102
Target dim_cast(Source s)
Definition: dimensions.h:153
Definition: dimensions.h:35
Definition: dimensions.h:35
bool operator!=(IntWrapper< Tag > const &lhs, IntWrapper< Tag > const &rhs)
Definition: dimensions.h:78
detail::IntWrapper< detail::y > Y
Definition: dimensions.h:114
Definition: dimensions.h:38
Definition: dimensions.h:35
DeltaX operator+(DeltaX lhs, DeltaX rhs)
Definition: dimensions.h:119
IntWrapper(AnyInteger value)
Definition: dimensions.h:45
int as_int() const
Definition: dimensions.h:51
IntWrapper()
Definition: dimensions.h:43

Copyright © 2012,2013 Canonical Ltd.
Generated on Tue Mar 24 16:15:19 UTC 2015