add StructMapper.setChars, getChars

This commit is contained in:
MihailRis
2024-08-29 12:32:41 +03:00
parent c67b867a31
commit b34fddbe94
3 changed files with 29 additions and 1 deletions
+23
View File
@@ -103,6 +103,18 @@ void StructMapping::setNumber(
}
}
void StructMapping::setChars(
ubyte* dst, std::string_view value, const std::string& name
) {
const auto& field = requreField(name);
if (field.type != FieldType::CHAR) {
throw std::runtime_error("'char' field type required");
}
auto ptr = reinterpret_cast<char*>(dst + field.offset);
std::memcpy(ptr, value.data(),
std::min(value.size(), static_cast<std::size_t>(field.elements)));
}
template<typename T>
static T get_int(const ubyte* src) {
return dataio::le2h(*reinterpret_cast<const T*>(src));
@@ -160,3 +172,14 @@ number_t StructMapping::getNumber(
}
throw std::runtime_error("type error");
}
std::string_view StructMapping::getChars(
const ubyte* src, const std::string& name
) const {
const auto& field = requreField(name);
if (field.type != FieldType::CHAR) {
throw std::runtime_error("'char' field type required");
}
auto ptr = src + field.offset;
return std::string_view(reinterpret_cast<const char*>(ptr), field.elements);
}
+2
View File
@@ -57,9 +57,11 @@ namespace data {
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;
std::string_view getChars(const ubyte* src, const std::string& name) 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);
void setChars(ubyte* dst, std::string_view value, const std::string& name);
int size() const {
return totalSize;