F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
PolyType.cpp
Go to the documentation of this file.
1 #include <Fw/Types/Assert.hpp>
2 #include <Fw/Types/PolyType.hpp>
3 #include <cstdio>
4 #define __STDC_FORMAT_MACROS
5 
6 namespace Fw {
7 
8 // U8 methods
9 
11  this->m_dataType = TYPE_NOTYPE;
12 }
13 
15  this->m_dataType = TYPE_U8;
16  this->m_val.u8Val = val;
17 }
18 
19 PolyType::operator U8() {
20  FW_ASSERT(TYPE_U8 == this->m_dataType);
21  return this->m_val.u8Val;
22 }
23 
24 void PolyType::get(U8& val) {
25  FW_ASSERT(TYPE_U8 == this->m_dataType);
26  val = this->m_val.u8Val;
27 }
28 
30  return (TYPE_U8 == this->m_dataType);
31 }
32 
34  this->m_dataType = TYPE_U8;
35  this->m_val.u8Val = other;
36  return *this;
37 }
38 
39 // I8 methods
40 
42  this->m_dataType = TYPE_I8;
43  this->m_val.i8Val = val;
44 }
45 
46 PolyType::operator I8() {
47  FW_ASSERT(TYPE_I8 == this->m_dataType);
48  return this->m_val.i8Val;
49 }
50 
51 void PolyType::get(I8& val) {
52  FW_ASSERT(TYPE_I8 == this->m_dataType);
53  val = this->m_val.i8Val;
54 }
55 
57  return (TYPE_I8 == this->m_dataType);
58 }
59 
61  this->m_dataType = TYPE_I8;
62  this->m_val.i8Val = other;
63  return *this;
64 }
65 
66 #if FW_HAS_16_BIT
67 
68 // U16 methods
69 
70 PolyType::PolyType(U16 val) {
71  this->m_dataType = TYPE_U16;
72  this->m_val.u16Val = val;
73 }
74 
75 PolyType::operator U16() {
76  FW_ASSERT(TYPE_U16 == this->m_dataType);
77  return this->m_val.u16Val;
78 }
79 
80 void PolyType::get(U16& val) {
81  FW_ASSERT(TYPE_U16 == this->m_dataType);
82  val = this->m_val.u16Val;
83 }
84 
85 bool PolyType::isU16() {
86  return (TYPE_U16 == this->m_dataType);
87 }
88 
89 PolyType& PolyType::operator=(U16 other) {
90  this->m_dataType = TYPE_U16;
91  this->m_val.u16Val = other;
92  return *this;
93 }
94 
95 // I16 methods
96 
97 PolyType::PolyType(I16 val) {
98  this->m_dataType = TYPE_I16;
99  this->m_val.i16Val = val;
100 }
101 
102 PolyType::operator I16() {
103  FW_ASSERT(TYPE_I16 == this->m_dataType);
104  return this->m_val.i16Val;
105 }
106 
107 void PolyType::get(I16& val) {
108  FW_ASSERT(TYPE_I16 == this->m_dataType);
109  val = this->m_val.i16Val;
110 }
111 
112 bool PolyType::isI16() {
113  return (TYPE_I16 == this->m_dataType);
114 }
115 
116 PolyType& PolyType::operator=(I16 other) {
117  this->m_dataType = TYPE_I16;
118  this->m_val.i16Val = other;
119  return *this;
120 }
121 
122 #endif
123 
124 #if FW_HAS_32_BIT
125 
126 // U32 methods
127 
128 PolyType::PolyType(U32 val) {
129  this->m_dataType = TYPE_U32;
130  this->m_val.u32Val = val;
131 }
132 
133 PolyType::operator U32() {
134  FW_ASSERT(TYPE_U32 == this->m_dataType);
135  return this->m_val.u32Val;
136 }
137 
138 void PolyType::get(U32& val) {
139  FW_ASSERT(TYPE_U32 == this->m_dataType);
140  val = this->m_val.u32Val;
141 }
142 
143 bool PolyType::isU32() {
144  return (TYPE_U32 == this->m_dataType);
145 }
146 
147 PolyType& PolyType::operator=(U32 other) {
148  this->m_dataType = TYPE_U32;
149  this->m_val.u32Val = other;
150  return *this;
151 }
152 
153 // I32 methods
154 
155 PolyType::PolyType(I32 val) {
156  this->m_dataType = TYPE_I32;
157  this->m_val.i32Val = val;
158 }
159 
160 PolyType::operator I32() {
161  FW_ASSERT(TYPE_I32 == this->m_dataType);
162  return this->m_val.i32Val;
163 }
164 
165 void PolyType::get(I32& val) {
166  FW_ASSERT(TYPE_I32 == this->m_dataType);
167  val = this->m_val.i32Val;
168 }
169 
170 bool PolyType::isI32() {
171  return (TYPE_I32 == this->m_dataType);
172 }
173 
174 PolyType& PolyType::operator=(I32 other) {
175  this->m_dataType = TYPE_I32;
176  this->m_val.i32Val = other;
177  return *this;
178 }
179 
180 #endif
181 #if FW_HAS_64_BIT
182 
183 // U64 methods
184 
185 PolyType::PolyType(U64 val) {
186  this->m_dataType = TYPE_U64;
187  this->m_val.u64Val = val;
188 }
189 
190 PolyType::operator U64() {
191  FW_ASSERT(TYPE_U64 == this->m_dataType);
192  return this->m_val.u64Val;
193 }
194 
195 void PolyType::get(U64& val) {
196  FW_ASSERT(TYPE_U64 == this->m_dataType);
197  val = this->m_val.u64Val;
198 }
199 
200 bool PolyType::isU64() {
201  return (TYPE_U64 == this->m_dataType);
202 }
203 
204 PolyType& PolyType::operator=(U64 other) {
205  this->m_dataType = TYPE_U64;
206  this->m_val.u64Val = other;
207  return *this;
208 }
209 
210 // I64 methods
211 
212 PolyType::PolyType(I64 val) {
213  this->m_dataType = TYPE_I64;
214  this->m_val.i64Val = val;
215 }
216 
217 PolyType::operator I64() {
218  FW_ASSERT(TYPE_I64 == this->m_dataType);
219  return this->m_val.i64Val;
220 }
221 
222 void PolyType::get(I64& val) {
223  FW_ASSERT(TYPE_I64 == this->m_dataType);
224  val = this->m_val.i64Val;
225 }
226 
227 bool PolyType::isI64() {
228  return (TYPE_I64 == this->m_dataType);
229 }
230 
231 PolyType& PolyType::operator=(I64 other) {
232  this->m_dataType = TYPE_I64;
233  this->m_val.i64Val = other;
234  return *this;
235 }
236 
237 #endif
238 
239 #if FW_HAS_F64
240 
241 PolyType::PolyType(F64 val) {
242  this->m_dataType = TYPE_F64;
243  this->m_val.f64Val = val;
244 }
245 
246 PolyType::operator F64() {
247  FW_ASSERT(TYPE_F64 == this->m_dataType);
248  return this->m_val.f64Val;
249 }
250 
251 void PolyType::get(F64& val) {
252  FW_ASSERT(TYPE_F64 == this->m_dataType);
253  val = this->m_val.f64Val;
254 }
255 
256 bool PolyType::isF64() {
257  return (TYPE_F64 == this->m_dataType);
258 }
259 
260 PolyType& PolyType::operator=(F64 other) {
261  this->m_dataType = TYPE_F64;
262  this->m_val.f64Val = other;
263  return *this;
264 }
265 
266 #endif
268  this->m_dataType = TYPE_F32;
269  this->m_val.f32Val = val;
270 }
271 
272 PolyType::operator F32() {
273  FW_ASSERT(TYPE_F32 == this->m_dataType);
274  return this->m_val.f32Val;
275 }
276 
277 void PolyType::get(F32& val) {
278  FW_ASSERT(TYPE_F32 == this->m_dataType);
279  val = this->m_val.f32Val;
280 }
281 
283  return (TYPE_F32 == this->m_dataType);
284 }
285 
287  this->m_dataType = TYPE_F32;
288  this->m_val.f32Val = other;
289  return *this;
290 }
291 
293  this->m_dataType = TYPE_BOOL;
294  this->m_val.boolVal = val;
295 }
296 
297 PolyType::operator bool() {
298  FW_ASSERT(TYPE_BOOL == this->m_dataType);
299  return this->m_val.boolVal;
300 }
301 
302 void PolyType::get(bool& val) {
303  FW_ASSERT(TYPE_BOOL == this->m_dataType);
304  val = this->m_val.boolVal;
305 }
306 
308  return (TYPE_BOOL == this->m_dataType);
309 }
310 
312  this->m_dataType = TYPE_BOOL;
313  this->m_val.boolVal = other;
314  return *this;
315 }
316 
317 PolyType::PolyType(void* val) {
318  this->m_dataType = TYPE_PTR;
319  this->m_val.ptrVal = val;
320 }
321 
322 PolyType::operator void*() {
323  FW_ASSERT(TYPE_PTR == this->m_dataType);
324  return this->m_val.ptrVal;
325 }
326 
327 void PolyType::get(void*& val) {
328  FW_ASSERT(TYPE_PTR == this->m_dataType);
329  val = this->m_val.ptrVal;
330 }
331 
333  return (TYPE_PTR == this->m_dataType);
334 }
335 
337  this->m_dataType = TYPE_PTR;
338  this->m_val.ptrVal = other;
339  return *this;
340 }
341 
343  this->m_dataType = original.m_dataType;
344  this->m_val = original.m_val;
345 }
346 
348 
350  this->m_dataType = src.m_dataType;
351  this->m_val = src.m_val;
352  return *this;
353 }
354 
355 bool PolyType::operator!=(const PolyType& other) const {
356  return !operator==(other);
357 }
358 
359 bool PolyType::operator==(const PolyType& other) const {
360  // if type doesn't match, not equal
361  if (this->m_dataType != other.m_dataType) {
362  return false;
363  } else {
364  // check based on type
365  bool valIsEqual = false;
366  switch (this->m_dataType) {
367  case TYPE_U8:
368  valIsEqual = (this->m_val.u8Val == other.m_val.u8Val);
369  break;
370  case TYPE_I8:
371  valIsEqual = (this->m_val.i8Val == other.m_val.i8Val);
372  break;
373 #if FW_HAS_16_BIT
374  case TYPE_U16:
375  valIsEqual = (this->m_val.u16Val == other.m_val.u16Val);
376  break;
377  case TYPE_I16:
378  valIsEqual = (this->m_val.i16Val == other.m_val.i16Val);
379  break;
380 #endif
381 #if FW_HAS_32_BIT
382  case TYPE_U32:
383  valIsEqual = (this->m_val.u32Val == other.m_val.u32Val);
384  break;
385  case TYPE_I32:
386  valIsEqual = (this->m_val.i32Val == other.m_val.i32Val);
387  break;
388 #endif
389 #if FW_HAS_64_BIT
390  case TYPE_U64:
391  valIsEqual = (this->m_val.u64Val == other.m_val.u64Val);
392  break;
393  case TYPE_I64:
394  valIsEqual = (this->m_val.i64Val == other.m_val.i64Val);
395  break;
396 #endif
397  case TYPE_BOOL:
398  valIsEqual = (this->m_val.boolVal == other.m_val.boolVal);
399  break;
400  case TYPE_PTR:
401  valIsEqual = (this->m_val.ptrVal == other.m_val.ptrVal);
402  break;
403 #if FW_HAS_F64
404  case TYPE_F64: // fall through, shouldn't test floating point
405 #endif
406  case TYPE_F32: // fall through, shouldn't test floating point
407  case TYPE_NOTYPE:
408  valIsEqual = false;
409  break;
410  default:
411  FW_ASSERT(0, static_cast<FwAssertArgType>(this->m_dataType));
412  return false; // for compiler
413  }
414  return valIsEqual;
415  }
416 }
417 
418 bool PolyType::operator<(const PolyType& other) const {
419  // if type doesn't match, not equal
420  if (this->m_dataType != other.m_dataType) {
421  return false;
422  } else {
423  // check based on type
424  bool result = false;
425  switch (this->m_dataType) {
426  case TYPE_U8:
427  result = (this->m_val.u8Val < other.m_val.u8Val);
428  break;
429  case TYPE_I8:
430  result = (this->m_val.i8Val < other.m_val.i8Val);
431  break;
432 #if FW_HAS_16_BIT
433  case TYPE_U16:
434  result = (this->m_val.u16Val < other.m_val.u16Val);
435  break;
436  case TYPE_I16:
437  result = (this->m_val.i16Val < other.m_val.i16Val);
438  break;
439 #endif
440 #if FW_HAS_32_BIT
441  case TYPE_U32:
442  result = (this->m_val.u32Val < other.m_val.u32Val);
443  break;
444  case TYPE_I32:
445  result = (this->m_val.i32Val < other.m_val.i32Val);
446  break;
447 #endif
448 #if FW_HAS_64_BIT
449  case TYPE_U64:
450  result = (this->m_val.u64Val < other.m_val.u64Val);
451  break;
452  case TYPE_I64:
453  result = (this->m_val.i64Val < other.m_val.i64Val);
454  break;
455 #endif
456 #if FW_HAS_F64
457  case TYPE_F64:
458  result = (this->m_val.f64Val < other.m_val.f64Val);
459  break;
460 #endif
461  case TYPE_F32:
462  result = (this->m_val.f32Val < other.m_val.f32Val);
463  break;
464  case TYPE_PTR:
465  result = (this->m_val.ptrVal < other.m_val.ptrVal);
466  break;
467  case TYPE_BOOL: // fall through, shouldn't test bool
468  case TYPE_NOTYPE:
469  result = false;
470  break;
471  default:
472  FW_ASSERT(0, static_cast<FwAssertArgType>(this->m_dataType));
473  return false; // for compiler
474  }
475  return result;
476  }
477 }
478 
479 bool PolyType::operator>(const PolyType& other) const {
480  return other.operator<(*this);
481 }
482 
483 bool PolyType::operator>=(const PolyType& other) const {
484  return (this->operator>(other)) || (this->operator==(other));
485 }
486 
487 bool PolyType::operator<=(const PolyType& other) const {
488  return (this->operator<(other)) || (this->operator==(other));
489 }
490 
492  // store type
493  SerializeStatus stat = buffer.serialize(static_cast<FwEnumStoreType>(this->m_dataType));
494  if (stat != FW_SERIALIZE_OK) {
495  return stat;
496  }
497 
498  // switch on type
499  switch (this->m_dataType) {
500  case TYPE_U8:
501  stat = buffer.serialize(this->m_val.u8Val);
502  break;
503  case TYPE_I8:
504  stat = buffer.serialize(this->m_val.i8Val);
505  break;
506 #if FW_HAS_16_BIT
507  case TYPE_U16:
508  stat = buffer.serialize(this->m_val.u16Val);
509  break;
510  case TYPE_I16:
511  stat = buffer.serialize(this->m_val.i16Val);
512  break;
513 #endif
514 #if FW_HAS_32_BIT
515  case TYPE_U32:
516  stat = buffer.serialize(this->m_val.u32Val);
517  break;
518  case TYPE_I32:
519  stat = buffer.serialize(this->m_val.i32Val);
520  break;
521 #endif
522 #if FW_HAS_64_BIT
523  case TYPE_U64:
524  stat = buffer.serialize(this->m_val.u64Val);
525  break;
526  case TYPE_I64:
527  stat = buffer.serialize(this->m_val.i64Val);
528  break;
529 #endif
530 #if FW_HAS_F64
531  case TYPE_F64:
532  stat = buffer.serialize(this->m_val.f64Val);
533  break;
534 #endif
535  case TYPE_F32:
536  stat = buffer.serialize(this->m_val.f32Val);
537  break;
538  case TYPE_BOOL:
539  stat = buffer.serialize(this->m_val.boolVal);
540  break;
541  case TYPE_PTR:
542  stat = buffer.serialize(this->m_val.ptrVal);
543  break;
544  default:
546  break;
547  }
548 
549  return stat;
550 }
551 
553  // get type
554  FwEnumStoreType des;
555  SerializeStatus stat = buffer.deserialize(des);
556 
557  if (stat != FW_SERIALIZE_OK) {
558  return stat;
559  } else {
560  this->m_dataType = static_cast<Type>(des);
561  // switch on type
562  switch (this->m_dataType) {
563  case TYPE_U8:
564  return buffer.deserialize(this->m_val.u8Val);
565  case TYPE_I8:
566  return buffer.deserialize(this->m_val.i8Val);
567 #if FW_HAS_16_BIT
568  case TYPE_U16:
569  return buffer.deserialize(this->m_val.u16Val);
570  case TYPE_I16:
571  return buffer.deserialize(this->m_val.i16Val);
572 #endif
573 #if FW_HAS_32_BIT
574  case TYPE_U32:
575  return buffer.deserialize(this->m_val.u32Val);
576  case TYPE_I32:
577  return buffer.deserialize(this->m_val.i32Val);
578 #endif
579 #if FW_HAS_64_BIT
580  case TYPE_U64:
581  return buffer.deserialize(this->m_val.u64Val);
582  case TYPE_I64:
583  return buffer.deserialize(this->m_val.i64Val);
584 #endif
585 #if FW_HAS_F64
586  case TYPE_F64:
587  return buffer.deserialize(this->m_val.f64Val);
588 #endif
589  case TYPE_F32:
590  return buffer.deserialize(this->m_val.f32Val);
591  case TYPE_BOOL:
592  return buffer.deserialize(this->m_val.boolVal);
593  case TYPE_PTR:
594  return buffer.deserialize(this->m_val.ptrVal);
595  default:
597  }
598  }
599 }
600 
601 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
602 
603 void PolyType::toString(StringBase& dest) const {
604  this->toString(dest, false);
605 }
606 
607 void PolyType::toString(StringBase& dest, bool append) const {
608  char valString[80];
609  switch (this->m_dataType) {
610  case TYPE_U8:
611  (void)snprintf(valString, sizeof(valString), "%" PRIu8 " ", this->m_val.u8Val);
612  break;
613  case TYPE_I8:
614  (void)snprintf(valString, sizeof(valString), "%" PRId8 " ", this->m_val.i8Val);
615  break;
616 #if FW_HAS_16_BIT
617  case TYPE_U16:
618  (void)snprintf(valString, sizeof(valString), "%" PRIu16 " ", this->m_val.u16Val);
619  break;
620  case TYPE_I16:
621  (void)snprintf(valString, sizeof(valString), "%" PRId16 " ", this->m_val.i16Val);
622  break;
623 #endif
624 #if FW_HAS_32_BIT
625  case TYPE_U32:
626  (void)snprintf(valString, sizeof(valString), "%" PRIu32 " ", this->m_val.u32Val);
627  break;
628  case TYPE_I32:
629  (void)snprintf(valString, sizeof(valString), "%" PRId32 " ", this->m_val.i32Val);
630  break;
631 #endif
632 #if FW_HAS_64_BIT
633  case TYPE_U64:
634  (void)snprintf(valString, sizeof(valString), "%" PRIu64 " ", this->m_val.u64Val);
635  break;
636  case TYPE_I64:
637  (void)snprintf(valString, sizeof(valString), "%" PRId64 " ", this->m_val.i64Val);
638  break;
639 #endif
640 #if FW_HAS_F64
641  case TYPE_F64:
642  (void)snprintf(valString, sizeof(valString), "%lg ", this->m_val.f64Val);
643  break;
644 #endif
645  case TYPE_F32:
646  (void)snprintf(valString, sizeof(valString), "%g ", this->m_val.f32Val);
647  break;
648  case TYPE_BOOL:
649  (void)snprintf(valString, sizeof(valString), "%s ", this->m_val.boolVal ? "T" : "F");
650  break;
651  case TYPE_PTR:
652  (void)snprintf(valString, sizeof(valString), "%p ", this->m_val.ptrVal);
653  break;
654  default:
655  (void)snprintf(valString, sizeof(valString), "%s ", "NT");
656  break;
657  }
658 
659  // NULL terminate
660  valString[sizeof(valString) - 1] = 0;
661 
662  if (append) {
663  dest += valString;
664  } else {
665  dest = valString;
666  }
667 }
668 
669 #endif
670 } // namespace Fw
#define FW_ASSERT(...)
Definition: Assert.hpp:14
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:25
float F32
32-bit floating point
Definition: BasicTypes.h:45
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
I32 FwEnumStoreType
Definition: FpConfig.h:51
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
bool isBool()
bool checker
Definition: PolyType.cpp:307
bool operator<(const PolyType &other) const
PolyType operator<.
Definition: PolyType.cpp:418
bool operator>=(const PolyType &other) const
PolyType operator>=.
Definition: PolyType.cpp:483
bool isPtr()
void* checker
Definition: PolyType.cpp:332
bool operator>(const PolyType &other) const
PolyType operator>
Definition: PolyType.cpp:479
bool operator==(const PolyType &other) const
PolyType operator==.
Definition: PolyType.cpp:359
PolyType()
default constructor
Definition: PolyType.cpp:10
SerializeStatus deserialize(SerializeBufferBase &buffer)
Deserialize function.
Definition: PolyType.cpp:552
SerializeStatus serialize(SerializeBufferBase &buffer) const
Serialize function.
Definition: PolyType.cpp:491
bool operator<=(const PolyType &other) const
PolyType operator<=.
Definition: PolyType.cpp:487
bool operator!=(const PolyType &other) const
PolyType operator!=.
Definition: PolyType.cpp:355
PolyType & operator=(U8 val)
U8 operator=.
Definition: PolyType.cpp:33
bool isU8()
U8 checker.
Definition: PolyType.cpp:29
virtual ~PolyType()
destructor
Definition: PolyType.cpp:347
bool isI8()
I8 checker.
Definition: PolyType.cpp:56
bool isF32()
F32 checker.
Definition: PolyType.cpp:282
void get(U8 &val)
U8 accessor.
Definition: PolyType.cpp:24
forward declaration
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
SerializeStatus
forward declaration for string
@ FW_DESERIALIZE_FORMAT_ERROR
Deserialization data had incorrect values (unexpected data types)
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_SERIALIZE_FORMAT_ERROR
Data was the wrong format (e.g. wrong packet type)
#define U64(C)
Definition: sha.h:176