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 
230  case UCharType:
231  case UShortType:
232  case UIntType:
233  case ULongType:
234  case CharType:
235  case ShortType:
236  case IntType:
237  case LongType:
238  case TimeType:
239  return longValue() != 0;
240 
241  case DoubleType:
242  return _value.doubleValue != 0;
243 
244  case StringType:
245  return strcmp(_value.stringValue, "true") == 0 || strcmp(_value.stringValue, "True") == 0 || longValue() != 0 || doubleValue() != 0;
246  }
247 
248  return 0;
249 }
250 
251 uint8_t KNXValue::ucharValue() const
252 {
253  switch (_type)
254  {
255  case UCharType:
256  return _value.ucharValue;
257 
258  case BoolType:
259  case UShortType:
260  case UIntType:
261  case ULongType:
262  case TimeType:
263  return (uint8_t)ulongValue();
264 
265  case CharType:
266  case ShortType:
267  case IntType:
268  case LongType:
269  case DoubleType:
270  case StringType:
271  return (uint8_t)longValue();
272  }
273 
274  return 0;
275 }
276 
277 uint16_t KNXValue::ushortValue() const
278 {
279  switch (_type)
280  {
281  case UShortType:
282  return _value.ushortValue;
283 
284  case BoolType:
285  case UCharType:
286  case UIntType:
287  case ULongType:
288  case TimeType:
289  return (uint16_t)ulongValue();
290 
291  case CharType:
292  case ShortType:
293  case IntType:
294  case LongType:
295  case DoubleType:
296  case StringType:
297  return (uint16_t)longValue();
298  }
299 
300  return 0;
301 }
302 
303 uint32_t KNXValue::uintValue() const
304 {
305  switch (_type)
306  {
307  case UIntType:
308  return _value.uintValue;
309 
310  case BoolType:
311  case UCharType:
312  case UShortType:
313  case ULongType:
314  case TimeType:
315  return (uint32_t)ulongValue();
316 
317  case CharType:
318  case ShortType:
319  case IntType:
320  case LongType:
321  case DoubleType:
322  case StringType:
323  return (uint32_t)longValue();
324  }
325 
326  return 0;
327 }
328 
329 uint64_t KNXValue::ulongValue() const
330 {
331  switch (_type)
332  {
333  case ULongType:
334  return _value.ulongValue;
335 
336  case BoolType:
337  return _value.boolValue ? 1 : 0;
338 
339  case UCharType:
340  return (uint64_t)_value.ucharValue;
341 
342  case UShortType:
343  return (uint64_t)_value.ushortValue;
344 
345  case UIntType:
346  return (uint64_t)_value.uintValue;
347 
348  case TimeType:
349  {
350  struct tm* timeptr = const_cast<struct tm*>(&_value.timeValue);
351  return (uint64_t)mktime(timeptr);
352  }
353 
354  case CharType:
355  return (uint64_t)_value.charValue;
356 
357  case ShortType:
358  return (uint64_t)_value.shortValue;
359 
360  case IntType:
361  return (uint64_t)_value.intValue;
362 
363  case LongType:
364  return (uint64_t)_value.longValue;
365 
366  case DoubleType:
367  return (uint64_t)_value.doubleValue;
368 
369  case StringType:
370 #ifndef KNX_NO_STRTOx_CONVERSION
371  return (uint64_t)strtoul(_value.stringValue, NULL, 0);
372 #else
373  return 0;
374 #endif
375  }
376 
377  return 0;
378 }
379 
380 int8_t KNXValue::charValue() const
381 {
382  switch (_type)
383  {
384  case CharType:
385  return _value.charValue;
386 
387  case BoolType:
388  case UCharType:
389  case UShortType:
390  case UIntType:
391  case ULongType:
392  case TimeType:
393  return (int8_t)ulongValue();
394 
395  case ShortType:
396  case IntType:
397  case LongType:
398  case DoubleType:
399  case StringType:
400  return (int8_t)longValue();
401  }
402 
403  return 0;
404 }
405 
406 int16_t KNXValue::shortValue() const
407 {
408  switch (_type)
409  {
410  case ShortType:
411  return _value.shortValue;
412 
413  case BoolType:
414  case UCharType:
415  case UShortType:
416  case UIntType:
417  case ULongType:
418  case TimeType:
419  return (int16_t)ulongValue();
420 
421  case CharType:
422  case IntType:
423  case LongType:
424  case DoubleType:
425  case StringType:
426  return (int16_t)longValue();
427  }
428 
429  return 0;
430 }
431 
432 int32_t KNXValue::intValue() const
433 {
434  switch (_type)
435  {
436  case IntType:
437  return _value.ulongValue;
438 
439  case BoolType:
440  case UCharType:
441  case UShortType:
442  case UIntType:
443  case ULongType:
444  case TimeType:
445  return (int32_t)ulongValue();
446 
447  case CharType:
448  case ShortType:
449  case LongType:
450  case DoubleType:
451  case StringType:
452  return (int32_t)longValue();
453  }
454 
455  return 0;
456 }
457 
458 int64_t KNXValue::longValue() const
459 {
460  switch (_type)
461  {
462  case LongType:
463  return _value.longValue;
464 
465  case BoolType:
466  return _value.boolValue ? 1 : 0;
467 
468  case UCharType:
469  return (int64_t)_value.ucharValue;
470 
471  case UShortType:
472  return (int64_t)_value.ushortValue;
473 
474  case UIntType:
475  return (int64_t)_value.uintValue;
476 
477  case ULongType:
478  return (int64_t)_value.uintValue;
479 
480  case TimeType:
481  return (int64_t)ulongValue();
482 
483  case CharType:
484  return (int64_t)_value.charValue;
485 
486  case ShortType:
487  return (int64_t)_value.shortValue;
488 
489  case IntType:
490  return (int64_t)_value.intValue;
491 
492  case DoubleType:
493  return (int64_t)_value.doubleValue;
494 
495  case StringType:
496 #ifndef KNX_NO_STRTOx_CONVERSION
497  return strtol(_value.stringValue, NULL, 0);
498 #else
499  return 0;
500 #endif
501  }
502 
503  return 0;
504 }
505 
506 double KNXValue::doubleValue() const
507 {
508  switch (_type)
509  {
510  case DoubleType:
511  return _value.doubleValue;
512 
513  case BoolType:
514  return _value.boolValue ? 1 : 0;
515 
516  case UCharType:
517  return _value.ucharValue;
518 
519  case UShortType:
520  return _value.ushortValue;
521 
522  case UIntType:
523  return _value.uintValue;
524 
525  case ULongType:
526  return _value.uintValue;
527 
528  case TimeType:
529  return ulongValue();
530 
531  case CharType:
532  return _value.charValue;
533 
534  case ShortType:
535  return _value.shortValue;
536 
537  case IntType:
538  return _value.intValue;
539 
540  case LongType:
541  return _value.longValue;
542 
543  case StringType:
544 #ifndef KNX_NO_STRTOx_CONVERSION
545  return strtod(_value.stringValue, NULL);
546 #else
547  return 0;
548 #endif
549  }
550 
551  return 0;
552 }
553 
554 const char* KNXValue::stringValue() const
555 {
556  switch (_type)
557  {
558  case DoubleType:
559  case BoolType:
560  case UCharType:
561  case UShortType:
562  case UIntType:
563  case ULongType:
564  case TimeType:
565  case CharType:
566  case ShortType:
567  case IntType:
568  case LongType:
569  return ""; // we would have to manage the memory for the string otherwise. Maybe later.
570 
571  case StringType:
572  return _value.stringValue;
573  }
574 
575  return 0;
576 }
577 
578 struct tm KNXValue::timeValue() const
579 {
580  switch (_type)
581  {
582  case TimeType:
583  return _value.timeValue;
584 
585  case BoolType:
586  case UCharType:
587  case UShortType:
588  case UIntType:
589  case ULongType:
590  case CharType:
591  case ShortType:
592  case IntType:
593  case LongType:
594  case DoubleType:
595  case StringType:
596  {
597  time_t timeVal = ulongValue();
598  struct tm timeStruct;
599  gmtime_r(&timeVal, &timeStruct);
600  return timeStruct;
601  }
602  }
603 
604  struct tm tmp;
605  memset(&tmp, 0, sizeof(tmp));
606 
607  return tmp;
608 }
609 
610 KNXValue::KNXValue(float value)
611 {
612  _value.doubleValue = value;
613  _type = DoubleType;
614 }
615 
616 KNXValue& KNXValue::operator=(const float value)
617 {
618  _value.doubleValue = value;
619  _type = DoubleType;
620  return *this;
621 }
622 
623 KNXValue::operator float() const
624 {
625  return doubleValue();
626 }
KNXValue & operator=(const bool value)
Definition: knx_value.cpp:139
KNXValue(bool value)
Definition: knx_value.cpp:7