我的目的是构建一个配置文件。所以我基本上必须以某种方式表示命令包结构。
当实际数据(二进制)到来时,我想将数据包与此配置文件进行比较,然后对其进行处理(将数据转换为 CSV 格式)。
所以有不同的命令类型。
因此,每次数据包到来时,我都必须获取其操作码并使用配置文件进行检查,并返回代表该数据包格式的适当命令格式。
命令格式可能如下所示:
操作码 - 1 字节 - 整数
命令 - 4 字节 - 字符串
...
所有命令不具有相同数量的字段或相同的格式。
我想检索所有这些详细信息。我可以用 XML 表示它并使用 libxml2 等库解析它。
下面给出了示例 XML 格式:
<cmd type="Multiplication">
<field name="opcode" type="string" bytes="4"/>
<field name="Multiplicand" type="number" bytes="2"/>
<field name="Multiplier" type="number" bytes="2"/>
</cmd>
但是这种方法相当慢。
我的想法是以某种方式在结构中表示命令包格式。但由于 C/C++ 不是一种反射语言,因此无法知道结构成员,并且每个结构(命令)需要一个函数来解析它。
请建议一种存储格式的方法,以便一个通用函数只需查看此格式即可解析二进制数据。
- 语言可以是 C 或 C++。
- 性能是重中之重,因此不鼓励使用 XML 和类似类型。
- 首选内存数据结构。
非常感谢任何帮助。
最佳答案
我认为最好的选择是将文件表示为正确命令类型的变体
的集合。例如,假设您有三个命令选项:
struct Constant {
short value;
};
struct UnaryOperation {
unsigned char opcode;
short value;
};
struct BinaryOperation {
unsigned char opcode;
short value1;
short value2;
};
表示未知命令。如果您有未知命令,可以将其表示为三种类型的变体
:
using Command = std::variant<Constant, UnaryOperation, BinaryOperation>;
根据命令类型应用函数。假设每个命令都有不同的函数:
short eval(Constant c) {
return c.value;
}
short eval(UnaryOperation u) {
switch(u.opcode) {
// stuff
}
}
short eval(BinaryOperation b) {
switch(b.opcode) {
// stuff
}
}
我们可以使用std::visit
来评估任意Command
:
short evaluate_command(Command const& command) {
short output;
// This calls the right overload automatically
std::visit(command, [&](auto cmd) { output = eval(cmd); });
return output;
}
解析命令。
我们可以从定义它的任何类型自动创建一个 std::variant
。这意味着,如果您提供一种方法来根据文件找出命令的内容,那就很容易做到。
enum class OpType : unsigned char {
ConstantOp, UnaryOp, BinaryOp
};
// Command can be automatically constructed from a Constant, a UnaryOperation, or a BinaryOperation
Command readFromStream(std::istream& i) {
OpType type;
unsigned char op;
short value, value2;
// Read the type of the operation
i >> (unsigned char&)type;
//Return either a Constant, a UnaryOperation, or a BinaryOperation
switch(type) {
case OpType::ConstantOp: {
i >> value;
return Constant{value};
}
case OpType::UnaryOp: {
i >> op >> value;
return UnaryOperation{op, value};
}
case OpType::BinaryOp {
i >> op >> value >> value2;
return BinaryOperation{op, value, value2};
}
}
}
关于c++ - 表示命令包格式的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100825/