标签:纯粹 cli 影响 摘要 产生 基础 等等 span 内存数据
这篇教程提供了一个面向 C++ 程序员关于 protocol buffers 的基础介绍。通过创建一个简单的示例应用程序,它将向我们展示:
我们接下来要使用的例子是一个非常简单的"地址簿"应用程序,它能从文件中读取联系人详细信息。地址簿中的每一个人都有一个名字、ID、邮件地址和联系电话。
12:3:-23:67。虽然它需要编写一次性的编码和解码代码且解码需要耗费一点运行时成本,但这是一种简单灵活的方法。这最适合编码非常简单的数据。示例代码被包含于源代码包,位于“examples”文件夹。可在这里下载代码。
package tutorial;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;}message AddressBook {repeated Person person = 1;}如你所见,其语法类似于 C++ 或 Java。我们开始看看文件的每一部分内容做了什么。
.proto 文件以一个 package 声明开始,这可以避免不同项目的命名冲突。在 C++,你生成的类会被置于与package 名字一样的命名空间。
required:必须提供该字段的值,否则消息会被认为是“未初始化的”uninitialized。如果 libprotobuf 以调试模式编译,序列化未初始化的消息将引起一个断言失败。以优化形式构建,将会跳过检查,并且无论如何都会写入该消息。然而,解析未初始化的消息总是会失败(通过 parse 方法返回 false)。除此之外,一个 required 字段的表现与 optional 字段完全一样。optional:字段可能会被设置,也可能不会。如果一个 optional 字段没被设置,它将使用默认值。对于简单类型,你可以指定你自己的默认值,正如例子中我们对电话号码的 type 一样,否则使用系统默认值:数字类型为 0、字符串为空字符串、布尔值为 false。对于嵌套消息,默认值总为消息的“默认实例”或“原型”,它的所有字段都没被设置。调用 accessor 来获取一个没有显式设置的optional(或 required) 字段的值总是返回字段的默认值。repeated:字段可以重复任意次数(包括 0 次)。repeated 值的顺序会被保存于 protocol buffer。可以将 repeated 字段想象为动态大小的数组。$SRC_DIR 相同),以及你的 .proto 路径。在此示例中:因为你想要 C++ 的类,所以你使用了 --cpp_out 选项——也为其他支持的语言提供了类似选项。
// nameinline bool has_name() const;inline void clear_name();inline const ::std::string& name() const;inline void set_name(const ::std::string& value);inline void set_name(const char* value);inline ::std::string* mutable_name();// idinline bool has_id() const;inline void clear_id();inline int32_t id() const;inline void set_id(int32_t value);// emailinline bool has_email() const;inline void clear_email();inline const ::std::string& email() const;inline void set_email(const ::std::string& value);inline void set_email(const char* value);inline ::std::string* mutable_email();// phoneinline int phone_size() const;inline void clear_phone();inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const;inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phone();inline const ::tutorial::Person_PhoneNumber& phone(int index) const;inline ::tutorial::Person_PhoneNumber* mutable_phone(int index);inline ::tutorial::Person_PhoneNumber* add_phone();repeated 字段也有一些特殊的方法——如果你看看 repeated 的 phone 字段的方法,你可以看到:
repeated 字段的 _size(也就是说,与 Person 相关的电话号码的个数)repeated 标量类型有一个 add_ 方法,用于传入新的值)为了获取 protocol 编译器为所有字段定义生成的方法的信息,可以查看 C++ generated code reference。
所有的消息方法都包含了许多别的方法,用于检查和操作整个消息,包括:
bool IsInitialized() const; :检查是否所有 required 字段已经被设置。string DebugString() const; :返回人类可读的消息表示,对调试特别有用。void CopyFrom(const Person& from);:使用给定的值重写消息。void Clear();:清除所有元素为空的状态。最后,所有 protocol buffer 类都有读写你选定类型消息的方法,这些方法使用了特定的 protocol buffer 二进制格式。这些方法包括:
bool SerializeToString(string* output) const;:序列化消息并将消息字节数据存储在给定的字符串中。注意,字节数据是二进制格式的,而不是文本格式;我们只使用 string 类作为合适的容器。bool ParseFromString(const string& data);:从给定的字符创解析消息。bool SerializeToOstream(ostream* output) const;:将消息写到给定的 C++ ostream。bool ParseFromIstream(istream* input);:从给定的 C++ istream 解析消息。这些只是两个用于解析和序列化的选择。再次说明,可以查看 Message API reference 完整的列表。
标签:纯粹 cli 影响 摘要 产生 基础 等等 span 内存数据
原文地址:http://www.cnblogs.com/lidoudou/p/6962948.html