protobuf消息引用相同类型的另一个消息 [英] protobuf message holding reference to another message of same type

查看:1931
本文介绍了protobuf消息引用相同类型的另一个消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Player 结构,它保存指向它最近的邻居的指针列表。在C ++中的结构可能如下:

  struct Player {
string handle;
vector< Player *>邻居
};



我想使用protobuf来序列化这个类的实例。我如何写一个消息定义来表示上述结构?

解决方案

protobuf中没有引用的概念。 / p>

因此,最简单的方法是:

 
message Player {
必需string handle = 1;
repeated string neighborHandles = 2;
};

通常,当你完成反序列化时,你会将它们转换为C ++引用。


I have a Player structure which holds a list of pointers to its closest neighbors. The structure might look as follows in C++:

struct Player {
  string handle;
  vector<Player*> neighbors;
};

I want to use protobuf to serialize instances of this class. How would I write a message definition to represent the above structure?

解决方案

There is no concept of "reference" in protobuf.

Therefore the sanest way to do it would be to:

message Player {
  required string handle = 1;
  repeated string neighborHandles = 2;
};

Usually you would then convert them to C++ references when you are done deserializing.

这篇关于protobuf消息引用相同类型的另一个消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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