Library of Assembled Shared Sources
xy.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::prim::XY
46 * @brief cyclic iterator over xy indices
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_XY_H
51#define LASS_GUARDIAN_OF_INCLUSION_PRIM_XY_H
52
53#include "prim_common.h"
54#include "../num/modulo.h"
55
56namespace lass
57{
58namespace prim
59{
60
61class XY
62{
63public:
64
65 static constexpr size_t dimension = 2;
66
67 /** intializes iterator to @a x axis.
68 */
69 constexpr XY(): value_(0) {}
70
71 /** initializes iterator to an axis by character: 'x' or 'y'.
72 */
73 constexpr XY(char axis)
74 {
75 switch (axis)
76 {
77 case 0:
78 case 'x':
79 case 'X':
80 value_ = 0;
81 break;
82
83 case 1:
84 case 'y':
85 case 'Y':
86 value_ = 1;
87 break;
88
89 default:
90 throw util::Exception("Invalid parameter axis. Try 'x', 'X', 'y' or 'Y'.", LASS_PRETTY_FUNCTION);
91 }
92 }
93
94 /** initializes iterator to an axis by number.
95 * ..., -1 == @a y, 0 == @a x, 1 == @a y, 2 == @a x, ...
96 */
97 explicit constexpr XY(int value): value_(value) {}
98
99 /** initializes iterator to an axis by character: "x" or "y".
100 */
101 explicit XY(const std::string& axis)
102 {
103 if (axis.length() != 1)
104 {
105 LASS_THROW("Invalid parameter axis '" << axis << "'. It must be a single character.");
106 }
107 *this = XY(axis[0]);
108 }
109
110 constexpr XY(const XY& other) = default;
111 constexpr XY& operator=(const XY& other) = default;
112
113 /** return axis by character: 'x' or 'y'.
114 */
115 constexpr char axis() const
116 {
117 LASS_ASSERT(value_ >= 0 && value_ < static_cast<int>(dimension));
118 constexpr char axes[] = { 'x', 'y' };
119 return axes[value_];
120 }
121
122 constexpr operator int() const { return value_; } /**< convert axis to integer. 0 == @a x, 1 == @a y */
123 constexpr operator size_t() const { return static_cast<size_t>(value_); } /**< convert axis to integer. 0 == @a x, 1 == @a y */
124
125 constexpr XY& operator++()
126 {
127 ++value_;
128 return *this;
129 }
130 constexpr XY& operator--()
131 {
132 --value_;
133 return *this;
134 }
135
136 constexpr XY operator++(int)
137 {
138 const XY result(*this);
139 ++*this;
140 return result;
141 }
142 constexpr XY operator--(int)
143 {
144 const XY result(*this);
145 --*this;
146 return result;
147 }
148
149 constexpr XY& operator+=(int offset)
150 {
151 value_ += offset;
152 return *this;
153 }
154 constexpr XY& operator-=(int offset)
155 {
156 value_ -= offset;
157 return *this;
158 }
159
160 constexpr XY operator+(int offset) const
161 {
162 return XY(value_ + offset);
163 }
164 constexpr XY operator-(int offset) const
165 {
166 return XY(value_ - offset);
167 }
168
169 constexpr bool operator==(XY other) const
170 {
171 return value_ == other.value_;
172 }
173 constexpr bool operator==(int other) const
174 {
175 return static_cast<int>(value_) == other;
176 }
177 constexpr bool operator==(size_t other) const
178 {
179 return static_cast<size_t>(value_) == other;
180 }
181 constexpr bool operator==(char other) const
182 {
183 return *this == XY(other);
184 }
185 bool operator==(const std::string& other) const
186 {
187 return *this == XY(other);
188 }
189
190 constexpr bool operator!=(XY other) const
191 {
192 return value_ != other.value_;
193 }
194 constexpr bool operator!=(int other) const
195 {
196 return static_cast<int>(value_) != other;
197 }
198 constexpr bool operator!=(size_t other) const
199 {
200 return static_cast<size_t>(value_) != other;
201 }
202 constexpr bool operator!=(char other) const
203 {
204 return *this != XY(other);
205 }
206 bool operator!=(const std::string& other) const
207 {
208 return *this != XY(other);
209 }
210
211private:
212
213 typedef num::Modulo<dimension, int> TValue;
214
215 TValue value_;
216};
217
218/** @relates lass::prim::XY
219 */
220inline constexpr bool operator==(int a, const XY& b)
221{
222 return b == a;
223}
224
225/** @relates lass::prim::XY
226 */
227inline constexpr bool operator==(size_t a, const XY& b)
228{
229 return b == a;
230}
231
232/** @relates lass::prim::XY
233 */
234inline constexpr bool operator==(char a, const XY& b)
235{
236 return b == a;
237}
238
239/** @relates lass::prim::XY
240 */
241inline bool operator==(const std::string& a, const XY& b)
242{
243 return b == a;
244}
245
246/** @relates lass::prim::XY
247 */
248inline constexpr bool operator!=(int a, const XY& b)
249{
250 return b != a;
251}
252
253/** @relates lass::prim::XY
254 */
255inline constexpr bool operator!=(size_t a, const XY& b)
256{
257 return b != a;
258}
259
260/** @relates lass::prim::XY
261 */
262inline constexpr bool operator!=(char a, const XY& b)
263{
264 return b != a;
265}
266
267/** @relates lass::prim::XY
268 */
269inline bool operator!=(const std::string& a, const XY& b)
270{
271 return b != a;
272}
273
274/** @relates lass::prim::XY
275 */
276template <typename Char, typename Traits>
277std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const XY& xy)
278{
279 stream << xy.axis();
280 return stream;
281}
282
283
284
285}
286
287}
288
289#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_XY
290#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
292#endif
293
294#endif
295
296// EOF
cyclic iterator over xy indices
Definition xy.h:62
XY(const std::string &axis)
initializes iterator to an axis by character: "x" or "y".
Definition xy.h:101
constexpr XY(char axis)
initializes iterator to an axis by character: 'x' or 'y'.
Definition xy.h:73
constexpr char axis() const
return axis by character: 'x' or 'y'.
Definition xy.h:115
constexpr XY(int value)
initializes iterator to an axis by number.
Definition xy.h:97
constexpr XY()
intializes iterator to x axis.
Definition xy.h:69
type of all exceptions in lass
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53