Agnel Kurian
Agnel Kurian

Reputation: 59476

protobuf message holding reference to another message of same type

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?

Upvotes: 3

Views: 3038

Answers (2)

jpa
jpa

Reputation: 12176

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.

Upvotes: 2

Mr. Beer
Mr. Beer

Reputation: 255

I think this would do the trick:

message Player
{
  required string handle = 1;

  repeated Player neighbors = 2;
}

I compiled the definition with protobuf-c and it seems to be working.

Upvotes: 2

Related Questions