knx
ETS configurable knx-stack
knx_value.cpp
Go to the documentation of this file.
1 #include "knx_value.h"
2 
3 #include <cstring>
4 #include <cstdlib>
5 #include <ctime>
6 
7 KNXValue::KNXValue(bool value)
8 {
9  _value.boolValue = value;
10  _type = BoolType;
11 }
12 
13 KNXValue::KNXValue(uint8_t value)
14 {
15  _value.ucharValue = value;
16  _type = UCharType;
17 }
18 
19 KNXValue::KNXValue(uint16_t value)
20 {
21  _value.ushortValue = value;
22  _type = UShortType;
23 }
24 
25 KNXValue::KNXValue(uint32_t value)
26 {
27  _value.uintValue = value;
28  _type = UIntType;
29 }
30 
31 KNXValue::KNXValue(uint64_t value)
32 {
33  _value.ulongValue = value;
34  _type = ULongType;
35 }
36 
37 KNXValue::KNXValue(int8_t value)
38 {
39  _value.charValue = value;
40  _type = CharType;
41 }
42 
43 KNXValue::KNXValue(int16_t value)
44 {
45  _value.shortValue = value;
46  _type = ShortType;
47 }
48 
49 KNXValue::KNXValue(int32_t value)
50 {
51  _value.intValue = value;
52  _type = IntType;
53 }
54 
55 KNXValue::KNXValue(int64_t value)
56 {
57  _value.longValue = value;
58  _type = LongType;
59 }
60 
61 KNXValue::KNXValue(double value)
62 {
63  _value.doubleValue = value;
64  _type = DoubleType;
65 }
66 
67 KNXValue::KNXValue(const char* value)
68 {
69  _value.stringValue = value;
70  _type = StringType;
71 }
72 
73 KNXValue::KNXValue(struct tm value)
74 {
75  _value.timeValue = value;
76  _type = TimeType;
77 }
78 
79 KNXValue::operator bool() const
80 {
81  return boolValue();
82 }
83 
84 KNXValue::operator uint8_t() const
85 {
86  return ucharValue();
87 }
88 
89 KNXValue::operator uint16_t() const
90 {
91  return ushortValue();
92 }
93 
94 KNXValue::operator uint32_t() const
95 {
96  return uintValue();
97 }
98 
99 KNXValue::operator uint64_t() const
100 {
101  return ulongValue();
102 }
103 
104 KNXValue::operator int8_t() const
105 {
106  return charValue();
107 }
108 
109 KNXValue::operator int16_t() const
110 {
111  return shortValue();
112 }
113 
114 KNXValue::operator int32_t() const
115 {
116  return intValue();
117 }
118 
119 KNXValue::operator int64_t() const
120 {
121  return longValue();
122 }
123 
124 KNXValue::operator double() const
125 {
126  return doubleValue();
127 }
128 
129 KNXValue::operator const char*() const
130 {
131  return stringValue();
132 }
133 
134 KNXValue::operator struct tm() const
135 {
136  return timeValue();
137 }
138 
139 KNXValue& KNXValue::operator=(const bool value)
140 {
141  _value.boolValue = value;
142  _type = BoolType;
143  return *this;
144 }
145 
146 KNXValue& KNXValue::operator=(const uint8_t value)
147 {
148  _value.ucharValue = value;
149  _type = UCharType;
150  return *this;
151 }
152 
153 KNXValue& KNXValue::operator=(const uint16_t value)
154 {
155  _value.ushortValue = value;
156  _type = UShortType;
157  return *this;
158 }
159 
160 KNXValue& KNXValue::operator=(const uint32_t value)
161 {
162  _value.uintValue = value;
163  _type = UIntType;
164  return *this;
165 }
166 
167 KNXValue& KNXValue::operator=(const uint64_t value)
168 {
169  _value.ulongValue = value;
170  _type = ULongType;
171  return *this;
172 }
173 
174 KNXValue& KNXValue::operator=(const int8_t value)
175 {
176  _value.charValue = value;
177  _type = CharType;
178  return *this;
179 }
180 
181 KNXValue& KNXValue::operator=(const int16_t value)
182 {
183  _value.shortValue = value;
184  _type = ShortType;
185  return *this;
186 }
187 
188 KNXValue& KNXValue::operator=(const int32_t value)
189 {
190  _value.intValue = value;
191  _type = IntType;
192  return *this;
193 }
194 
195 KNXValue& KNXValue::operator=(const int64_t value)
196 {
197  _value.longValue = value;
198  _type = LongType;
199  return *this;
200 }
201 
202 KNXValue& KNXValue::operator=(const double value)
203 {
204  _value.doubleValue = value;
205  _type = DoubleType;
206  return *this;
207 }
208 
209 KNXValue& KNXValue::operator=(const char* value)
210 {
211  _value.stringValue = value;
212  _type = StringType;
213  return *this;
214 }
215 
216 KNXValue& KNXValue::operator=(const struct tm value)
217 {
218  _value.timeValue = value;
219  _type = TimeType;
220  return *this;
221 }
222 
223 bool KNXValue::boolValue() const
224 {
225  switch (_type)
226  {
227  case BoolType:
228  return _value.boolValue;
229  case UCharType:
230  case UShortType:
231  case UIntType:
232  case ULongType:
233  case CharType:
234  case ShortType:
235  case IntType:
236  case LongType:
237  case TimeType:
238  return longValue() != 0;
239  case DoubleType:
240  return _value.doubleValue != 0;
241  case StringType:
242  return strcmp(_value.stringValue, "true") == 0 || strcmp(_value.stringValue, "True") == 0 || longValue() != 0 || doubleValue() != 0;
243  }
244  return 0;
245 }
246 
247 uint8_t KNXValue::ucharValue() const
248 {
249  switch (_type)
250  {
251  case UCharType:
252  return _value.ucharValue;
253  case BoolType:
254  case UShortType:
255  case UIntType:
256  case ULongType:
257  case TimeType:
258  return (uint8_t)ulongValue();
259  case CharType:
260  case ShortType:
261  case IntType:
262  case LongType:
263  case DoubleType:
264  case StringType:
265  return (uint8_t)longValue();
266  }
267  return 0;
268 }
269 
270 uint16_t KNXValue::ushortValue() const
271 {
272  switch (_type)
273  {
274  case UShortType:
275  return _value.ushortValue;
276  case BoolType:
277  case UCharType:
278  case UIntType:
279  case ULongType:
280  case TimeType:
281  return (uint16_t)ulongValue();
282  case CharType:
283  case ShortType:
284  case IntType:
285  case LongType:
286  case DoubleType:
287  case StringType:
288  return (uint16_t)longValue();
289  }
290  return 0;
291 }
292 
293 uint32_t KNXValue::uintValue() const
294 {
295  switch (_type)
296  {
297  case UIntType:
298  return _value.uintValue;
299  case BoolType:
300  case UCharType:
301  case UShortType:
302  case ULongType:
303  case TimeType:
304  return (uint32_t)ulongValue();
305  case CharType:
306  case ShortType:
307  case IntType:
308  case LongType:
309  case DoubleType:
310  case StringType:
311  return (uint32_t)longValue();
312  }
313  return 0;
314 }
315 
316 uint64_t KNXValue::ulongValue() const
317 {
318  switch (_type)
319  {
320  case ULongType:
321  return _value.ulongValue;
322  case BoolType:
323  return _value.boolValue ? 1 : 0;
324  case UCharType:
325  return (uint64_t)_value.ucharValue;
326  case UShortType:
327  return (uint64_t)_value.ushortValue;
328  case UIntType:
329  return (uint64_t)_value.uintValue;
330  case TimeType:
331  {
332  struct tm* timeptr = const_cast<struct tm*>(&_value.timeValue);
333  return (uint64_t)mktime(timeptr);
334  }
335  case CharType:
336  return (uint64_t)_value.charValue;
337  case ShortType:
338  return (uint64_t)_value.shortValue;
339  case IntType:
340  return (uint64_t)_value.intValue;
341  case LongType:
342  return (uint64_t)_value.longValue;
343  case DoubleType:
344  return (uint64_t)_value.doubleValue;
345  case StringType:
346 #ifndef KNX_NO_STRTOx_CONVERSION
347  return (uint64_t)strtoul(_value.stringValue, NULL, 0);
348 #else
349  return 0;
350 #endif
351  }
352  return 0;
353 }
354 
355 int8_t KNXValue::charValue() const
356 {
357  switch (_type)
358  {
359  case CharType:
360  return _value.charValue;
361  case BoolType:
362  case UCharType:
363  case UShortType:
364  case UIntType:
365  case ULongType:
366  case TimeType:
367  return (int8_t)ulongValue();
368  case ShortType:
369  case IntType:
370  case LongType:
371  case DoubleType:
372  case StringType:
373  return (int8_t)longValue();
374  }
375  return 0;
376 }
377 
378 int16_t KNXValue::shortValue() const
379 {
380  switch (_type)
381  {
382  case ShortType:
383  return _value.shortValue;
384  case BoolType:
385  case UCharType:
386  case UShortType:
387  case UIntType:
388  case ULongType:
389  case TimeType:
390  return (int16_t)ulongValue();
391  case CharType:
392  case IntType:
393  case LongType:
394  case DoubleType:
395  case StringType:
396  return (int16_t)longValue();
397  }
398  return 0;
399 }
400 
401 int32_t KNXValue::intValue() const
402 {
403  switch (_type)
404  {
405  case IntType:
406  return _value.shortValue;
407  case BoolType:
408  case UCharType:
409  case UShortType:
410  case UIntType:
411  case ULongType:
412  case TimeType:
413  return (int32_t)ulongValue();
414  case CharType:
415  case ShortType:
416  case LongType:
417  case DoubleType:
418  case StringType:
419  return (int32_t)longValue();
420  }
421  return 0;
422 }
423 
424 int64_t KNXValue::longValue() const
425 {
426  switch (_type)
427  {
428  case LongType:
429  return _value.longValue;
430  case BoolType:
431  return _value.boolValue ? 1 : 0;
432  case UCharType:
433  return (int64_t)_value.ucharValue;
434  case UShortType:
435  return (int64_t)_value.ushortValue;
436  case UIntType:
437  return (int64_t)_value.uintValue;
438  case ULongType:
439  return (int64_t)_value.uintValue;
440  case TimeType:
441  return (int64_t)ulongValue();
442  case CharType:
443  return (int64_t)_value.charValue;
444  case ShortType:
445  return (int64_t)_value.shortValue;
446  case IntType:
447  return (int64_t)_value.intValue;
448  case DoubleType:
449  return (int64_t)_value.doubleValue;
450  case StringType:
451 #ifndef KNX_NO_STRTOx_CONVERSION
452  return strtol(_value.stringValue, NULL, 0);
453 #else
454  return 0;
455 #endif
456  }
457  return 0;
458 }
459 
460 double KNXValue::doubleValue() const
461 {
462  switch (_type)
463  {
464  case DoubleType:
465  return _value.doubleValue;
466  case BoolType:
467  return _value.boolValue ? 1 : 0;
468  case UCharType:
469  return _value.ucharValue;
470  case UShortType:
471  return _value.ushortValue;
472  case UIntType:
473  return _value.uintValue;
474  case ULongType:
475  return _value.uintValue;
476  case TimeType:
477  return ulongValue();
478  case CharType:
479  return _value.charValue;
480  case ShortType:
481  return _value.shortValue;
482  case IntType:
483  return _value.intValue;
484  case LongType:
485  return _value.longValue;
486  case StringType:
487 #ifndef KNX_NO_STRTOx_CONVERSION
488  return strtod(_value.stringValue, NULL);
489 #else
490  return 0;
491 #endif
492  }
493  return 0;
494 }
495 
496 const char* KNXValue::stringValue() const
497 {
498  switch (_type)
499  {
500  case DoubleType:
501  case BoolType:
502  case UCharType:
503  case UShortType:
504  case UIntType:
505  case ULongType:
506  case TimeType:
507  case CharType:
508  case ShortType:
509  case IntType:
510  case LongType:
511  return ""; // we would have to manage the memory for the string otherwise. Maybe later.
512  case StringType:
513  return _value.stringValue;
514  }
515  return 0;
516 }
517 
518 struct tm KNXValue::timeValue() const
519 {
520  switch (_type)
521  {
522  case TimeType:
523  return _value.timeValue;
524  case BoolType:
525  case UCharType:
526  case UShortType:
527  case UIntType:
528  case ULongType:
529  case CharType:
530  case ShortType:
531  case IntType:
532  case LongType:
533  case DoubleType:
534  case StringType:
535  {
536  time_t timeVal = ulongValue();
537  struct tm timeStruct;
538  gmtime_r(&timeVal, &timeStruct);
539  return timeStruct;
540  }
541  }
542  struct tm tmp = {0};
543  return tmp;
544 }
545 
546 KNXValue::KNXValue(float value)
547 {
548  _value.doubleValue = value;
549  _type = DoubleType;
550 }
551 
552 KNXValue& KNXValue::operator=(const float value)
553 {
554  _value.doubleValue = value;
555  _type = DoubleType;
556  return *this;
557 }
558 
559 KNXValue::operator float() const
560 {
561  return doubleValue();
562 }
KNXValue & operator=(const bool value)
Definition: knx_value.cpp:139
KNXValue(bool value)
Definition: knx_value.cpp:7