protobuf practice
https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
三点: 1. 编写proto文件
2. 编译proto生成对应语言的实现
3. 在需求代码中引入2中的实现
1. 编写proto文件
清单 1. lm.helloworld.proto 文件 package lm; message helloworld { required int32 id = 1; // ID required string str = 2; // str optional int32 opt = 3; //optional field }
2. 编译proto生成对应语言的实现
编译 protoc --cpp_out=. lm.helloworld.proto
生成 lm.helloworld.pb.h 和 lm.helloworld.pb.cc,这两个文件为对应lm.helloworld.proto文件内部相关数据结构的类实现,直接在功能代码中使用。
class helloworld : public ::google::protobuf::Message
proto文件生成的对应类,是继承于google::protobuf相关的类,携带有大量很好的操作方法。
如:string DebugString() const;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // Generated by the protocol buffer compiler. DO NOT EDIT! 2 // source: lm.helloworld.proto 3 4 #ifndef PROTOBUF_lm_2ehelloworld_2eproto__INCLUDED 5 #define PROTOBUF_lm_2ehelloworld_2eproto__INCLUDED 6 7 #include <string> 8 9 #include <google/protobuf/stubs/common.h> 10 11 #if GOOGLE_PROTOBUF_VERSION < 2006000 12 #error This file was generated by a newer version of protoc which is 13 #error incompatible with your Protocol Buffer headers. Please update 14 #error your headers. 15 #endif 16 #if 2006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 17 #error This file was generated by an older version of protoc which is 18 #error incompatible with your Protocol Buffer headers. Please 19 #error regenerate this file with a newer version of protoc. 20 #endif 21 22 #include <google/protobuf/generated_message_util.h> 23 #include <google/protobuf/message.h> 24 #include <google/protobuf/repeated_field.h> 25 #include <google/protobuf/extension_set.h> 26 #include <google/protobuf/unknown_field_set.h> 27 // @@protoc_insertion_point(includes) 28 29 namespace lm { 30 31 // Internal implementation detail -- do not call these. 32 void protobuf_AddDesc_lm_2ehelloworld_2eproto(); 33 void protobuf_AssignDesc_lm_2ehelloworld_2eproto(); 34 void protobuf_ShutdownFile_lm_2ehelloworld_2eproto(); 35 36 class helloworld; 37 38 // =================================================================== 39 40 class helloworld : public ::google::protobuf::Message { 41 public: 42 helloworld(); 43 virtual ~helloworld(); 44 45 helloworld(const helloworld& from); 46 47 inline helloworld& operator=(const helloworld& from) { 48 CopyFrom(from); 49 return *this; 50 } 51 52 inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { 53 return _unknown_fields_; 54 } 55 56 inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { 57 return &_unknown_fields_; 58 } 59 60 static const ::google::protobuf::Descriptor* descriptor(); 61 static const helloworld& default_instance(); 62 63 void Swap(helloworld* other); 64 65 // implements Message ---------------------------------------------- 66 67 helloworld* New() const; 68 void CopyFrom(const ::google::protobuf::Message& from); 69 void MergeFrom(const ::google::protobuf::Message& from); 70 void CopyFrom(const helloworld& from); 71 void MergeFrom(const helloworld& from); 72 void Clear(); 73 bool IsInitialized() const; 74 75 int ByteSize() const; 76 bool MergePartialFromCodedStream( 77 ::google::protobuf::io::CodedInputStream* input); 78 void SerializeWithCachedSizes( 79 ::google::protobuf::io::CodedOutputStream* output) const; 80 ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; 81 int GetCachedSize() const { return _cached_size_; } 82 private: 83 void SharedCtor(); 84 void SharedDtor(); 85 void SetCachedSize(int size) const; 86 public: 87 ::google::protobuf::Metadata GetMetadata() const; 88 89 // nested types ---------------------------------------------------- 90 91 // accessors ------------------------------------------------------- 92 93 // required int32 id = 1; 94 inline bool has_id() const; 95 inline void clear_id(); 96 static const int kIdFieldNumber = 1; 97 inline ::google::protobuf::int32 id() const; 98 inline void set_id(::google::protobuf::int32 value); 99 100 // required string str = 2; 101 inline bool has_str() const; 102 inline void clear_str(); 103 static const int kStrFieldNumber = 2; 104 inline const ::std::string& str() const; 105 inline void set_str(const ::std::string& value); 106 inline void set_str(const char* value); 107 inline void set_str(const char* value, size_t size); 108 inline ::std::string* mutable_str(); 109 inline ::std::string* release_str(); 110 inline void set_allocated_str(::std::string* str); 111 112 // optional int32 opt = 3; 113 inline bool has_opt() const; 114 inline void clear_opt(); 115 static const int kOptFieldNumber = 3; 116 inline ::google::protobuf::int32 opt() const; 117 inline void set_opt(::google::protobuf::int32 value); 118 119 // @@protoc_insertion_point(class_scope:lm.helloworld) 120 private: 121 inline void set_has_id(); 122 inline void clear_has_id(); 123 inline void set_has_str(); 124 inline void clear_has_str(); 125 inline void set_has_opt(); 126 inline void clear_has_opt(); 127 128 ::google::protobuf::UnknownFieldSet _unknown_fields_; 129 130 ::google::protobuf::uint32 _has_bits_[1]; 131 mutable int _cached_size_; 132 ::std::string* str_; 133 ::google::protobuf::int32 id_; 134 ::google::protobuf::int32 opt_; 135 friend void protobuf_AddDesc_lm_2ehelloworld_2eproto(); 136 friend void protobuf_AssignDesc_lm_2ehelloworld_2eproto(); 137 friend void protobuf_ShutdownFile_lm_2ehelloworld_2eproto(); 138 139 void InitAsDefaultInstance(); 140 static helloworld* default_instance_; 141 }; 142 // =================================================================== 143 144 145 // =================================================================== 146 147 // helloworld 148 149 // required int32 id = 1; 150 inline bool helloworld::has_id() const { 151 return (_has_bits_[0] & 0x00000001u) != 0; 152 } 153 inline void helloworld::set_has_id() { 154 _has_bits_[0] |= 0x00000001u; 155 } 156 inline void helloworld::clear_has_id() { 157 _has_bits_[0] &= ~0x00000001u; 158 } 159 inline void helloworld::clear_id() { 160 id_ = 0; 161 clear_has_id(); 162 } 163 inline ::google::protobuf::int32 helloworld::id() const { 164 // @@protoc_insertion_point(field_get:lm.helloworld.id) 165 return id_; 166 } 167 inline void helloworld::set_id(::google::protobuf::int32 value) { 168 set_has_id(); 169 id_ = value; 170 // @@protoc_insertion_point(field_set:lm.helloworld.id) 171 } 172 173 // required string str = 2; 174 inline bool helloworld::has_str() const { 175 return (_has_bits_[0] & 0x00000002u) != 0; 176 } 177 inline void helloworld::set_has_str() { 178 _has_bits_[0] |= 0x00000002u; 179 } 180 inline void helloworld::clear_has_str() { 181 _has_bits_[0] &= ~0x00000002u; 182 } 183 inline void helloworld::clear_str() { 184 if (str_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 185 str_->clear(); 186 } 187 clear_has_str(); 188 } 189 inline const ::std::string& helloworld::str() const { 190 // @@protoc_insertion_point(field_get:lm.helloworld.str) 191 return *str_; 192 } 193 inline void helloworld::set_str(const ::std::string& value) { 194 set_has_str(); 195 if (str_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 196 str_ = new ::std::string; 197 } 198 str_->assign(value); 199 // @@protoc_insertion_point(field_set:lm.helloworld.str) 200 } 201 inline void helloworld::set_str(const char* value) { 202 set_has_str(); 203 if (str_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 204 str_ = new ::std::string; 205 } 206 str_->assign(value); 207 // @@protoc_insertion_point(field_set_char:lm.helloworld.str) 208 } 209 inline void helloworld::set_str(const char* value, size_t size) { 210 set_has_str(); 211 if (str_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 212 str_ = new ::std::string; 213 } 214 str_->assign(reinterpret_cast<const char*>(value), size); 215 // @@protoc_insertion_point(field_set_pointer:lm.helloworld.str) 216 } 217 inline ::std::string* helloworld::mutable_str() { 218 set_has_str(); 219 if (str_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 220 str_ = new ::std::string; 221 } 222 // @@protoc_insertion_point(field_mutable:lm.helloworld.str) 223 return str_; 224 } 225 inline ::std::string* helloworld::release_str() { 226 clear_has_str(); 227 if (str_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 228 return NULL; 229 } else { 230 ::std::string* temp = str_; 231 str_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); 232 return temp; 233 } 234 } 235 inline void helloworld::set_allocated_str(::std::string* str) { 236 if (str_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 237 delete str_; 238 } 239 if (str) { 240 set_has_str(); 241 str_ = str; 242 } else { 243 clear_has_str(); 244 str_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); 245 } 246 // @@protoc_insertion_point(field_set_allocated:lm.helloworld.str) 247 } 248 249 // optional int32 opt = 3; 250 inline bool helloworld::has_opt() const { 251 return (_has_bits_[0] & 0x00000004u) != 0; 252 } 253 inline void helloworld::set_has_opt() { 254 _has_bits_[0] |= 0x00000004u; 255 } 256 inline void helloworld::clear_has_opt() { 257 _has_bits_[0] &= ~0x00000004u; 258 } 259 inline void helloworld::clear_opt() { 260 opt_ = 0; 261 clear_has_opt(); 262 } 263 inline ::google::protobuf::int32 helloworld::opt() const { 264 // @@protoc_insertion_point(field_get:lm.helloworld.opt) 265 return opt_; 266 } 267 inline void helloworld::set_opt(::google::protobuf::int32 value) { 268 set_has_opt(); 269 opt_ = value; 270 // @@protoc_insertion_point(field_set:lm.helloworld.opt) 271 } 272 273 274 // @@protoc_insertion_point(namespace_scope) 275 276 } // namespace lm 277 278 #ifndef SWIG 279 namespace google { 280 namespace protobuf { 281 282 283 } // namespace google 284 } // namespace protobuf 285 #endif // SWIG 286 287 // @@protoc_insertion_point(global_scope) 288 289 #endif // PROTOBUF_lm_2ehelloworld_2eproto__INCLUDED
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // Generated by the protocol buffer compiler. DO NOT EDIT! 2 // source: lm.helloworld.proto 3 4 #define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION 5 #include "lm.helloworld.pb.h" 6 7 #include <algorithm> 8 9 #include <google/protobuf/stubs/common.h> 10 #include <google/protobuf/stubs/once.h> 11 #include <google/protobuf/io/coded_stream.h> 12 #include <google/protobuf/wire_format_lite_inl.h> 13 #include <google/protobuf/descriptor.h> 14 #include <google/protobuf/generated_message_reflection.h> 15 #include <google/protobuf/reflection_ops.h> 16 #include <google/protobuf/wire_format.h> 17 // @@protoc_insertion_point(includes) 18 19 namespace lm { 20 21 namespace { 22 23 const ::google::protobuf::Descriptor* helloworld_descriptor_ = NULL; 24 const ::google::protobuf::internal::GeneratedMessageReflection* 25 helloworld_reflection_ = NULL; 26 27 } // namespace 28 29 30 void protobuf_AssignDesc_lm_2ehelloworld_2eproto() { 31 protobuf_AddDesc_lm_2ehelloworld_2eproto(); 32 const ::google::protobuf::FileDescriptor* file = 33 ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName( 34 "lm.helloworld.proto"); 35 GOOGLE_CHECK(file != NULL); 36 helloworld_descriptor_ = file->message_type(0); 37 static const int helloworld_offsets_[3] = { 38 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, id_), 39 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, str_), 40 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, opt_), 41 }; 42 helloworld_reflection_ = 43 new ::google::protobuf::internal::GeneratedMessageReflection( 44 helloworld_descriptor_, 45 helloworld::default_instance_, 46 helloworld_offsets_, 47 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, _has_bits_[0]), 48 GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(helloworld, _unknown_fields_), 49 -1, 50 ::google::protobuf::DescriptorPool::generated_pool(), 51 ::google::protobuf::MessageFactory::generated_factory(), 52 sizeof(helloworld)); 53 } 54 55 namespace { 56 57 GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_); 58 inline void protobuf_AssignDescriptorsOnce() { 59 ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_, 60 &protobuf_AssignDesc_lm_2ehelloworld_2eproto); 61 } 62 63 void protobuf_RegisterTypes(const ::std::string&) { 64 protobuf_AssignDescriptorsOnce(); 65 ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( 66 helloworld_descriptor_, &helloworld::default_instance()); 67 } 68 69 } // namespace 70 71 void protobuf_ShutdownFile_lm_2ehelloworld_2eproto() { 72 delete helloworld::default_instance_; 73 delete helloworld_reflection_; 74 } 75 76 void protobuf_AddDesc_lm_2ehelloworld_2eproto() { 77 static bool already_here = false; 78 if (already_here) return; 79 already_here = true; 80 GOOGLE_PROTOBUF_VERIFY_VERSION; 81 82 ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( 83 "\n\023lm.helloworld.proto\022\002lm\"2\n\nhelloworld\022" 84 "\n\n\002id\030\001 \002(\005\022\013\n\003str\030\002 \002(\t\022\013\n\003opt\030\003 \001(\005", 77); 85 ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( 86 "lm.helloworld.proto", &protobuf_RegisterTypes); 87 helloworld::default_instance_ = new helloworld(); 88 helloworld::default_instance_->InitAsDefaultInstance(); 89 ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_lm_2ehelloworld_2eproto); 90 } 91 92 // Force AddDescriptors() to be called at static initialization time. 93 struct StaticDescriptorInitializer_lm_2ehelloworld_2eproto { 94 StaticDescriptorInitializer_lm_2ehelloworld_2eproto() { 95 protobuf_AddDesc_lm_2ehelloworld_2eproto(); 96 } 97 } static_descriptor_initializer_lm_2ehelloworld_2eproto_; 98 99 // =================================================================== 100 101 #ifndef _MSC_VER 102 const int helloworld::kIdFieldNumber; 103 const int helloworld::kStrFieldNumber; 104 const int helloworld::kOptFieldNumber; 105 #endif // !_MSC_VER 106 107 helloworld::helloworld() 108 : ::google::protobuf::Message() { 109 SharedCtor(); 110 // @@protoc_insertion_point(constructor:lm.helloworld) 111 } 112 113 void helloworld::InitAsDefaultInstance() { 114 } 115 116 helloworld::helloworld(const helloworld& from) 117 : ::google::protobuf::Message() { 118 SharedCtor(); 119 MergeFrom(from); 120 // @@protoc_insertion_point(copy_constructor:lm.helloworld) 121 } 122 123 void helloworld::SharedCtor() { 124 ::google::protobuf::internal::GetEmptyString(); 125 _cached_size_ = 0; 126 id_ = 0; 127 str_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); 128 opt_ = 0; 129 ::memset(_has_bits_, 0, sizeof(_has_bits_)); 130 } 131 132 helloworld::~helloworld() { 133 // @@protoc_insertion_point(destructor:lm.helloworld) 134 SharedDtor(); 135 } 136 137 void helloworld::SharedDtor() { 138 if (str_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 139 delete str_; 140 } 141 if (this != default_instance_) { 142 } 143 } 144 145 void helloworld::SetCachedSize(int size) const { 146 GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); 147 _cached_size_ = size; 148 GOOGLE_SAFE_CONCURRENT_WRITES_END(); 149 } 150 const ::google::protobuf::Descriptor* helloworld::descriptor() { 151 protobuf_AssignDescriptorsOnce(); 152 return helloworld_descriptor_; 153 } 154 155 const helloworld& helloworld::default_instance() { 156 if (default_instance_ == NULL) protobuf_AddDesc_lm_2ehelloworld_2eproto(); 157 return *default_instance_; 158 } 159 160 helloworld* helloworld::default_instance_ = NULL; 161 162 helloworld* helloworld::New() const { 163 return new helloworld; 164 } 165 166 void helloworld::Clear() { 167 #define OFFSET_OF_FIELD_(f) (reinterpret_cast<char*>( \ 168 &reinterpret_cast<helloworld*>(16)->f) - \ 169 reinterpret_cast<char*>(16)) 170 171 #define ZR_(first, last) do { \ 172 size_t f = OFFSET_OF_FIELD_(first); \ 173 size_t n = OFFSET_OF_FIELD_(last) - f + sizeof(last); \ 174 ::memset(&first, 0, n); \ 175 } while (0) 176 177 if (_has_bits_[0 / 32] & 7) { 178 ZR_(id_, opt_); 179 if (has_str()) { 180 if (str_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) { 181 str_->clear(); 182 } 183 } 184 } 185 186 #undef OFFSET_OF_FIELD_ 187 #undef ZR_ 188 189 ::memset(_has_bits_, 0, sizeof(_has_bits_)); 190 mutable_unknown_fields()->Clear(); 191 } 192 193 bool helloworld::MergePartialFromCodedStream( 194 ::google::protobuf::io::CodedInputStream* input) { 195 #define DO_(EXPRESSION) if (!(EXPRESSION)) goto failure 196 ::google::protobuf::uint32 tag; 197 // @@protoc_insertion_point(parse_start:lm.helloworld) 198 for (;;) { 199 ::std::pair< ::google::protobuf::uint32, bool> p = input->ReadTagWithCutoff(127); 200 tag = p.first; 201 if (!p.second) goto handle_unusual; 202 switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { 203 // required int32 id = 1; 204 case 1: { 205 if (tag == 8) { 206 DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< 207 ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( 208 input, &id_))); 209 set_has_id(); 210 } else { 211 goto handle_unusual; 212 } 213 if (input->ExpectTag(18)) goto parse_str; 214 break; 215 } 216 217 // required string str = 2; 218 case 2: { 219 if (tag == 18) { 220 parse_str: 221 DO_(::google::protobuf::internal::WireFormatLite::ReadString( 222 input, this->mutable_str())); 223 ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( 224 this->str().data(), this->str().length(), 225 ::google::protobuf::internal::WireFormat::PARSE, 226 "str"); 227 } else { 228 goto handle_unusual; 229 } 230 if (input->ExpectTag(24)) goto parse_opt; 231 break; 232 } 233 234 // optional int32 opt = 3; 235 case 3: { 236 if (tag == 24) { 237 parse_opt: 238 DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< 239 ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( 240 input, &opt_))); 241 set_has_opt(); 242 } else { 243 goto handle_unusual; 244 } 245 if (input->ExpectAtEnd()) goto success; 246 break; 247 } 248 249 default: { 250 handle_unusual: 251 if (tag == 0 || 252 ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 253 ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { 254 goto success; 255 } 256 DO_(::google::protobuf::internal::WireFormat::SkipField( 257 input, tag, mutable_unknown_fields())); 258 break; 259 } 260 } 261 } 262 success: 263 // @@protoc_insertion_point(parse_success:lm.helloworld) 264 return true; 265 failure: 266 // @@protoc_insertion_point(parse_failure:lm.helloworld) 267 return false; 268 #undef DO_ 269 } 270 271 void helloworld::SerializeWithCachedSizes( 272 ::google::protobuf::io::CodedOutputStream* output) const { 273 // @@protoc_insertion_point(serialize_start:lm.helloworld) 274 // required int32 id = 1; 275 if (has_id()) { 276 ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->id(), output); 277 } 278 279 // required string str = 2; 280 if (has_str()) { 281 ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( 282 this->str().data(), this->str().length(), 283 ::google::protobuf::internal::WireFormat::SERIALIZE, 284 "str"); 285 ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased( 286 2, this->str(), output); 287 } 288 289 // optional int32 opt = 3; 290 if (has_opt()) { 291 ::google::protobuf::internal::WireFormatLite::WriteInt32(3, this->opt(), output); 292 } 293 294 if (!unknown_fields().empty()) { 295 ::google::protobuf::internal::WireFormat::SerializeUnknownFields( 296 unknown_fields(), output); 297 } 298 // @@protoc_insertion_point(serialize_end:lm.helloworld) 299 } 300 301 ::google::protobuf::uint8* helloworld::SerializeWithCachedSizesToArray( 302 ::google::protobuf::uint8* target) const { 303 // @@protoc_insertion_point(serialize_to_array_start:lm.helloworld) 304 // required int32 id = 1; 305 if (has_id()) { 306 target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->id(), target); 307 } 308 309 // required string str = 2; 310 if (has_str()) { 311 ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( 312 this->str().data(), this->str().length(), 313 ::google::protobuf::internal::WireFormat::SERIALIZE, 314 "str"); 315 target = 316 ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 317 2, this->str(), target); 318 } 319 320 // optional int32 opt = 3; 321 if (has_opt()) { 322 target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(3, this->opt(), target); 323 } 324 325 if (!unknown_fields().empty()) { 326 target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( 327 unknown_fields(), target); 328 } 329 // @@protoc_insertion_point(serialize_to_array_end:lm.helloworld) 330 return target; 331 } 332 333 int helloworld::ByteSize() const { 334 int total_size = 0; 335 336 if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 337 // required int32 id = 1; 338 if (has_id()) { 339 total_size += 1 + 340 ::google::protobuf::internal::WireFormatLite::Int32Size( 341 this->id()); 342 } 343 344 // required string str = 2; 345 if (has_str()) { 346 total_size += 1 + 347 ::google::protobuf::internal::WireFormatLite::StringSize( 348 this->str()); 349 } 350 351 // optional int32 opt = 3; 352 if (has_opt()) { 353 total_size += 1 + 354 ::google::protobuf::internal::WireFormatLite::Int32Size( 355 this->opt()); 356 } 357 358 } 359 if (!unknown_fields().empty()) { 360 total_size += 361 ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( 362 unknown_fields()); 363 } 364 GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); 365 _cached_size_ = total_size; 366 GOOGLE_SAFE_CONCURRENT_WRITES_END(); 367 return total_size; 368 } 369 370 void helloworld::MergeFrom(const ::google::protobuf::Message& from) { 371 GOOGLE_CHECK_NE(&from, this); 372 const helloworld* source = 373 ::google::protobuf::internal::dynamic_cast_if_available<const helloworld*>( 374 &from); 375 if (source == NULL) { 376 ::google::protobuf::internal::ReflectionOps::Merge(from, this); 377 } else { 378 MergeFrom(*source); 379 } 380 } 381 382 void helloworld::MergeFrom(const helloworld& from) { 383 GOOGLE_CHECK_NE(&from, this); 384 if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { 385 if (from.has_id()) { 386 set_id(from.id()); 387 } 388 if (from.has_str()) { 389 set_str(from.str()); 390 } 391 if (from.has_opt()) { 392 set_opt(from.opt()); 393 } 394 } 395 mutable_unknown_fields()->MergeFrom(from.unknown_fields()); 396 } 397 398 void helloworld::CopyFrom(const ::google::protobuf::Message& from) { 399 if (&from == this) return; 400 Clear(); 401 MergeFrom(from); 402 } 403 404 void helloworld::CopyFrom(const helloworld& from) { 405 if (&from == this) return; 406 Clear(); 407 MergeFrom(from); 408 } 409 410 bool helloworld::IsInitialized() const { 411 if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false; 412 413 return true; 414 } 415 416 void helloworld::Swap(helloworld* other) { 417 if (other != this) { 418 std::swap(id_, other->id_); 419 std::swap(str_, other->str_); 420 std::swap(opt_, other->opt_); 421 std::swap(_has_bits_[0], other->_has_bits_[0]); 422 _unknown_fields_.Swap(&other->_unknown_fields_); 423 std::swap(_cached_size_, other->_cached_size_); 424 } 425 } 426 427 ::google::protobuf::Metadata helloworld::GetMetadata() const { 428 protobuf_AssignDescriptorsOnce(); 429 ::google::protobuf::Metadata metadata; 430 metadata.descriptor = helloworld_descriptor_; 431 metadata.reflection = helloworld_reflection_; 432 return metadata; 433 } 434 435 436 // @@protoc_insertion_point(namespace_scope) 437 438 } // namespace lm 439 440 // @@protoc_insertion_point(global_scope)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // Abstract interface for protocol messages. 2 // 3 // See also MessageLite, which contains most every-day operations. Message 4 // adds descriptors and reflection on top of that. 5 // 6 // The methods of this class that are virtual but not pure-virtual have 7 // default implementations based on reflection. Message classes which are 8 // optimized for speed will want to override these with faster implementations, 9 // but classes optimized for code size may be happy with keeping them. See 10 // the optimize_for option in descriptor.proto. 11 class LIBPROTOBUF_EXPORT Message : public MessageLite { 12 public: 13 inline Message() {} 14 virtual ~Message(); 15 16 // Basic Operations ------------------------------------------------ 17 18 // Construct a new instance of the same type. Ownership is passed to the 19 // caller. (This is also defined in MessageLite, but is defined again here 20 // for return-type covariance.) 21 virtual Message* New() const = 0; 22 23 // Make this message into a copy of the given message. The given message 24 // must have the same descriptor, but need not necessarily be the same class. 25 // By default this is just implemented as "Clear(); MergeFrom(from);". 26 virtual void CopyFrom(const Message& from); 27 28 // Merge the fields from the given message into this message. Singular 29 // fields will be overwritten, except for embedded messages which will 30 // be merged. Repeated fields will be concatenated. The given message 31 // must be of the same type as this message (i.e. the exact same class). 32 virtual void MergeFrom(const Message& from); 33 34 // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with 35 // a nice error message. 36 void CheckInitialized() const; 37 38 // Slowly build a list of all required fields that are not set. 39 // This is much, much slower than IsInitialized() as it is implemented 40 // purely via reflection. Generally, you should not call this unless you 41 // have already determined that an error exists by calling IsInitialized(). 42 void FindInitializationErrors(vector<string>* errors) const; 43 44 // Like FindInitializationErrors, but joins all the strings, delimited by 45 // commas, and returns them. 46 string InitializationErrorString() const; 47 48 // Clears all unknown fields from this message and all embedded messages. 49 // Normally, if unknown tag numbers are encountered when parsing a message, 50 // the tag and value are stored in the message's UnknownFieldSet and 51 // then written back out when the message is serialized. This allows servers 52 // which simply route messages to other servers to pass through messages 53 // that have new field definitions which they don't yet know about. However, 54 // this behavior can have security implications. To avoid it, call this 55 // method after parsing. 56 // 57 // See Reflection::GetUnknownFields() for more on unknown fields. 58 virtual void DiscardUnknownFields(); 59 60 // Computes (an estimate of) the total number of bytes currently used for 61 // storing the message in memory. The default implementation calls the 62 // Reflection object's SpaceUsed() method. 63 virtual int SpaceUsed() const; 64 65 // Debugging & Testing---------------------------------------------- 66 67 // Generates a human readable form of this message, useful for debugging 68 // and other purposes. 69 string DebugString() const; 70 // Like DebugString(), but with less whitespace. 71 string ShortDebugString() const; 72 // Like DebugString(), but do not escape UTF-8 byte sequences. 73 string Utf8DebugString() const; 74 // Convenience function useful in GDB. Prints DebugString() to stdout. 75 void PrintDebugString() const; 76 77 // Heavy I/O ------------------------------------------------------- 78 // Additional parsing and serialization methods not implemented by 79 // MessageLite because they are not supported by the lite library. 80 81 // Parse a protocol buffer from a file descriptor. If successful, the entire 82 // input will be consumed. 83 bool ParseFromFileDescriptor(int file_descriptor); 84 // Like ParseFromFileDescriptor(), but accepts messages that are missing 85 // required fields. 86 bool ParsePartialFromFileDescriptor(int file_descriptor); 87 // Parse a protocol buffer from a C++ istream. If successful, the entire 88 // input will be consumed. 89 bool ParseFromIstream(istream* input); 90 // Like ParseFromIstream(), but accepts messages that are missing 91 // required fields. 92 bool ParsePartialFromIstream(istream* input); 93 94 // Serialize the message and write it to the given file descriptor. All 95 // required fields must be set. 96 bool SerializeToFileDescriptor(int file_descriptor) const; 97 // Like SerializeToFileDescriptor(), but allows missing required fields. 98 bool SerializePartialToFileDescriptor(int file_descriptor) const; 99 // Serialize the message and write it to the given C++ ostream. All 100 // required fields must be set. 101 bool SerializeToOstream(ostream* output) const; 102 // Like SerializeToOstream(), but allows missing required fields. 103 bool SerializePartialToOstream(ostream* output) const; 104 105 106 // Reflection-based methods ---------------------------------------- 107 // These methods are pure-virtual in MessageLite, but Message provides 108 // reflection-based default implementations. 109 110 virtual string GetTypeName() const; 111 virtual void Clear(); 112 virtual bool IsInitialized() const; 113 virtual void CheckTypeAndMergeFrom(const MessageLite& other); 114 virtual bool MergePartialFromCodedStream(io::CodedInputStream* input); 115 virtual int ByteSize() const; 116 virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const; 117 118 private: 119 // This is called only by the default implementation of ByteSize(), to 120 // update the cached size. If you override ByteSize(), you do not need 121 // to override this. If you do not override ByteSize(), you MUST override 122 // this; the default implementation will crash. 123 // 124 // The method is private because subclasses should never call it; only 125 // override it. Yes, C++ lets you do that. Crazy, huh? 126 virtual void SetCachedSize(int size) const; 127 128 public: 129 130 // Introspection --------------------------------------------------- 131 132 // Typedef for backwards-compatibility. 133 typedef google::protobuf::Reflection Reflection; 134 135 // Get a Descriptor for this message's type. This describes what 136 // fields the message contains, the types of those fields, etc. 137 const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; } 138 139 // Get the Reflection interface for this Message, which can be used to 140 // read and modify the fields of the Message dynamically (in other words, 141 // without knowing the message type at compile time). This object remains 142 // property of the Message. 143 // 144 // This method remains virtual in case a subclass does not implement 145 // reflection and wants to override the default behavior. 146 virtual const Reflection* GetReflection() const { 147 return GetMetadata().reflection; 148 } 149 150 protected: 151 // Get a struct containing the metadata for the Message. Most subclasses only 152 // need to implement this method, rather than the GetDescriptor() and 153 // GetReflection() wrappers. 154 virtual Metadata GetMetadata() const = 0; 155 156 157 private: 158 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message); 159 };
3. 在需求代码中引入2中的实现
使用:
Docker与Dockerfile极简入门文档
https://blog.csdn.net/qq_33256688/article/details/80319673
posted on 2019-01-19 17:10 csuyangpeng 阅读(332) 评论(0) 编辑 收藏 举报