来自协议缓冲区消息的普通结构 [英] Plain struct from protocol buffer message

查看:57
本文介绍了来自协议缓冲区消息的普通结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 C++ 中的协议缓冲区消息中获取一个简单的结构?即

struct PlainFoo {int32_t 栏;浮动巴兹;};

来自

message Foo {所需的 int32 bar = 1;所需的浮动 baz = 2;}

我通过网络在协议缓冲区中获取了一些数据,我希望能够将其添加到其他类似数据中,并将其存储在紧凑的表示中以供内部使用,而无需基本上复制代码中的消息声明.

我知道我可以使用生成的消息类本身,但它有一堆内部成员,我不想移动.

有问题的消息只有必填字段.

解决方案

我不知道可以生成普通结构的 C++ protobuf 库,但也有可以在 C++ 代码中使用的 C 库.至少 nanopbpbtools 生成简单的结构:

<预>/* 纳米级 */typedef 结构 _Foo {int32_t 栏;浮动巴兹;福;

<预>/* pbtools */结构Foo_t {int32_t 栏;浮动巴兹;};

使用 nanopb,您还可以选择使用 PB_BIND().proto 文件中的标签编号定义连接到您自己的代码中定义的结构.如果您想将代码与接口规范分离,这会很有用,并允许例如添加未序列化的额外字段.

Is there a way to obtain a simple struct from protocol buffers message in C++? i.e.

struct PlainFoo {
  int32_t bar;
  float baz;
};

from

message Foo {
  required int32 bar = 1;
  required float baz = 2;
}

I obtain some data over network in a protocol buffer and I'd like to be able to add it to other similar data and store it in a compact representation for internal use without having to basically copy the message declaration in my code.

I know I can use the generated message class itself, but it has bunch of internal members that I'd prefer not having to move around.

The messages in question only have required fields.

解决方案

I'm not aware of a C++ protobuf library that would generate plain structures, but there are C libraries that can be used in C++ code also. At least nanopb and pbtools generate simple structs:

/* nanopb */
typedef struct _Foo {
    int32_t bar;
    float baz;
} Foo;

/* pbtools */
struct Foo_t {
    int32_t bar;
    float baz;
};

With nanopb, you also have the option of using PB_BIND() to connect the tag number definitions from .proto file to a structure that is defined in your own code. This can be useful if you want to decouple your code from the interface specifications, and allows e.g. adding extra fields that are not serialized.

这篇关于来自协议缓冲区消息的普通结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆