2

**Hi All, I am new to the protobuf. I am trying to understand the basics here. I have created the sample proto file as Test.proto in directory /path/to/Directory/:

syntax = "proto2";

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }**

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

and compiled it with

 protoc -I=/path/to/Directory/ --cpp_out=/path/to/Directory/ /path/to/Directory/Test.proto

This created 2 files Test.pb.h and Test.pb.cc. Now I can see that there are different functions in the class Person. Let's just take the function (derived from the line required string name = 1; of Test.proto file)

Now the compiler does it's thing and gives these various functions:

bool has_name() const;
void clear_name();
static const int kNameFieldNumber = 1;
const ::std::string& name() const;
void set_name(const ::std::string& value);
void set_name(::std::string&& value);
void set_name(const char* value);
void set_name(const char* value, size_t size);
::std::string* mutable_name();
::std::string* release_name();
void set_allocated_name(::std::string* name);

Now my question is: Where can I find the descriptions of each functions and what they do?

RC0993
  • 898
  • 1
  • 13
  • 44

2 Answers2

3

The starting point is here (static code) and here (generated code). The latter includes overviews of what you ask in your "in specific" - search for: string* mutable_foo() and string* release_foo() (noting that they are duplicated for proto2 vs proto3).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

As per the documentation of C++ Generated Code:

string* mutable_foo(): Returns a pointer to the mutable string object that stores the field's value. If the field was not set prior to the call, then the returned string will be empty (not the default value). After calling this, has_foo() will return true and foo() will return whatever value is written into the given string.

string* release_foo(): Releases the ownership of the field and returns the pointer of the string object. After calling this, caller takes the ownership of the allocated string object, has_foo() will return false, and foo() will return the default value.

You can find the descriptions of the rest of the functions in the same page.

P.W
  • 26,289
  • 6
  • 39
  • 76