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