gusimplewhiteboard
MyPosition.hpp
Go to the documentation of this file.
1/*
2 * file MyPosition.hpp
3 *
4 * This file was generated by classgenerator from my_position.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Morgan McColl. 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 Morgan McColl.
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_MyPosition_h
61#define guWhiteboard_MyPosition_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_my_position.h"
71
72#undef guWhiteboard_MyPosition_DEFINED
73#define guWhiteboard_MyPosition_DEFINED
74
75#undef MyPosition_DEFINED
76#define MyPosition_DEFINED
77
78namespace guWhiteboard {
79
83 class MyPosition: public wb_my_position {
84
85 private:
86
90 void init(int16_t t_x = 0, int16_t t_y = 0, int16_t t_heading = 0) {
91 set_x(t_x);
92 set_y(t_y);
93 set_heading(t_heading);
94 }
95
96 public:
97
101 MyPosition(int16_t t_x = 0, int16_t t_y = 0, int16_t t_heading = 0) {
102 this->init(t_x, t_y, t_heading);
103 }
104
109 this->init(t_other.x(), t_other.y(), t_other.heading());
110 }
111
115 MyPosition(const struct wb_my_position &t_other): wb_my_position() {
116 this->init(t_other.x, t_other.y, t_other.heading);
117 }
118
123 this->init(t_other.x(), t_other.y(), t_other.heading());
124 return *this;
125 }
126
130 MyPosition &operator = (const struct wb_my_position &t_other) {
131 this->init(t_other.x, t_other.y, t_other.heading);
132 return *this;
133 }
134
135 bool operator ==(const MyPosition &t_other) const
136 {
137 return x() == t_other.x()
138 && y() == t_other.y()
139 && heading() == t_other.heading();
140 }
141
142 bool operator !=(const MyPosition &t_other) const
143 {
144 return !(*this == t_other);
145 }
146
147 bool operator ==(const wb_my_position &t_other) const
148 {
149 return *this == MyPosition(t_other);
150 }
151
152 bool operator !=(const wb_my_position &t_other) const
153 {
154 return !(*this == t_other);
155 }
156
157 int16_t & x()
158 {
159 return wb_my_position::x;
160 }
161
162 const int16_t & x() const
163 {
164 return wb_my_position::x;
165 }
166
167 void set_x(const int16_t &t_newValue)
168 {
169 wb_my_position::x = t_newValue;
170 }
171
172 int16_t & y()
173 {
174 return wb_my_position::y;
175 }
176
177 const int16_t & y() const
178 {
179 return wb_my_position::y;
180 }
181
182 void set_y(const int16_t &t_newValue)
183 {
184 wb_my_position::y = t_newValue;
185 }
186
187 int16_t & heading()
188 {
190 }
191
192 const int16_t & heading() const
193 {
195 }
196
197 void set_heading(const int16_t &t_newValue)
198 {
199 wb_my_position::heading = t_newValue;
200 }
201
202#ifdef WHITEBOARD_POSTER_STRING_CONVERSION
206 MyPosition(const std::string &t_str) {
207 this->init();
208 this->from_string(t_str);
209 }
210
211 std::string description() {
212#ifdef USE_WB_MY_POSITION_C_CONVERSION
213 char buffer[MY_POSITION_DESC_BUFFER_SIZE];
214 wb_my_position_description(this, buffer, sizeof(buffer));
215 std::string descr = buffer;
216 return descr;
217#else
218 std::ostringstream ss;
219 ss << "x=" << static_cast<signed>(this->x());
220 ss << ", ";
221 ss << "y=" << static_cast<signed>(this->y());
222 ss << ", ";
223 ss << "heading=" << static_cast<signed>(this->heading());
224 return ss.str();
225#endif
226 }
227
228 std::string to_string() {
229#ifdef USE_WB_MY_POSITION_C_CONVERSION
231 wb_my_position_to_string(this, buffer, sizeof(buffer));
232 std::string toString = buffer;
233 return toString;
234#else
235 std::ostringstream ss;
236 ss << static_cast<signed>(this->x());
237 ss << ", ";
238 ss << static_cast<signed>(this->y());
239 ss << ", ";
240 ss << static_cast<signed>(this->heading());
241 return ss.str();
242#endif
243 }
244
245#ifdef USE_WB_MY_POSITION_C_CONVERSION
246 void from_string(const std::string &t_str) {
247 wb_my_position_from_string(this, t_str.c_str());
248#else
249 void from_string(const std::string &t_str) {
250 char * str_cstr = const_cast<char *>(t_str.c_str());
251 size_t temp_length = strlen(str_cstr);
252 int length = (temp_length <= INT_MAX) ? static_cast<int>(static_cast<ssize_t>(temp_length)) : -1;
253 if (length < 1 || length > MY_POSITION_DESC_BUFFER_SIZE) {
254 return;
255 }
256 char var_str_buffer[MY_POSITION_DESC_BUFFER_SIZE + 1];
257 char* var_str = &var_str_buffer[0];
258 char key_buffer[8];
259 char* key = &key_buffer[0];
260 int bracecount = 0;
261 int startVar = 0;
262 int index = 0;
263 int startKey = 0;
264 int endKey = -1;
265 int varIndex = 0;
266 if (index == 0 && str_cstr[0] == '{') {
267 index = 1;
268 }
269 startVar = index;
270 startKey = startVar;
271 do {
272 for (int i = index; i < length; i++) {
273 index = i + 1;
274 if (bracecount == 0 && str_cstr[i] == '=') {
275 endKey = i - 1;
276 startVar = index;
277 continue;
278 }
279 if (bracecount == 0 && isspace(str_cstr[i])) {
280 startVar = index;
281 if (endKey == -1) {
282 startKey = index;
283 }
284 continue;
285 }
286 if (bracecount == 0 && str_cstr[i] == ',') {
287 index = i - 1;
288 break;
289 }
290 if (str_cstr[i] == '{') {
291 bracecount++;
292 continue;
293 }
294 if (str_cstr[i] == '}') {
295 bracecount--;
296 if (bracecount < 0) {
297 index = i - 1;
298 break;
299 }
300 }
301 if (i == length - 1) {
302 index = i;
303 }
304 }
305 if (endKey >= startKey && endKey - startKey < length) {
306 strncpy(key, str_cstr + startKey, static_cast<size_t>((endKey - startKey) + 1));
307 key[(endKey - startKey) + 1] = 0;
308 } else {
309 key[0] = 0;
310 }
311 strncpy(var_str, str_cstr + startVar, static_cast<size_t>((index - startVar) + 1));
312 var_str[(index - startVar) + 1] = 0;
313 bracecount = 0;
314 index += 2;
315 startVar = index;
316 startKey = startVar;
317 endKey = -1;
318 if (strlen(key) > 0) {
319 if (0 == strcmp("x", key)) {
320 varIndex = 0;
321 } else if (0 == strcmp("y", key)) {
322 varIndex = 1;
323 } else if (0 == strcmp("heading", key)) {
324 varIndex = 2;
325 } else {
326 varIndex = -1;
327 }
328 }
329 switch (varIndex) {
330 case -1: { break; }
331 case 0:
332 {
333 this->set_x(static_cast<int16_t>(atoi(var_str)));
334 break;
335 }
336 case 1:
337 {
338 this->set_y(static_cast<int16_t>(atoi(var_str)));
339 break;
340 }
341 case 2:
342 {
343 this->set_heading(static_cast<int16_t>(atoi(var_str)));
344 break;
345 }
346 }
347 if (varIndex >= 0) {
348 varIndex++;
349 }
350 } while(index < length);
351#endif
352 }
353#endif
354 };
355
356}
357
358#endif
Provides a C++ wrapper around wb_my_position.
Definition: MyPosition.hpp:83
void set_y(const int16_t &t_newValue)
Definition: MyPosition.hpp:182
MyPosition(int16_t t_x=0, int16_t t_y=0, int16_t t_heading=0)
Create a new MyPosition.
Definition: MyPosition.hpp:101
MyPosition(const MyPosition &t_other)
Copy Constructor.
Definition: MyPosition.hpp:108
bool operator!=(const MyPosition &t_other) const
Definition: MyPosition.hpp:142
bool operator==(const MyPosition &t_other) const
Definition: MyPosition.hpp:135
MyPosition & operator=(const MyPosition &t_other)
Copy Assignment Operator.
Definition: MyPosition.hpp:122
void set_heading(const int16_t &t_newValue)
Definition: MyPosition.hpp:197
void from_string(const std::string &t_str)
Definition: MyPosition.hpp:249
const int16_t & y() const
Definition: MyPosition.hpp:177
const int16_t & heading() const
Definition: MyPosition.hpp:192
const int16_t & x() const
Definition: MyPosition.hpp:162
MyPosition(const struct wb_my_position &t_other)
Copy Constructor.
Definition: MyPosition.hpp:115
void set_x(const int16_t &t_newValue)
Definition: MyPosition.hpp:167
std::string description()
Definition: MyPosition.hpp:211
MyPosition(const std::string &t_str)
String Constructor.
Definition: MyPosition.hpp:206
/file APM_Interface.h
WHITEBOARD_POSTER_STRING_CONVERSION.
int16_t heading
degrees
struct wb_my_position * wb_my_position_from_string(struct wb_my_position *self, const char *str)
Convert from a string.
const char * wb_my_position_description(const struct wb_my_position *self, char *descString, size_t bufferSize)
Convert to a description string.
const char * wb_my_position_to_string(const struct wb_my_position *self, char *toString, size_t bufferSize)
Convert to a string.
#define MY_POSITION_DESC_BUFFER_SIZE
#define MY_POSITION_TO_STRING_BUFFER_SIZE