protobuf c++ 接口分析
来源:互联网 发布:网络用语dc是什么意思 编辑:程序博客网 时间:2024/06/05 15:27
最近学习protobuf, 遇到好多问题, 最主要的就是接口的使用, 现在还没搞明白, 一边总结一边补充吧
首先编写一个 .proto,格式网上都有很多,我用的是 person.proto
message person { required string name = 1; //name optional uint32 age = 2; // age}然后用protoc person.proto --cpp_out=. 编译 将目标文件放到当前目录下面 这时有了
person.pb.h
// Generated by the protocol buffer compiler. DO NOT EDIT!// source: person.proto#ifndef PROTOBUF_person_2eproto__INCLUDED#define PROTOBUF_person_2eproto__INCLUDED#include <string>#include <google/protobuf/stubs/common.h>#if GOOGLE_PROTOBUF_VERSION < 2005000#error This file was generated by a newer version of protoc which is#error incompatible with your Protocol Buffer headers. Please update#error your headers.#endif#if 2005000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION#error This file was generated by an older version of protoc which is#error incompatible with your Protocol Buffer headers. Please#error regenerate this file with a newer version of protoc.#endif#include <google/protobuf/generated_message_util.h>#include <google/protobuf/message.h>#include <google/protobuf/repeated_field.h>#include <google/protobuf/extension_set.h>#include <google/protobuf/unknown_field_set.h>// @@protoc_insertion_point(includes)// Internal implementation detail -- do not call these.void protobuf_AddDesc_person_2eproto();void protobuf_AssignDesc_person_2eproto();void protobuf_ShutdownFile_person_2eproto();class person;// ===================================================================class person : public ::google::protobuf::Message { public: person(); virtual ~person(); person(const person& from); inline person& operator=(const person& from) { CopyFrom(from); return *this; } inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { return _unknown_fields_; } inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { return &_unknown_fields_; } static const ::google::protobuf::Descriptor* descriptor(); static const person& default_instance(); void Swap(person* other); // implements Message ---------------------------------------------- person* New() const; void CopyFrom(const ::google::protobuf::Message& from); void MergeFrom(const ::google::protobuf::Message& from); void CopyFrom(const person& from); void MergeFrom(const person& from); void Clear(); bool IsInitialized() const; int ByteSize() const; bool MergePartialFromCodedStream( ::google::protobuf::io::CodedInputStream* input); void SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const; ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; int GetCachedSize() const { return _cached_size_; } private: void SharedCtor(); void SharedDtor(); void SetCachedSize(int size) const; public: ::google::protobuf::Metadata GetMetadata() const; // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- // required string name = 1; inline bool has_name() const; inline void clear_name(); static const int kNameFieldNumber = 1; inline const ::std::string& name() const; inline void set_name(const ::std::string& value); inline void set_name(const char* value); inline void set_name(const char* value, size_t size); inline ::std::string* mutable_name(); inline ::std::string* release_name(); inline void set_allocated_name(::std::string* name); // optional uint32 age = 2; inline bool has_age() const; inline void clear_age(); static const int kAgeFieldNumber = 2; inline ::google::protobuf::uint32 age() const; inline void set_age(::google::protobuf::uint32 value); // @@protoc_insertion_point(class_scope:person) private: inline void set_has_name(); inline void clear_has_name(); inline void set_has_age(); inline void clear_has_age(); ::google::protobuf::UnknownFieldSet _unknown_fields_; ::std::string* name_; ::google::protobuf::uint32 age_; mutable int _cached_size_; ::google::protobuf::uint32 _has_bits_[(2 + 31) / 32]; friend void protobuf_AddDesc_person_2eproto(); friend void protobuf_AssignDesc_person_2eproto(); friend void protobuf_ShutdownFile_person_2eproto(); void InitAsDefaultInstance(); static person* default_instance_;};// ===================================================================// ===================================================================// person// required string name = 1;inline bool person::has_name() const { return (_has_bits_[0] & 0x00000001u) != 0;}inline void person::set_has_name() { _has_bits_[0] |= 0x00000001u;}inline void person::clear_has_name() { _has_bits_[0] &= ~0x00000001u;}inline void person::clear_name() { if (name_ != &::google::protobuf::internal::kEmptyString) { name_->clear(); } clear_has_name();}inline const ::std::string& person::name() const { return *name_;}inline void person::set_name(const ::std::string& value) { set_has_name(); if (name_ == &::google::protobuf::internal::kEmptyString) { name_ = new ::std::string; } name_->assign(value);}inline void person::set_name(const char* value) { set_has_name(); if (name_ == &::google::protobuf::internal::kEmptyString) { name_ = new ::std::string; } name_->assign(value);}inline void person::set_name(const char* value, size_t size) { set_has_name(); if (name_ == &::google::protobuf::internal::kEmptyString) { name_ = new ::std::string; } name_->assign(reinterpret_cast<const char*>(value), size);}inline ::std::string* person::mutable_name() { set_has_name(); if (name_ == &::google::protobuf::internal::kEmptyString) { name_ = new ::std::string; } return name_;}inline ::std::string* person::release_name() { clear_has_name(); if (name_ == &::google::protobuf::internal::kEmptyString) { return NULL; } else { ::std::string* temp = name_; name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); return temp; }}inline void person::set_allocated_name(::std::string* name) { if (name_ != &::google::protobuf::internal::kEmptyString) { delete name_; } if (name) { set_has_name(); name_ = name; } else { clear_has_name(); name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); }}// optional uint32 age = 2;inline bool person::has_age() const { return (_has_bits_[0] & 0x00000002u) != 0;}inline void person::set_has_age() { _has_bits_[0] |= 0x00000002u;}inline void person::clear_has_age() { _has_bits_[0] &= ~0x00000002u;}inline void person::clear_age() { age_ = 0u; clear_has_age();}inline ::google::protobuf::uint32 person::age() const { return age_;}inline void person::set_age(::google::protobuf::uint32 value) { set_has_age(); age_ = value;}// @@protoc_insertion_point(namespace_scope)#ifndef SWIGnamespace google {namespace protobuf {} // namespace google} // namespace protobuf#endif // SWIG// @@protoc_insertion_point(global_scope)#endif // PROTOBUF_person_2eproto__INCLUDED和person.pb.cc
// Generated by the protocol buffer compiler. DO NOT EDIT!// source: person.proto#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION#include "person.pb.h"#include <algorithm>#include <google/protobuf/stubs/common.h>#include <google/protobuf/stubs/once.h>#include <google/protobuf/io/coded_stream.h>#include <google/protobuf/wire_format_lite_inl.h>#include <google/protobuf/descriptor.h>#include <google/protobuf/generated_message_reflection.h>#include <google/protobuf/reflection_ops.h>#include <google/protobuf/wire_format.h>// @@protoc_insertion_point(includes)namespace {const ::google::protobuf::Descriptor* person_descriptor_ = NULL;const ::google::protobuf::internal::GeneratedMessageReflection* person_reflection_ = NULL;} // namespacevoid protobuf_AssignDesc_person_2eproto() { protobuf_AddDesc_person_2eproto(); const ::google::protobuf::FileDescriptor* file = ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName( "person.proto"); GOOGLE_CHECK(file != NULL); person_descriptor_ = file->message_type(0); static const int person_offsets_[2] = { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(person, name_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(person, age_), }; person_reflection_ = new ::google::protobuf::internal::GeneratedMessageReflection( person_descriptor_, person::default_instance_, person_offsets_, GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(person, _has_bits_[0]), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(person, _unknown_fields_), -1, ::google::protobuf::DescriptorPool::generated_pool(), ::google::protobuf::MessageFactory::generated_factory(), sizeof(person));}namespace {GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);inline void protobuf_AssignDescriptorsOnce() { ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_, &protobuf_AssignDesc_person_2eproto);}void protobuf_RegisterTypes(const ::std::string&) { protobuf_AssignDescriptorsOnce(); ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( person_descriptor_, &person::default_instance());}} // namespacevoid protobuf_ShutdownFile_person_2eproto() { delete person::default_instance_; delete person_reflection_;}void protobuf_AddDesc_person_2eproto() { static bool already_here = false; if (already_here) return; already_here = true; GOOGLE_PROTOBUF_VERIFY_VERSION; ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( "\n\014person.proto\"#\n\006person\022\014\n\004name\030\001 \002(\t\022\013" "\n\003age\030\002 \001(\r", 51); ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( "person.proto", &protobuf_RegisterTypes); person::default_instance_ = new person(); person::default_instance_->InitAsDefaultInstance(); ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_person_2eproto);}// Force AddDescriptors() to be called at static initialization time.struct StaticDescriptorInitializer_person_2eproto { StaticDescriptorInitializer_person_2eproto() { protobuf_AddDesc_person_2eproto(); }} static_descriptor_initializer_person_2eproto_;// ===================================================================#ifndef _MSC_VERconst int person::kNameFieldNumber;const int person::kAgeFieldNumber;#endif // !_MSC_VERperson::person() : ::google::protobuf::Message() { SharedCtor();}void person::InitAsDefaultInstance() {}person::person(const person& from) : ::google::protobuf::Message() { SharedCtor(); MergeFrom(from);}void person::SharedCtor() { _cached_size_ = 0; name_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); age_ = 0u; ::memset(_has_bits_, 0, sizeof(_has_bits_));}person::~person() { SharedDtor();}void person::SharedDtor() { if (name_ != &::google::protobuf::internal::kEmptyString) { delete name_; } if (this != default_instance_) { }}void person::SetCachedSize(int size) const { GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); _cached_size_ = size; GOOGLE_SAFE_CONCURRENT_WRITES_END();}const ::google::protobuf::Descriptor* person::descriptor() { protobuf_AssignDescriptorsOnce(); return person_descriptor_;}const person& person::default_instance() { if (default_instance_ == NULL) protobuf_AddDesc_person_2eproto(); return *default_instance_;}person* person::default_instance_ = NULL;person* person::New() const { return new person;}void person::Clear() { if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { if (has_name()) { if (name_ != &::google::protobuf::internal::kEmptyString) { name_->clear(); } } age_ = 0u; } ::memset(_has_bits_, 0, sizeof(_has_bits_)); mutable_unknown_fields()->Clear();}bool person::MergePartialFromCodedStream( ::google::protobuf::io::CodedInputStream* input) {#define DO_(EXPRESSION) if (!(EXPRESSION)) return false ::google::protobuf::uint32 tag; while ((tag = input->ReadTag()) != 0) { switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { // required string name = 1; case 1: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_name())); ::google::protobuf::internal::WireFormat::VerifyUTF8String( this->name().data(), this->name().length(), ::google::protobuf::internal::WireFormat::PARSE); } else { goto handle_uninterpreted; } if (input->ExpectTag(16)) goto parse_age; break; } // optional uint32 age = 2; case 2: { if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { parse_age: DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::uint32, ::google::protobuf::internal::WireFormatLite::TYPE_UINT32>( input, &age_))); set_has_age(); } else { goto handle_uninterpreted; } if (input->ExpectAtEnd()) return true; break; } default: { handle_uninterpreted: if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { return true; } DO_(::google::protobuf::internal::WireFormat::SkipField( input, tag, mutable_unknown_fields())); break; } } } return true;#undef DO_}void person::SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const { // required string name = 1; if (has_name()) { ::google::protobuf::internal::WireFormat::VerifyUTF8String( this->name().data(), this->name().length(), ::google::protobuf::internal::WireFormat::SERIALIZE); ::google::protobuf::internal::WireFormatLite::WriteString( 1, this->name(), output); } // optional uint32 age = 2; if (has_age()) { ::google::protobuf::internal::WireFormatLite::WriteUInt32(2, this->age(), output); } if (!unknown_fields().empty()) { ::google::protobuf::internal::WireFormat::SerializeUnknownFields( unknown_fields(), output); }}::google::protobuf::uint8* person::SerializeWithCachedSizesToArray( ::google::protobuf::uint8* target) const { // required string name = 1; if (has_name()) { ::google::protobuf::internal::WireFormat::VerifyUTF8String( this->name().data(), this->name().length(), ::google::protobuf::internal::WireFormat::SERIALIZE); target = ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 1, this->name(), target); } // optional uint32 age = 2; if (has_age()) { target = ::google::protobuf::internal::WireFormatLite::WriteUInt32ToArray(2, this->age(), target); } if (!unknown_fields().empty()) { target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( unknown_fields(), target); } return target;}int person::ByteSize() const { int total_size = 0; if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { // required string name = 1; if (has_name()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::StringSize( this->name()); } // optional uint32 age = 2; if (has_age()) { total_size += 1 + ::google::protobuf::internal::WireFormatLite::UInt32Size( this->age()); } } if (!unknown_fields().empty()) { total_size += ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( unknown_fields()); } GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); _cached_size_ = total_size; GOOGLE_SAFE_CONCURRENT_WRITES_END(); return total_size;}void person::MergeFrom(const ::google::protobuf::Message& from) { GOOGLE_CHECK_NE(&from, this); const person* source = ::google::protobuf::internal::dynamic_cast_if_available<const person*>( &from); if (source == NULL) { ::google::protobuf::internal::ReflectionOps::Merge(from, this); } else { MergeFrom(*source); }}void person::MergeFrom(const person& from) { GOOGLE_CHECK_NE(&from, this); if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { if (from.has_name()) { set_name(from.name()); } if (from.has_age()) { set_age(from.age()); } } mutable_unknown_fields()->MergeFrom(from.unknown_fields());}void person::CopyFrom(const ::google::protobuf::Message& from) { if (&from == this) return; Clear(); MergeFrom(from);}void person::CopyFrom(const person& from) { if (&from == this) return; Clear(); MergeFrom(from);}bool person::IsInitialized() const { if ((_has_bits_[0] & 0x00000001) != 0x00000001) return false; return true;}void person::Swap(person* other) { if (other != this) { std::swap(name_, other->name_); std::swap(age_, other->age_); std::swap(_has_bits_[0], other->_has_bits_[0]); _unknown_fields_.Swap(&other->_unknown_fields_); std::swap(_cached_size_, other->_cached_size_); }}::google::protobuf::Metadata person::GetMetadata() const { protobuf_AssignDescriptorsOnce(); ::google::protobuf::Metadata metadata; metadata.descriptor = person_descriptor_; metadata.reflection = person_reflection_; return metadata;}// @@protoc_insertion_point(namespace_scope)// @@protoc_insertion_point(global_scope)
然后编写调用接口的c++文件,这里是1.cpp
#include "person.pb.h"#include <iostream>#include <ios>using namespace std;void InfoStudents(const person& sxy){ cout<< "sxy info:"<<endl; cout<<"name: "<<sxy.name()<<endl; cout<<"age: "<<sxy.age()<<endl;}int main(){ person sxy; sxy.set_name("shixiaoyu"); sxy.set_age(25); int size = sxy.ByteSize(); cout<<"size: "<<size<<endl; cout<<"name1: "<<sxy.has_age()<<endl; //字段是否存在 设置? unsigned char btc[size]; sxy.SerializeWithCachedSizesToArray(btc); InfoStudents(sxy); return 0; }编译: g++ 1.cpp -o aa person.pb.cc -I /usr/local/protobuf/include -L /usr/local/protobuf/lib -lprotobuf -pthread
运行结果:
size: 13name1: 1sxy info:name: shixiaoyuage: 25
阅读全文
0 0
- protobuf c++ 接口分析
- protobuf-c
- Google Protobuf 协议分析
- protobuf 之 MessageLite 接口摘录
- protobuf 之 message 接口摘录
- protobuf-c学习笔记
- protobuf-c学习总结
- protobuf-c学习小结
- protobuf之c基础
- C++-glog/gflags/protobuf
- 【C++】google protobuf库
- Ubuntu上安装protobuf 和protobuf-c
- Android 匿名共享内存C接口分析
- Android 匿名共享内存C接口分析
- google protobuf源码分析1
- 【转载】Google Protobuf 协议分析
- linux 下安装protobuf-c
- protobuf-c的学习总结
- Spring MVC框架
- java内存模型
- 为什么我们需要 集合(set)?
- c++实现快速排序
- Java的接口中是否可以使用静态方法
- protobuf c++ 接口分析
- 《数据结构》学习笔记(4)
- 数组指针和指针数组的区别
- 快速理解VirtualBox的四种网络连接方式
- 手机变成万能红外遥控器,自己动手制作教程
- 俞润东做人做事十六字箴言
- python学习笔记(四):核心模块方法
- c语言 stdlib库函数
- Android 开源网络框架(Android-Async-Http、Volley、OkHttp3、Retrofit2.0)对比