0

For example, we define a Person message.

message Person {
   int32 age = 1;
   string name = 2;
};

I want to use this message in the following way:

Person p;
assert(p.empty());
p.set_age(10);
assert(p.size() == 1);  // True
p.Clear()
assert(p.empty()) // True

Is this possible?

PeopleMoutainPeopleSea
  • 1,492
  • 1
  • 15
  • 24

1 Answers1

0

With regard to your desire to have a empty() function you can use the has() function for optional fields in proto3. This has been added as of version 3.15. An example has given in this answer.

Bart
  • 1,405
  • 6
  • 32
  • Ok, So the only way is to iterate all the fields names and check that all `has_xyz` method returns false. – PeopleMoutainPeopleSea Jun 01 '21 at 05:34
  • Likely yes, there might be a short cut for messages nested in other messages. The nested message have a has_ function in the parent I from the top of my head I would say return true if at least one variable is set – Bart Jun 01 '21 at 07:49
  • Thanks. In my case the message is not a field in another message. Maybe I can use DebugString().empty() instead. – PeopleMoutainPeopleSea Jun 07 '21 at 06:46