2022-08-30 12:07:09 +02:00
|
|
|
#ifndef FSFW_UTIL_DATAWRAPPER_H
|
|
|
|
#define FSFW_UTIL_DATAWRAPPER_H
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "fsfw/serialize.h"
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
struct RawData {
|
2022-08-30 13:24:29 +02:00
|
|
|
const uint8_t* data = nullptr;
|
|
|
|
size_t len = 0;
|
2022-08-30 12:07:09 +02:00
|
|
|
};
|
|
|
|
|
2022-08-30 13:24:29 +02:00
|
|
|
enum DataTypes { NONE, RAW, SERIALIZABLE };
|
2022-08-30 12:07:09 +02:00
|
|
|
|
|
|
|
union DataUnion {
|
|
|
|
RawData raw;
|
2022-08-30 13:24:29 +02:00
|
|
|
SerializeIF* serializable = nullptr;
|
2022-08-30 12:07:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DataWrapper {
|
2022-08-30 13:24:29 +02:00
|
|
|
DataTypes type = DataTypes::NONE;
|
2022-08-30 12:07:09 +02:00
|
|
|
DataUnion dataUnion;
|
|
|
|
using BufPairT = std::pair<const uint8_t*, size_t>;
|
|
|
|
|
|
|
|
[[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;
|
|
|
|
}
|
|
|
|
|
2022-08-30 13:24:29 +02:00
|
|
|
[[nodiscard]] bool isNull() const {
|
|
|
|
if (type == DataTypes::RAW and dataUnion.raw.data == nullptr or
|
|
|
|
(type == DataTypes::SERIALIZABLE and dataUnion.serializable == nullptr) or
|
|
|
|
(type == DataTypes::NONE)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-30 12:07:09 +02:00
|
|
|
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
|