add block.get_field(...), block.set_field(...)

This commit is contained in:
MihailRis
2024-09-30 18:55:01 +03:00
parent 8d89e06ac5
commit fc99343fb5
5 changed files with 169 additions and 28 deletions
+10 -16
View File
@@ -183,7 +183,7 @@ std::vector<FieldIncapatibility> StructLayout::checkCompatibility(
return report;
}
const Field& StructLayout::requreField(const std::string& name) const {
const Field& StructLayout::requireField(const std::string& name) const {
auto found = indices.find(name);
if (found == indices.end()) {
throw std::runtime_error("field '"+name+"' does not exist");
@@ -200,9 +200,8 @@ static void set_int(ubyte* dst, integer_t value) {
}
void StructLayout::setInteger(
ubyte* dst, integer_t value, const std::string& name, int index
ubyte* dst, integer_t value, const Field& field, int index
) const {
const auto& field = requreField(name);
if (index < 0 || index >= field.elements) {
throw std::out_of_range(
"index out of bounds [0, "+std::to_string(field.elements)+"]");
@@ -216,7 +215,7 @@ void StructLayout::setInteger(
case FieldType::CHAR: set_int<int8_t>(ptr, value); break;
case FieldType::F32:
case FieldType::F64:
setNumber(dst, static_cast<number_t>(value), name, index);
setNumber(dst, static_cast<number_t>(value), field, index);
break;
default:
throw std::runtime_error("type error");
@@ -224,9 +223,8 @@ void StructLayout::setInteger(
}
void StructLayout::setNumber(
ubyte* dst, number_t value, const std::string& name, int index
ubyte* dst, number_t value, const Field& field, int index
) const {
const auto& field = requreField(name);
if (index < 0 || index >= field.elements) {
throw std::out_of_range(
"index out of bounds [0, "+std::to_string(field.elements)+"]");
@@ -255,7 +253,7 @@ void StructLayout::setNumber(
size_t StructLayout::setAscii(
ubyte* dst, std::string_view value, const std::string& name
) const {
const auto& field = requreField(name);
const auto& field = requireField(name);
if (field.type != FieldType::CHAR) {
throw std::runtime_error("'char' field type required");
}
@@ -266,9 +264,8 @@ size_t StructLayout::setAscii(
}
size_t StructLayout::setUnicode(
ubyte* dst, std::string_view value, const std::string& name
ubyte* dst, std::string_view value, const Field& field
) const {
const auto& field = requreField(name);
if (field.type != FieldType::CHAR) {
throw std::runtime_error("'char' field type required");
}
@@ -285,9 +282,8 @@ static T get_int(const ubyte* src) {
}
integer_t StructLayout::getInteger(
const ubyte* src, const std::string& name, int index
const ubyte* src, const Field& field, int index
) const {
const auto& field = requreField(name);
if (index < 0 || index >= field.elements) {
throw std::out_of_range(
"index out of bounds [0, "+std::to_string(field.elements)+"]");
@@ -305,9 +301,8 @@ integer_t StructLayout::getInteger(
}
number_t StructLayout::getNumber(
const ubyte* src, const std::string& name, int index
const ubyte* src, const Field& field, int index
) const {
const auto& field = requreField(name);
if (index < 0 || index >= field.elements) {
throw std::out_of_range(
"index out of bounds [0, "+std::to_string(field.elements)+"]");
@@ -331,16 +326,15 @@ number_t StructLayout::getNumber(
case FieldType::I32:
case FieldType::I64:
case FieldType::CHAR:
return getInteger(src, name, index);
return getInteger(src, field, index);
default:
throw std::runtime_error("type error");
}
}
std::string_view StructLayout::getChars(
const ubyte* src, const std::string& name
const ubyte* src, const Field& field
) const {
const auto& field = requreField(name);
if (field.type != FieldType::CHAR) {
throw std::runtime_error("'char' field type required");
}