Protobuf:了解 proto 文件的编译输出

Protobuf: Understanding the compiled output of proto files

**大家好, 我是 protobuf 的新手。我试图了解这里的基础知识。 我在目录 /path/to/Directory/:

中创建了示例原型文件 Test.proto
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;
}

并用

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

这创建了 2 个文件 Test.pb.hTest.pb.cc。现在我可以看到 class Person 中有不同的功能。让我们只取函数 (派生自 Test.proto 文件的第 required string name = 1; 行)

现在编译器完成了它的工作并提供了这些不同的功能:

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);

现在我的问题是:在哪里可以找到每个函数的描述以及它们的作用?

起点是here (static code) and here(生成的代码)。后者包括您在 "in specific" 中询问的内容的概述 - 搜索:string* mutable_foo()string* release_foo()(注意它们对于 proto2 和 proto3 是重复的)。

根据 C++ 生成代码的 documentation

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.

您可以在同一页面中找到其余功能的说明。