gusimplewhiteboard
WBMsg.h
Go to the documentation of this file.
1/*
2 * WBMsg.h
3 *
4 * Created by René Hexel on 21/12/11.
5 * Copyright (c) 2011, 2012, 2013, 2016 Rene Hexel.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58#ifndef gusimplewhiteboard_WBMsg_h
59#define gusimplewhiteboard_WBMsg_h
60
61#include <cstdlib>
62#include <cstring>
63#include <string>
64#include <iostream> //for cerr
65#include <sstream> // for stringValue()
66#include "gusimplewhiteboard.h" //for GU_SIMPLE_WHITEBOARD_GENERATIONS
67
68#ifdef bool
69#undef bool
70#endif
71
72#ifdef true
73#undef true
74#undef false
75#endif
76
77#pragma clang diagnostic push
78#pragma clang diagnostic ignored "-Wweak-vtables"
79#pragma clang diagnostic ignored "-Wpadded"
80#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
81#pragma clang diagnostic ignored "-Wconversion"
82#pragma clang diagnostic ignored "-Wdeprecated"
83
85class WBMsg
86{
87public:
92 typedef enum wb_type
93 {
102private:
103 union
104 {
107 float floatVal;
108 const void *binaryVal;
109 const std::vector<int> *arrayVal;
110 };
111 WBType type;
112 int binarySize;
113 std::string typeName;
114 std::string stringVal;
115 bool doFree;
116
117public:
123 WBMsg(const WBMsg &orig) { memcpy(this, &orig, sizeof(orig)); }
124
129 WBMsg(): type(TypeEmpty) {}
130
136 WBMsg(bool val): boolVal(val), type(TypeBool) {}
137
144 WBMsg(int val): intVal(val), type(TypeInt) {}
151 WBMsg(float val): floatVal(val), type(TypeFloat) {}
152
158 WBMsg(const std::string &val): type(TypeString), stringVal(val) {}
159
165 WBMsg(const char *val): type(TypeString), stringVal(val) {}
166
176 WBMsg(const void *data, int nBytes, bool needsFree = false): binaryVal(data), type(TypeBinary), binarySize(nBytes), doFree(needsFree) {}
177
185 WBMsg(const std::vector<int> *val, bool needsFree = false): arrayVal(val), type(TypeArray), doFree(needsFree) {}
186
193 WBMsg(const std::vector<int> &val, bool needsFree = false): arrayVal(new std::vector<int>(val)), type(TypeArray), doFree(true) { if (needsFree) delete &val; }
194
199 WBType getType() const { return type; }
200
204 void setTypeName(const std::string &t) { typeName = t; }
205
210 const std::string &getTypeName() const { return typeName; }
211
217
221 void setLifeSpan(int life)
222 {
223 std::cerr << "Setting a life span (" << life << ") not supported on WBMsg()\n" << std::endl;
224 }
225
226#pragma clang diagnostic push
227#pragma clang diagnostic ignored "-Wswitch-enum"
228
233 bool boolValue() const
234 {
235 switch (type)
236 {
237 case TypeBool:
238 return boolVal;
239 case TypeInt:
240 return intVal != 0;
241 case TypeFloat:
242 return floatVal != 0.0f;
243 case TypeString:
244 return toupper(stringVal[0]) == 'Y' ||
245 toupper(stringVal[0]) == 'T' ||
246 stringVal == "on" || stringVal == "On" ||
247 stringVal == "ON" ||
248 atoi(stringVal.c_str()) != 0;
249 case TypeArray:
250 return !arrayVal->size() || (*arrayVal)[0];
251 default:
252 return true;
253 }
254 }
255
260 int intValue() const
261 {
262 switch (type)
263 {
264 case TypeBool:
265 return int(boolVal);
266 case TypeInt:
267 return intVal;
268 case TypeFloat:
269 return int(floatVal);
270 case TypeString:
271 return atoi(stringVal.c_str());
272 case TypeArray:
273 if (arrayVal->size()) return (*arrayVal)[0];
274 default:
275 return 0;
276 }
277 }
278
283 int floatValue() const
284 {
285 switch (type)
286 {
287 case TypeBool:
288 return float(boolVal);
289 case TypeInt:
290 return float(intVal);
291 case TypeFloat:
292 return floatVal;
293 case TypeString:
294 return atof(stringVal.c_str());
295 case TypeArray:
296 if (arrayVal->size()) return (*arrayVal)[0];
297 default:
298 return 0;
299 }
300 }
301
306 const std::string stringValue() const
307 {
308 std::stringstream ss;
309 switch (type)
310 {
311 case TypeBool:
312 ss << boolVal;
313 break;
314 case TypeInt:
315 ss << intVal;
316 break;
317 case TypeFloat:
318 ss << floatVal;
319 break;
320 case TypeString:
321 return stringVal;
322 case TypeArray:
323 {
324 std::vector<int>::const_iterator i = arrayVal->begin();
325 ss << *i++;
326 while (i != arrayVal->end())
327 {
328 ss << ", ";
329 ss << *i++;
330 }
331 break;
332 }
333 case TypeBinary:
334 {
335 const unsigned char *c = static_cast<const unsigned char *>(binaryVal);
336 ss.setf(std::ios::hex);
337 for (int i = 0; i < binarySize; i++)
338 {
339 ss << static_cast<unsigned>(c[i]);
340 if (i < binarySize-1)
341 ss << " ";
342 }
343 break;
344 }
345 default:
346 break;
347 }
348 return ss.str();
349 }
350
351#pragma clang diagnostic pop
352
358 bool getBoolValue() const { return boolVal; }
359
365 int getIntValue() const { return intVal; }
366
372 float getFloatValue() const { return floatVal; }
373
379 const std::string &getStringValue() const { return stringVal; }
380
386 const void *getBinaryValue() const { return binaryVal; }
387
393 const std::vector<int> &getArrayValue() const { return *arrayVal; }
394
400 int getSizeInBytes() const { return binarySize; }
401
405 ~WBMsg() { if (doFree) { if (type == TypeBinary) free(const_cast<void *>(binaryVal)); else if (type == TypeArray) delete arrayVal; } }
406};
407
408#pragma clang diagnostic pop
409
410#endif
Old WB class for storing shared data.
Definition: WBMsg.h:86
WBMsg(bool val)
WBMsg - Boolean constructor.
Definition: WBMsg.h:136
WBMsg(const WBMsg &orig)
WBMsg - copy constructor.
Definition: WBMsg.h:123
const std::string & getStringValue() const
getStringValue.
Definition: WBMsg.h:379
WBMsg(const std::string &val)
WBMsg - String constructor.
Definition: WBMsg.h:158
WBMsg(const char *val)
WBMsg - String constructor.
Definition: WBMsg.h:165
float floatVal
Definition: WBMsg.h:107
const std::string stringValue() const
stringValue.
Definition: WBMsg.h:306
bool boolVal
Definition: WBMsg.h:105
int intValue() const
intValue.
Definition: WBMsg.h:260
int intVal
Definition: WBMsg.h:106
int getIntValue() const
getIntValue.
Definition: WBMsg.h:365
const std::vector< int > * arrayVal
Definition: WBMsg.h:109
WBMsg()
WBMsg - empty constructor.
Definition: WBMsg.h:129
bool boolValue() const
boolValue.
Definition: WBMsg.h:233
WBMsg(const std::vector< int > *val, bool needsFree=false)
WBMsg - Vector<int> pointer constructor.
Definition: WBMsg.h:185
~WBMsg()
default destructor
Definition: WBMsg.h:405
const std::vector< int > & getArrayValue() const
getArrayValue.
Definition: WBMsg.h:393
int getLifeSpan() const
getLifeSpan
Definition: WBMsg.h:216
wb_type
Message internal Datatype Enum.
Definition: WBMsg.h:93
@ TypeEmpty
Definition: WBMsg.h:100
@ TypeString
Definition: WBMsg.h:97
@ TypeBinary
Definition: WBMsg.h:99
@ TypeFloat
Definition: WBMsg.h:96
@ TypeBool
Definition: WBMsg.h:94
@ TypeArray
Definition: WBMsg.h:98
@ TypeInt
Definition: WBMsg.h:95
void setTypeName(const std::string &t)
setTypeName
Definition: WBMsg.h:204
float getFloatValue() const
getFloatValue.
Definition: WBMsg.h:372
const std::string & getTypeName() const
getTypeName
Definition: WBMsg.h:210
WBMsg(const std::vector< int > &val, bool needsFree=false)
WBMsg - Vector<int> constructor.
Definition: WBMsg.h:193
WBMsg(float val)
WBMsg - Float constructor.
Definition: WBMsg.h:151
WBType getType() const
getType.
Definition: WBMsg.h:199
const void * getBinaryValue() const
getBinaryValue.
Definition: WBMsg.h:386
void setLifeSpan(int life)
setLifeSpan
Definition: WBMsg.h:221
int getSizeInBytes() const
getSizeInBytes.
Definition: WBMsg.h:400
bool getBoolValue() const
getBoolValue.
Definition: WBMsg.h:358
WBMsg(const void *data, int nBytes, bool needsFree=false)
WBMsg - Binary data constructor.
Definition: WBMsg.h:176
enum WBMsg::wb_type WBType
Message internal Datatype Enum.
WBMsg(int val)
WBMsg - Integer constructor.
Definition: WBMsg.h:144
int floatValue() const
floatValue.
Definition: WBMsg.h:283
const void * binaryVal
Definition: WBMsg.h:108
#define GU_SIMPLE_WHITEBOARD_GENERATIONS
lifespan (max)