From 20d42add0348941baba7b079f03ba0ff57bfd083 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 30 Aug 2022 12:07:09 +0200 Subject: [PATCH] add new data wrapper helper type --- src/fsfw/util/dataWrapper.h | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/fsfw/util/dataWrapper.h diff --git a/src/fsfw/util/dataWrapper.h b/src/fsfw/util/dataWrapper.h new file mode 100644 index 00000000..f5dab447 --- /dev/null +++ b/src/fsfw/util/dataWrapper.h @@ -0,0 +1,52 @@ +#ifndef FSFW_UTIL_DATAWRAPPER_H +#define FSFW_UTIL_DATAWRAPPER_H + +#include +#include +#include + +#include "fsfw/serialize.h" + +namespace util { + +struct RawData { + const uint8_t* data; + size_t len; +}; + +enum DataTypes { RAW, SERIALIZABLE }; + +union DataUnion { + RawData raw; + SerializeIF* serializable; +}; + +struct DataWrapper { + DataTypes type; + DataUnion dataUnion; + using BufPairT = std::pair; + + [[nodiscard]] size_t getLength() const { + if (type == DataTypes::RAW) { + return dataUnion.raw.len; + } else if (type == DataTypes::SERIALIZABLE and dataUnion.serializable != nullptr) { + return dataUnion.serializable->getSerializedSize(); + } + return 0; + } + + void setRawData(BufPairT bufPair) { + type = DataTypes::RAW; + dataUnion.raw.data = bufPair.first; + dataUnion.raw.len = bufPair.second; + } + + void setSerializable(SerializeIF& serializable) { + type = DataTypes::SERIALIZABLE; + dataUnion.serializable = &serializable; + } +}; + +} // namespace util + +#endif // FSFW_UTIL_DATAWRAPPER_H