add block.get_field(...), block.set_field(...)
This commit is contained in:
+10
-16
@@ -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");
|
||||
}
|
||||
|
||||
+39
-12
@@ -129,35 +129,54 @@ namespace data {
|
||||
/// @throws std::runtime_exception - field not found
|
||||
/// @param name field name
|
||||
/// @return read-only field reference
|
||||
const Field& requreField(const std::string& name) const;
|
||||
const Field& requireField(const std::string& name) const;
|
||||
|
||||
[[nodiscard]]
|
||||
integer_t getInteger(const ubyte* src, const std::string& name, int index=0) const {
|
||||
return getInteger(src, requireField(name), index);
|
||||
}
|
||||
|
||||
/// @brief Get integer from specified field.
|
||||
/// Types: (i8, i16, i32, i64, char)
|
||||
/// @throws std::runtime_exception - field not found
|
||||
/// @throws std::out_of_range - index is out of range [0, elements-1]
|
||||
/// @param src source buffer
|
||||
/// @param name field name
|
||||
/// @param field target field
|
||||
/// @param index array index
|
||||
/// @return field value
|
||||
[[nodiscard]]
|
||||
integer_t getInteger(const ubyte* src, const std::string& name, int index=0) const;
|
||||
integer_t getInteger(const ubyte* src, const Field& field, int index=0) const;
|
||||
|
||||
[[nodiscard]]
|
||||
number_t getNumber(const ubyte* src, const std::string& name, int index=0) const {
|
||||
return getNumber(src, requireField(name), index);
|
||||
}
|
||||
|
||||
/// @brief Get floating-point number from specified field.
|
||||
/// Types: (f32, f64, i8, i16, i32, i64, char)
|
||||
/// @throws std::runtime_exception - field not found
|
||||
/// @throws std::out_of_range - index is out of range [0, elements-1]
|
||||
/// @param src source buffer
|
||||
/// @param name field name
|
||||
/// @param field target field
|
||||
/// @param index array index
|
||||
/// @return field value
|
||||
[[nodiscard]]
|
||||
number_t getNumber(const ubyte* src, const std::string& name, int index=0) const;
|
||||
number_t getNumber(const ubyte* src, const Field& field, int index=0) const;
|
||||
|
||||
[[nodiscard]]
|
||||
std::string_view getChars(const ubyte* src, const std::string& name) const {
|
||||
return getChars(src, requireField(name));
|
||||
}
|
||||
|
||||
/// @brief Get read-only chars array as string_view.
|
||||
/// @param src source buffer
|
||||
/// @param name field name
|
||||
/// @param field target field
|
||||
[[nodiscard]]
|
||||
std::string_view getChars(const ubyte* src, const std::string& name) const;
|
||||
std::string_view getChars(const ubyte* src, const Field& field) const;
|
||||
|
||||
void setInteger(ubyte* dst, integer_t value, const std::string& name, int index=0) const {
|
||||
setInteger(dst, value, requireField(name), index);
|
||||
}
|
||||
|
||||
/// @brief Set field integer value.
|
||||
/// Types: (i8, i16, i32, i64, f32, f64, char)
|
||||
@@ -165,9 +184,13 @@ namespace data {
|
||||
/// @throws std::out_of_range - index is out of range [0, elements-1]
|
||||
/// @param dst destination buffer
|
||||
/// @param value value
|
||||
/// @param name field name
|
||||
/// @param field target field
|
||||
/// @param index array index
|
||||
void setInteger(ubyte* dst, integer_t value, const std::string& name, int index=0) const;
|
||||
void setInteger(ubyte* dst, integer_t value, const Field& field, int index=0) const;
|
||||
|
||||
void setNumber(ubyte* dst, number_t value, const std::string& name, int index=0) const {
|
||||
setNumber(dst, value, requireField(name), index);
|
||||
}
|
||||
|
||||
/// @brief Set field numeric value.
|
||||
/// Types: (f32, f64)
|
||||
@@ -175,9 +198,9 @@ namespace data {
|
||||
/// @throws std::out_of_range - index is out of range [0, elements-1]
|
||||
/// @param dst destination buffer
|
||||
/// @param value value
|
||||
/// @param name field name
|
||||
/// @param field target field
|
||||
/// @param index array index
|
||||
void setNumber(ubyte* dst, number_t value, const std::string& name, int index=0) const;
|
||||
void setNumber(ubyte* dst, number_t value, const Field& field, int index=0) const;
|
||||
|
||||
/// @brief Replace chars array to given ASCII string
|
||||
/// @throws std::runtime_exception - field not found
|
||||
@@ -187,6 +210,10 @@ namespace data {
|
||||
/// @param name field name
|
||||
/// @return number of written string chars
|
||||
size_t setAscii(ubyte* dst, std::string_view value, const std::string& name) const;
|
||||
|
||||
size_t setUnicode(ubyte* dst, std::string_view value, const std::string& name) const {
|
||||
return setUnicode(dst, value, requireField(name));
|
||||
}
|
||||
|
||||
/// @brief Unicode-safe version of setAscii
|
||||
/// @throws std::runtime_exception - field not found
|
||||
@@ -194,7 +221,7 @@ namespace data {
|
||||
/// @param value utf-8 string
|
||||
/// @param name field name
|
||||
/// @return number of written string chars
|
||||
size_t setUnicode(ubyte* dst, std::string_view value, const std::string& name) const;
|
||||
size_t setUnicode(ubyte* dst, std::string_view value, const Field& field) const;
|
||||
|
||||
/// @return total structure size (bytes)
|
||||
[[nodiscard]] size_t size() const {
|
||||
|
||||
Reference in New Issue
Block a user