add StructMapper read/write methods

This commit is contained in:
MihailRis 2024-08-29 11:57:05 +03:00
parent 2b1db0b075
commit c67b867a31
3 changed files with 224 additions and 1 deletions

View file

@ -8,26 +8,64 @@
namespace data {
enum class FieldType {
I8, I16, I32, I64, F32, F64
I8=0, I16, I32, I64, F32, F64, CHAR,
COUNT
};
inline constexpr int sizeof_type(FieldType type) {
const int sizes[static_cast<int>(FieldType::COUNT)] = {
1, 2, 4, 8, 4, 8, 1
};
return sizes[static_cast<int>(type)];
}
struct Field {
FieldType type;
std::string name;
/// @brief Number of field elements (array size)
int elements;
/// @brief Byte offset of the field
int offset;
/// @brief Byte size of the field
int size;
};
class StructMapping {
int totalSize;
std::vector<Field> fields;
std::unordered_map<std::string, int> indices;
public:
StructMapping(
int totalSize,
std::vector<Field> fields,
std::unordered_map<std::string, int> indices
) : totalSize(totalSize),
fields(std::move(fields)),
indices(std::move(indices))
{}
const Field* getField(const std::string& name) const {
auto found = indices.find(name);
if (found == indices.end()) {
return nullptr;
}
return &fields.at(found->second);
}
const Field& requreField(const std::string& name) const;
integer_t getInteger(const ubyte* src, const std::string& name, int index=0) const;
number_t getNumber(const ubyte* src, const std::string& name, int index=0) const;
void setInteger(ubyte* dst, integer_t value, const std::string& name, int index=0);
void setNumber(ubyte* dst, number_t value, const std::string& name, int index=0);
int size() const {
return totalSize;
}
static StructMapping create(const std::vector<Field>& fields);
};
class StructAccess {