我有以下结构(以及许多其他结构),我想通过套接字发送它。
struct Position
{
float x; // position selon x
float y; // position selon y
float theta; // orientation % z
Position (float x_, float y_, float theta_): x(x_), y(y_), theta(theta_){}
Position(){}
};
但首先,出于解码目的,我考虑使用 msgpack用于编码和解码的库,因为它可以大大简化我的任务。
唯一的问题是我不知道我到底该怎么做,这里和其他地方提供的几乎所有示例都只是尝试打包传统缓冲区( vector 、列表...),但我找不到结构打包!
那么,可以做到吗?如何做到?
PS:结构示例被简化了,我得到了一些更复杂的。
谢谢。
最佳答案
是的,这是可行的。您可以使用 MSGPACK_DEFINE_MAP
或 MSGPACK_DEFINE_ARRAY
:
struct Position
{
float x; // position selon x
float y; // position selon y
float theta; // orientation % z
Position (float x_, float y_, float theta_): x(x_), y(y_), theta(theta_){}
Position(){}
MSGPACK_DEFINE_MAP(x, y, theta)
};
此宏将生成所需的适配器函数。
不同之处在于数组只会列出您的成员(因此更紧凑),而映射将包含结构成员作为键值对。
来源:https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor#defining-custom-adaptors
关于c++ - msgpack 打包结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322145/