protobuf 序列化 c++

发布于 2021-11-29 09:49:24 字数 7860 浏览 875 评论 1

大家好,请问大家一个protobuf序列化的问题,我用的pb版本是2.5.0,问题是:

生成的头文件里面只有一个SerializeWithCachedSizes方法,我想把这个类序列化成字节数组,通过网络发送出去,网上pb教程中不少提到了SerializeToArray方法,很符合我的要求,为什么我的头文件中没有这个方法呢?首次使用pb,还不是很熟悉,如果可以解决,请您尽量附上完整代码,小弟在此谢过了!!!

proto文件如下:

package ddz.proto;  
  
option optimize_for = LITE_RUNTIME;  
option java_package = "ddz.proto";  
option java_outer_classname = "Login";  
  
message LoginMessage {  
  optional string userAccount = 1;  
}
生成命令
protoc -I=./ --cpp_out=./ ./hello.proto  
生成的头文件如下:
Java代码  
// Generated by the protocol buffer compiler.  DO NOT EDIT!  
// source: hello.proto  
  
#ifndef PROTOBUF_hello_2eproto__INCLUDED  
#define PROTOBUF_hello_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_lite.h>  
#include <google/protobuf/repeated_field.h>  
#include <google/protobuf/extension_set.h>  
// @@protoc_insertion_point(includes)  
  
namespace ddz {  
namespace proto {  
  
// Internal implementation detail -- do not call these.  
void  protobuf_AddDesc_hello_2eproto();  
void protobuf_AssignDesc_hello_2eproto();  
void protobuf_ShutdownFile_hello_2eproto();  
  
class LoginMessage;  
  
// ===================================================================  
  
class LoginMessage : public ::google::protobuf::MessageLite {  
public:  
  LoginMessage();  
  virtual ~LoginMessage();  
  
  LoginMessage(ct LoginMessage& from);  
  
  inline LoginMessage& operator=(ct LoginMessage& from) {  
    CopyFrom(from);  
    return *this;  
  }  
  
  static ct LoginMessage& default_instance();  
  
  #ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER  
  // Returns the internal default instance pointer. This function can  
  // return NULL thus should not be used by the user. This is intended  
  // for Protobuf internal code. Please use default_instance() declared  
  // above instead.  
  static inline ct LoginMessage* internal_default_instance() {  
    return default_instance_;  
  }  
  #endif  
  
  void Swap(LoginMessage* other);  
  
  // implements Message ----------------------------------------------  
  
  LoginMessage* New() ct;  
  void CheckTypeAndMergeFrom(ct ::google::protobuf::MessageLite& from);  
  void CopyFrom(ct LoginMessage& from);  
  void MergeFrom(ct LoginMessage& from);  
  void Clear();  
  bool IsInitialized() ct;  
  
  int ByteSize() ct;  
  bool MergePartialFromCodedStream(  
      ::google::protobuf::io::CodedInputStream* input);  
  void SerializeWithCachedSizes(  
      ::google::protobuf::io::CodedOutputStream* output) ct;  
  int GetCachedSize() ct { return _cached_size_; }  
  private:  
  void SharedCtor();  
  void SharedDtor();  
  void SetCachedSize(int size) ct;  
  public:  
  
  ::std::string GetTypeName() ct;  
  
  // nested types ----------------------------------------------------  
  
  // accessors -------------------------------------------------------  
  
  // optional string userAccount = 1;  
  inline bool has_useraccount() ct;  
  inline void clear_useraccount();  
  static ct int kUserAccountFieldNumber = 1;  
  inline ct ::std::string& useraccount() ct;  
  inline void set_useraccount(ct ::std::string& value);  
  inline void set_useraccount(ct char* value);  
  inline void set_useraccount(ct char* value, size_t size);  
  inline ::std::string* mutable_useraccount();  
  inline ::std::string* release_useraccount();  
  inline void set_allocated_useraccount(::std::string* useraccount);  
  
  // @@protoc_insertion_point(class_scope:ddz.proto.LoginMessage)  
private:  
  inline void set_has_useraccount();  
  inline void clear_has_useraccount();  
  
  ::std::string* useraccount_;  
  
  mutable int _cached_size_;  
  ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32];  
  
  #ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER  
  friend void  protobuf_AddDesc_hello_2eproto_impl();  
  #else  
  friend void  protobuf_AddDesc_hello_2eproto();  
  #endif  
  friend void protobuf_AssignDesc_hello_2eproto();  
  friend void protobuf_ShutdownFile_hello_2eproto();  
  
  void InitAsDefaultInstance();  
  static LoginMessage* default_instance_;  
};  
// ===================================================================  
  
  
// ===================================================================  
  
// LoginMessage  
  
// optional string userAccount = 1;  
inline bool LoginMessage::has_useraccount() ct {  
  return (_has_bits_[0] & 0x00000001u) != 0;  
}  
inline void LoginMessage::set_has_useraccount() {  
  _has_bits_[0] |= 0x00000001u;  
}  
inline void LoginMessage::clear_has_useraccount() {  
  _has_bits_[0] &= ~0x00000001u;  
}  
inline void LoginMessage::clear_useraccount() {  
  if (useraccount_ != &::google::protobuf::internal::kEmptyString) {  
    useraccount_->clear();  
  }  
  clear_has_useraccount();  
}  
inline ct ::std::string& LoginMessage::useraccount() ct {  
  return *useraccount_;  
}  
inline void LoginMessage::set_useraccount(ct ::std::string& value) {  
  set_has_useraccount();  
  if (useraccount_ == &::google::protobuf::internal::kEmptyString) {  
    useraccount_ = new ::std::string;  
  }  
  useraccount_->assign(value);  
}  
inline void LoginMessage::set_useraccount(ct char* value) {  
  set_has_useraccount();  
  if (useraccount_ == &::google::protobuf::internal::kEmptyString) {  
    useraccount_ = new ::std::string;  
  }  
  useraccount_->assign(value);  
}  
inline void LoginMessage::set_useraccount(ct char* value, size_t size) {  
  set_has_useraccount();  
  if (useraccount_ == &::google::protobuf::internal::kEmptyString) {  
    useraccount_ = new ::std::string;  
  }  
  useraccount_->assign(reinterpret_cast<ct char*>(value), size);  
}  
inline ::std::string* LoginMessage::mutable_useraccount() {  
  set_has_useraccount();  
  if (useraccount_ == &::google::protobuf::internal::kEmptyString) {  
    useraccount_ = new ::std::string;  
  }  
  return useraccount_;  
}  
inline ::std::string* LoginMessage::release_useraccount() {  
  clear_has_useraccount();  
  if (useraccount_ == &::google::protobuf::internal::kEmptyString) {  
    return NULL;  
  } else {  
    ::std::string* temp = useraccount_;  
    useraccount_ = ct_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);  
    return temp;  
  }  
}  
inline void LoginMessage::set_allocated_useraccount(::std::string* useraccount) {  
  if (useraccount_ != &::google::protobuf::internal::kEmptyString) {  
    delete useraccount_;  
  }  
  if (useraccount) {  
    set_has_useraccount();  
    useraccount_ = useraccount;  
  } else {  
    clear_has_useraccount();  
    useraccount_ = ct_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString);  
  }  
}  
  
  
// @@protoc_insertion_point(namespace_scope)  
  
}  // namespace proto  
}  // namespace ddz  
  
// @@protoc_insertion_point(global_scope)  
  
#endif  // PROTOBUF_hello_2eproto__INCLUDED
查看:http://bbs.ibei#feng.com/read-htm-tid-66113.html 去掉“#”

或:http://www.dwz.cn/kmZ1b


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2021-11-29 10:04:30

PB本身就有自己的序列化啊,不是字节数据组吗?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文