c++ - Protobug 消息中的消息标识符
问题描述
有没有办法获得类似 protobuf 消息的 ID 的东西。
例如我有:
syntax = "proto3";
package protobuf;
message parameters_t
{
string audio_device = 1;
}
message master_t
{
enum type_t
{
unknown = 0;
login_rsp = 1;
}
type_t type = 1;
}
我想在 C++ 中做的是通过线路发送大小,然后是消息的一些 ID 和缓冲区。
例如paramters_t
是 ID 0(静态可访问)并且master_t
是 ID 1,所以我可以在我的代码中做:
if (id == protobuf::master_t::id) {
...
}
else if (id == protobuf::paramters_t::id) {
...
}
有没有办法在不手动分配值的情况下实现这一点?我希望以某种方式将其设置在原型文件中。它可以是我自己定义的常数,我不在乎。
解决方案
一种典型的方法是只定义一个枚举:
enum MsgId {
MSGID_MASTER = 0;
MSGID_PARAMETERS = 1;
}
然后,您可以像常量一样在 C++ 中访问它们。
推荐阅读
- node.js - axios 获取请求挂起,无错误,catch 未触发
- react-native - 如何进一步调试 cli react-native bundle 命令?命令挂起,没有错误
- python - 如何在文本输入框内添加滚动条
- typescript - 通用方法的打字稿接口
- terraform - 如何针对单个 ECS 服务附加两个目标组
- jenkins - 在 GitHub 上重命名 jenkins
- node.js - Heroku - React 应用程序在 fetch() 请求中调用 localhost,而不是我的 Express 后端的 Heroku URL
- python - NaN 是否会干扰 pandas 中的列连接?
- php - 我的联系表格工作正常,直到我添加了附件选项。不再发送消息。知道如何验证附件字段吗?
- python-3.x - 有选择地更新数据框(python)的多级列名称,具有特定文本的列具有特定名称