gusimplewhiteboard
Point2D.hpp
Go to the documentation of this file.
1/*
2 * file Point2D.hpp
3 *
4 * This file was generated by classgenerator from point2D.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Carl Lusty. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Carl Lusty.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef guWhiteboard_Point2D_h
61#define guWhiteboard_Point2D_h
62
63#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
64#include <cstdlib>
65#include <string.h>
66#include <sstream>
67#endif
68
69#include <gu_util.h>
70#include "wb_point2d.h"
71
72#undef guWhiteboard_Point2D_DEFINED
73#define guWhiteboard_Point2D_DEFINED
74
75#undef Point2D_DEFINED
76#define Point2D_DEFINED
77
78namespace guWhiteboard {
79
83 class Point2D: public wb_point2d {
84
85 private:
86
90 void init(int16_t t_x = 0, int16_t t_y = 0) {
91 set_x(t_x);
92 set_y(t_y);
93 }
94
95 public:
96
100 Point2D(int16_t t_x = 0, int16_t t_y = 0) {
101 this->init(t_x, t_y);
102 }
103
107 Point2D(const Point2D &t_other): wb_point2d() {
108 this->init(t_other.x(), t_other.y());
109 }
110
114 Point2D(const struct wb_point2d &t_other): wb_point2d() {
115 this->init(t_other.x, t_other.y);
116 }
117
121 Point2D &operator = (const Point2D &t_other) {
122 this->init(t_other.x(), t_other.y());
123 return *this;
124 }
125
129 Point2D &operator = (const struct wb_point2d &t_other) {
130 this->init(t_other.x, t_other.y);
131 return *this;
132 }
133
134 bool operator ==(const Point2D &t_other) const
135 {
136 return x() == t_other.x()
137 && y() == t_other.y();
138 }
139
140 bool operator !=(const Point2D &t_other) const
141 {
142 return !(*this == t_other);
143 }
144
145 bool operator ==(const wb_point2d &t_other) const
146 {
147 return *this == Point2D(t_other);
148 }
149
150 bool operator !=(const wb_point2d &t_other) const
151 {
152 return !(*this == t_other);
153 }
154
155 int16_t & x()
156 {
157 return wb_point2d::x;
158 }
159
160 const int16_t & x() const
161 {
162 return wb_point2d::x;
163 }
164
165 void set_x(const int16_t &t_newValue)
166 {
167 wb_point2d::x = t_newValue;
168 }
169
170 int16_t & y()
171 {
172 return wb_point2d::y;
173 }
174
175 const int16_t & y() const
176 {
177 return wb_point2d::y;
178 }
179
180 void set_y(const int16_t &t_newValue)
181 {
182 wb_point2d::y = t_newValue;
183 }
184
185#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
189 Point2D(const std::string &t_str) {
190 this->init();
191 this->from_string(t_str);
192 }
193
194 std::string description() {
195#ifdef USE_WB_POINT2D_C_CONVERSION
196 char buffer[POINT2D_DESC_BUFFER_SIZE];
197 wb_point2d_description(this, buffer, sizeof(buffer));
198 std::string descr = buffer;
199 return descr;
200#else
201 std::ostringstream ss;
202 ss << "x=" << static_cast<signed>(this->x());
203 ss << ", ";
204 ss << "y=" << static_cast<signed>(this->y());
205 return ss.str();
206#endif
207 }
208
209 std::string to_string() {
210#ifdef USE_WB_POINT2D_C_CONVERSION
212 wb_point2d_to_string(this, buffer, sizeof(buffer));
213 std::string toString = buffer;
214 return toString;
215#else
216 std::ostringstream ss;
217 ss << static_cast<signed>(this->x());
218 ss << ", ";
219 ss << static_cast<signed>(this->y());
220 return ss.str();
221#endif
222 }
223
224#ifdef USE_WB_POINT2D_C_CONVERSION
225 void from_string(const std::string &t_str) {
226 wb_point2d_from_string(this, t_str.c_str());
227#else
228 void from_string(const std::string &t_str) {
229 char * str_cstr = const_cast<char *>(t_str.c_str());
230 size_t temp_length = strlen(str_cstr);
231 int length = (temp_length <= INT_MAX) ? static_cast<int>(static_cast<ssize_t>(temp_length)) : -1;
232 if (length < 1 || length > POINT2D_DESC_BUFFER_SIZE) {
233 return;
234 }
235 char var_str_buffer[POINT2D_DESC_BUFFER_SIZE + 1];
236 char* var_str = &var_str_buffer[0];
237 char key_buffer[2];
238 char* key = &key_buffer[0];
239 int bracecount = 0;
240 int startVar = 0;
241 int index = 0;
242 int startKey = 0;
243 int endKey = -1;
244 int varIndex = 0;
245 if (index == 0 && str_cstr[0] == '{') {
246 index = 1;
247 }
248 startVar = index;
249 startKey = startVar;
250 do {
251 for (int i = index; i < length; i++) {
252 index = i + 1;
253 if (bracecount == 0 && str_cstr[i] == '=') {
254 endKey = i - 1;
255 startVar = index;
256 continue;
257 }
258 if (bracecount == 0 && isspace(str_cstr[i])) {
259 startVar = index;
260 if (endKey == -1) {
261 startKey = index;
262 }
263 continue;
264 }
265 if (bracecount == 0 && str_cstr[i] == ',') {
266 index = i - 1;
267 break;
268 }
269 if (str_cstr[i] == '{') {
270 bracecount++;
271 continue;
272 }
273 if (str_cstr[i] == '}') {
274 bracecount--;
275 if (bracecount < 0) {
276 index = i - 1;
277 break;
278 }
279 }
280 if (i == length - 1) {
281 index = i;
282 }
283 }
284 if (endKey >= startKey && endKey - startKey < length) {
285 strncpy(key, str_cstr + startKey, static_cast<size_t>((endKey - startKey) + 1));
286 key[(endKey - startKey) + 1] = 0;
287 } else {
288 key[0] = 0;
289 }
290 strncpy(var_str, str_cstr + startVar, static_cast<size_t>((index - startVar) + 1));
291 var_str[(index - startVar) + 1] = 0;
292 bracecount = 0;
293 index += 2;
294 startVar = index;
295 startKey = startVar;
296 endKey = -1;
297 if (strlen(key) > 0) {
298 if (0 == strcmp("x", key)) {
299 varIndex = 0;
300 } else if (0 == strcmp("y", key)) {
301 varIndex = 1;
302 } else {
303 varIndex = -1;
304 }
305 }
306 switch (varIndex) {
307 case -1: { break; }
308 case 0:
309 {
310 this->set_x(static_cast<int16_t>(atoi(var_str)));
311 break;
312 }
313 case 1:
314 {
315 this->set_y(static_cast<int16_t>(atoi(var_str)));
316 break;
317 }
318 }
319 if (varIndex >= 0) {
320 varIndex++;
321 }
322 } while(index < length);
323#endif
324 }
325#endif
326 };
327
328}
329
330#endif
Provides a C++ wrapper around wb_point2d.
Definition: Point2D.hpp:83
Point2D(const std::string &t_str)
String Constructor.
Definition: Point2D.hpp:189
bool operator!=(const Point2D &t_other) const
Definition: Point2D.hpp:140
void set_y(const int16_t &t_newValue)
Definition: Point2D.hpp:180
std::string to_string()
Definition: Point2D.hpp:209
Point2D(const struct wb_point2d &t_other)
Copy Constructor.
Definition: Point2D.hpp:114
void from_string(const std::string &t_str)
Definition: Point2D.hpp:228
Point2D(int16_t t_x=0, int16_t t_y=0)
Create a new Point2D.
Definition: Point2D.hpp:100
Point2D(const Point2D &t_other)
Copy Constructor.
Definition: Point2D.hpp:107
std::string description()
Definition: Point2D.hpp:194
const int16_t & y() const
Definition: Point2D.hpp:175
bool operator==(const Point2D &t_other) const
Definition: Point2D.hpp:134
const int16_t & x() const
Definition: Point2D.hpp:160
void set_x(const int16_t &t_newValue)
Definition: Point2D.hpp:165
Point2D & operator=(const Point2D &t_other)
Copy Assignment Operator.
Definition: Point2D.hpp:121
/file APM_Interface.h
WHITEBOARD_POSTER_STRING_CONVERSION.
Definition: wb_point2d.h:105
int16_t y
y
Definition: wb_point2d.h:115
int16_t x
x
Definition: wb_point2d.h:110
struct wb_point2d * wb_point2d_from_string(struct wb_point2d *self, const char *str)
Convert from a string.
Definition: wb_point2d.c:173
const char * wb_point2d_description(const struct wb_point2d *self, char *descString, size_t bufferSize)
Convert to a description string.
Definition: wb_point2d.c:125
const char * wb_point2d_to_string(const struct wb_point2d *self, char *toString, size_t bufferSize)
Convert to a string.
Definition: wb_point2d.c:149
#define POINT2D_DESC_BUFFER_SIZE
Definition: wb_point2d.h:93
#define POINT2D_TO_STRING_BUFFER_SIZE
Definition: wb_point2d.h:94