当前位置: 首页 > news >正文

protobuf的应用

1.版本和引用

syntax = "proto3";    // proto2
package tutorial;     // package类似C++命名空间
// 可以引用本地的,也可以引用include里面的
import "google/protobuf/timestamp.proto";   // 已经写好的proto文件是可以引用

我们版本选择proto3  引用的是谷歌的protobuf包

2.如何编译

//protoc -I=/路径1 --cpp_out=./路径2 /路径1/addressbook.proto
//路径1为.proto所在的路径
//路径2为.cc和.h生成的位置
//将指定proto文件生成.pb.cc和.pb.h
//protoc -I=./ --cpp_out=./ addressbook.proto
//将对应目录的所有proto文件生成.pb.cc和.pb.h
//protoc -I=./ --cpp_out=./ *.proto

我们通过上述编译代码可以生成.pb.cc和.pb.h的文件 提供给我们c/c++开发语言使用使用

3.版本号选择

// [START java_declaration]
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
// [END csharp_declaration]

我们可以选择java和C#的版本来使用protobuf包

4.主体内容

// 通讯录
// [START messages]  
message Person {    // message类似C++的class
  string name   = 1;  // 名字
  int32 id      = 2;  // Unique ID number for this person. 每个人的唯一id
  string email  = 3;

  enum PhoneType {    // enum 枚举类型
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;    // 字符串 电话号码
    PhoneType type = 2;   //
  }

  repeated PhoneNumber phones = 4;    // 重复0~多个,一个人有多个电话

  google.protobuf.Timestamp last_updated = 5; // import "google/protobuf/timestamp.proto"
}

// Our address book file is just one of these.
message AddressBook {
  repeated Person people = 1;   // 电话簿有多人的电话
}
// [END messages]

类似于C++创建类的方法

5.代码案例

// See README.txt for information and build instructions.

#include <ctime>
#include <fstream>
#include <google/protobuf/util/time_util.h>
#include <iostream>
#include <string>

#include "addressbook.pb.h"

using namespace std;

using google::protobuf::util::TimeUtil;


// 添加人联系
// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
  cout << "Enter person ID number: ";
  int id;
  cin >> id;
  person->set_id(id);
  cin.ignore(256, '\n');

  cout << "Enter name: ";
  getline(cin, *person->mutable_name());

  cout << "Enter email address (blank for none): ";
  string email;
  getline(cin, email);
  if (!email.empty()) {
    person->set_email(email);
  }

  while (true) {
    cout << "Enter a phone number (or leave blank to finish): ";
    string number;
    getline(cin, number);
    if (number.empty()) {
      break;
    }

    tutorial::Person::PhoneNumber* phone_number = person->add_phones();
    phone_number->set_number(number);

    cout << "Is this a mobile, home, or work phone? ";
    string type;
    getline(cin, type);
    if (type == "mobile") {
      phone_number->set_type(tutorial::Person::MOBILE);
    } else if (type == "home") {
      phone_number->set_type(tutorial::Person::HOME);
    } else if (type == "work") {
      phone_number->set_type(tutorial::Person::WORK);
    } else {
      cout << "Unknown phone type.  Using default." << endl;
    }
  }
  *person->mutable_last_updated() = TimeUtil::SecondsToTimestamp(time(NULL)); // 设置时间
}
// 正确方式 g++ -std=c++11 -o add_person add_person.cc addressbook.pb.cc -lprotobuf  -lpthread
// Main function:  Reads the entire address book from a file,
//   adds one person based on user input, then writes it back out to the same
//   file.
int main(int argc, char* argv[]) {
  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  if (argc != 2) {
    cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
    return -1;
  }

  tutorial::AddressBook address_book;

  {
    // Read the existing address book.  本地文件保存电话本的数据
    fstream input(argv[1], ios::in | ios::binary);
    if (!input) {
      cout << argv[1] << ": File not found.  Creating a new file." << endl;
    } else if (!address_book.ParseFromIstream(&input)) {
      cerr << "Failed to parse address book." << endl;
      return -1;
    }
  }

  // Add an address.  address_book.add_people() 新建一个对象Person
  PromptForAddress(address_book.add_people());



  {
    // Write the new address book back to disk.  保存电话本
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!address_book.SerializeToOstream(&output)) {
      cerr << "Failed to write address book." << endl;
      return -1;
    }
  }

  // Optional:  Delete all global objects allocated by libprotobuf.
  google::protobuf::ShutdownProtobufLibrary();

  return 0;
}

6.结果展示

输入

输出

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/126567.html

相关文章:

  • 第三节:React 基础篇-React组件通信方案
  • JAVA Web_定义Servlet_1 欢迎考生
  • 客户案例 | 日事清×初心家居:多部门协作实现新品上架自动化
  • 分布式ID生成方案的深度解析与Java实现
  • Docker 常用命令指南
  • Python表达式入门指南:从基础到实践
  • leetcode-动态规划25
  • Java接口深度解析
  • B3647 【模板】Floyd
  • 深入 Redis 持久化:从原理到企业级应用的全景图
  • 音视频 五 看书的笔记 MediaCodec
  • 计算机网络 3-3 数据链路层(介质访问控制)
  • 第六章 进阶03 外包测试亮相
  • 第四十六篇 人力资源管理数据仓库架构设计与高阶实践
  • Nginx 命令大全:Linux 与 Windows 系统的全面解析
  • 《计算机名人堂》专栏介绍:先驱之路
  • c# BitMap的data是rgb格式还是bgr格式
  • 【Origin绘图系列第7棒】3D瀑布图
  • python入门:简单介绍和python和pycharm软件安装/学习网址/pycharm设置(改成中文界面,主题,新建文件)
  • Python的那些事第四十九篇:基于Python的智能客服系统设计与实现
  • 蓝桥杯 14g
  • Activiti(五)- 工作流引擎中流程定义删除机制
  • K8s常用基础管理命令(一)
  • PPT模板之--个人简历
  • 安全序列(DP)
  • IO流——字符输入输出流:FileReader FileWriter
  • 【服务器端表单字符验证】
  • 若依前后端分离版之使用Swagger
  • 解决unity设置鼠标图标发布以后没有效果的问题
  • 一维差分数组