3

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?

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217

2 Answers2

2

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.

Mr. Beer
  • 255
  • 1
  • 5
  • 1
    The problem with this definition is that neighbors holds instances and not references. I am looking for a way to represent a reference (like a pointer) to another object. – Agnel Kurian Jul 04 '13 at 16:01
  • 1
    You could introduce a playerIdentifier into player and use that to identify the neighbors. – Bruce Martin Jul 05 '13 at 00:04
2

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.

jpa
  • 10,351
  • 1
  • 28
  • 45