Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NonreflectableMessage and support Protobuf v5 #2782

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/brpc/esp_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef BRPC_ESP_HEAD_H
#define BRPC_ESP_HEAD_H

#include <cstdint>

namespace brpc {

#pragma pack(push, r1, 1)
Expand Down
90 changes: 6 additions & 84 deletions src/brpc/esp_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@

#include "esp_message.h"

#include <google/protobuf/reflection_ops.h> // ReflectionOps::Merge
#include <google/protobuf/wire_format.h> // WireFormatLite::GetTagWireType

#include "brpc/proto_base.pb.h"
#include "butil/logging.h"

namespace brpc {

EspMessage::EspMessage()
: ::google::protobuf::Message() {
SharedCtor();
}

EspMessage::EspMessage(const EspMessage& from)
: ::google::protobuf::Message() {
: NonreflectableMessage<EspMessage>() {
SharedCtor();
MergeFrom(from);
}

void EspMessage::SharedCtor() {
Expand All @@ -46,91 +38,21 @@ EspMessage::~EspMessage() {
void EspMessage::SharedDtor() {
}

const ::google::protobuf::Descriptor* EspMessage::descriptor() {
return EspMessageBase::descriptor();
}

EspMessage* EspMessage::New() const {
return new EspMessage;
}

#if GOOGLE_PROTOBUF_VERSION >= 3006000
EspMessage* EspMessage::New(::google::protobuf::Arena* arena) const {
return CreateMaybeMessage<EspMessage>(arena);
}
#endif

void EspMessage::Clear() {
head.body_len = 0;
body.clear();
}

bool EspMessage::MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!(EXPRESSION)) return false
::google::protobuf::uint32 tag;

while ((tag = input->ReadTag()) != 0) {
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
return true;
}
}
return true;
#undef DO_
}

void EspMessage::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream*) const {
}

::google::protobuf::uint8* EspMessage::SerializeWithCachedSizesToArray(
::google::protobuf::uint8* target) const {
return target;
}

int EspMessage::ByteSize() const {
size_t EspMessage::ByteSizeLong() const {
return sizeof(head) + body.size();
}

void EspMessage::MergeFrom(const ::google::protobuf::Message& from) {
CHECK_NE(&from, this);
const EspMessage* source = dynamic_cast<const EspMessage*>(&from);
if (source == NULL) {
::google::protobuf::internal::ReflectionOps::Merge(from, this);
} else {
MergeFrom(*source);
}
}

void EspMessage::MergeFrom(const EspMessage& from) {
CHECK_NE(&from, this);
head = from.head;
body = from.body;
}

void EspMessage::CopyFrom(const ::google::protobuf::Message& from) {
if (&from == this) {
return;
}

Clear();
MergeFrom(from);
}

void EspMessage::CopyFrom(const EspMessage& from) {
if (&from == this) {
return;
}

Clear();
MergeFrom(from);
}

bool EspMessage::IsInitialized() const {
return true;
}

void EspMessage::Swap(EspMessage* other) {
if (other != this) {
const EspHead tmp = other->head;
Expand All @@ -141,9 +63,9 @@ void EspMessage::Swap(EspMessage* other) {
}

::google::protobuf::Metadata EspMessage::GetMetadata() const {
::google::protobuf::Metadata metadata;
metadata.descriptor = EspMessage::descriptor();
metadata.reflection = NULL;
::google::protobuf::Metadata metadata{};
metadata.descriptor = EspMessageBase::descriptor();
metadata.reflection = nullptr;
return metadata;
}

Expand Down
48 changes: 8 additions & 40 deletions src/brpc/esp_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,32 @@
#ifndef BRPC_ESP_MESSAGE_H
#define BRPC_ESP_MESSAGE_H

#include <string>

#include <google/protobuf/message.h>
#include <google/protobuf/generated_message_reflection.h> // dynamic_cast_if_available
#include <google/protobuf/reflection_ops.h> // ReflectionOps::Merge

#include "brpc/esp_head.h"
#include "butil/iobuf.h"
#include "brpc/proto_base.pb.h"
#include "brpc/pb_compat.h"
#include "brpc/nonreflectable_message.h"
#include "butil/iobuf.h"

namespace brpc {

class EspMessage : public ::google::protobuf::Message {
class EspMessage : public NonreflectableMessage<EspMessage> {
public:
EspHead head;
butil::IOBuf body;

public:
EspMessage();
virtual ~EspMessage();

EspMessage(const EspMessage& from);

inline EspMessage& operator=(const EspMessage& from) {
CopyFrom(from);
return *this;
}

static const ::google::protobuf::Descriptor* descriptor();
static const EspMessage& default_instance();
~EspMessage() override;

void Swap(EspMessage* other);

// implements Message ----------------------------------------------

EspMessage* New() const PB_319_OVERRIDE;
#if GOOGLE_PROTOBUF_VERSION >= 3006000
EspMessage* New(::google::protobuf::Arena* arena) const override;
#endif
void CopyFrom(const ::google::protobuf::Message& from) PB_321_OVERRIDE;
void MergeFrom(const ::google::protobuf::Message& from) override;
void CopyFrom(const EspMessage& from);
void MergeFrom(const EspMessage& from);
void MergeFrom(const EspMessage& from) override;
void Clear() override;
bool IsInitialized() const override;

int ByteSize() const;
bool MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input) PB_310_OVERRIDE;
void SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const PB_310_OVERRIDE;
::google::protobuf::uint8* SerializeWithCachedSizesToArray(
::google::protobuf::uint8* output) const PB_310_OVERRIDE;
int GetCachedSize() const PB_422_OVERRIDE { return ByteSize(); }
size_t ByteSizeLong() const override;
int GetCachedSize() const PB_425_OVERRIDE { return ByteSize(); }

protected:
::google::protobuf::Metadata GetMetadata() const override;
::google::protobuf::Metadata GetMetadata() const PB_527_OVERRIDE;

private:
void SharedCtor();
Expand Down
Loading
Loading