mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 09:52:40 +00:00
Use the Google Style for clang-format without exceptions (#8706)
This reduces the friction when merging from github and google repos by using the exact same clang style guide. MARKDOWN=true
This commit is contained in:
@@ -30,23 +30,24 @@ namespace grpc {
|
||||
// `grpc_slice` and also provides flatbuffers-specific helpers such as `Verify`
|
||||
// and `GetRoot`. Since it is backed by a `grpc_slice`, the underlying buffer
|
||||
// is refcounted and ownership is be managed automatically.
|
||||
template<class T> class Message {
|
||||
template <class T>
|
||||
class Message {
|
||||
public:
|
||||
Message() {}
|
||||
|
||||
Message(::grpc::Slice slice) : slice_(slice) {}
|
||||
|
||||
Message &operator=(const Message &other) = delete;
|
||||
Message& operator=(const Message& other) = delete;
|
||||
|
||||
Message(Message &&other) = default;
|
||||
Message(Message&& other) = default;
|
||||
|
||||
Message(const Message &other) = delete;
|
||||
Message(const Message& other) = delete;
|
||||
|
||||
Message &operator=(Message &&other) = default;
|
||||
Message& operator=(Message&& other) = default;
|
||||
|
||||
const uint8_t *mutable_data() const { return slice_.begin(); }
|
||||
const uint8_t* mutable_data() const { return slice_.begin(); }
|
||||
|
||||
const uint8_t *data() const { return slice_.begin(); }
|
||||
const uint8_t* data() const { return slice_.begin(); }
|
||||
|
||||
size_t size() const { return slice_.size(); }
|
||||
|
||||
@@ -55,12 +56,12 @@ template<class T> class Message {
|
||||
return verifier.VerifyBuffer<T>(nullptr);
|
||||
}
|
||||
|
||||
T *GetMutableRoot() { return flatbuffers::GetMutableRoot<T>(mutable_data()); }
|
||||
T* GetMutableRoot() { return flatbuffers::GetMutableRoot<T>(mutable_data()); }
|
||||
|
||||
const T *GetRoot() const { return flatbuffers::GetRoot<T>(data()); }
|
||||
const T* GetRoot() const { return flatbuffers::GetRoot<T>(data()); }
|
||||
|
||||
// This is only intended for serializer use, or if you know what you're doing
|
||||
const ::grpc::Slice &BorrowSlice() const { return slice_; }
|
||||
const ::grpc::Slice& BorrowSlice() const { return slice_; }
|
||||
|
||||
private:
|
||||
::grpc::Slice slice_;
|
||||
@@ -75,41 +76,41 @@ class SliceAllocator : public Allocator {
|
||||
public:
|
||||
SliceAllocator() {}
|
||||
|
||||
SliceAllocator(const SliceAllocator &other) = delete;
|
||||
SliceAllocator &operator=(const SliceAllocator &other) = delete;
|
||||
SliceAllocator(const SliceAllocator& other) = delete;
|
||||
SliceAllocator& operator=(const SliceAllocator& other) = delete;
|
||||
|
||||
SliceAllocator(SliceAllocator &&other) {
|
||||
SliceAllocator(SliceAllocator&& other) {
|
||||
// default-construct and swap idiom
|
||||
swap(other);
|
||||
}
|
||||
|
||||
SliceAllocator &operator=(SliceAllocator &&other) {
|
||||
SliceAllocator& operator=(SliceAllocator&& other) {
|
||||
// move-construct and swap idiom
|
||||
SliceAllocator temp(std::move(other));
|
||||
swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(SliceAllocator &other) {
|
||||
void swap(SliceAllocator& other) {
|
||||
using std::swap;
|
||||
swap(slice_, other.slice_);
|
||||
}
|
||||
|
||||
virtual ~SliceAllocator() {}
|
||||
|
||||
virtual uint8_t *allocate(size_t size) override {
|
||||
virtual uint8_t* allocate(size_t size) override {
|
||||
FLATBUFFERS_ASSERT(slice_.size() == 0);
|
||||
slice_ = ::grpc::Slice(size);
|
||||
return const_cast<uint8_t *>(slice_.begin());
|
||||
return const_cast<uint8_t*>(slice_.begin());
|
||||
}
|
||||
|
||||
virtual void deallocate(uint8_t *p, size_t size) override {
|
||||
virtual void deallocate(uint8_t* p, size_t size) override {
|
||||
FLATBUFFERS_ASSERT(p == slice_.begin());
|
||||
FLATBUFFERS_ASSERT(size == slice_.size());
|
||||
slice_ = ::grpc::Slice();
|
||||
}
|
||||
|
||||
virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
|
||||
virtual uint8_t* reallocate_downward(uint8_t* old_p, size_t old_size,
|
||||
size_t new_size, size_t in_use_back,
|
||||
size_t in_use_front) override {
|
||||
FLATBUFFERS_ASSERT(old_p == slice_.begin());
|
||||
@@ -117,15 +118,15 @@ class SliceAllocator : public Allocator {
|
||||
FLATBUFFERS_ASSERT(new_size > old_size);
|
||||
::grpc::Slice old_slice = slice_;
|
||||
::grpc::Slice new_slice = ::grpc::Slice(new_size);
|
||||
uint8_t *new_p = const_cast<uint8_t *>(new_slice.begin());
|
||||
uint8_t* new_p = const_cast<uint8_t*>(new_slice.begin());
|
||||
memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
|
||||
in_use_front);
|
||||
slice_ = new_slice;
|
||||
return const_cast<uint8_t *>(slice_.begin());
|
||||
return const_cast<uint8_t*>(slice_.begin());
|
||||
}
|
||||
|
||||
private:
|
||||
::grpc::Slice &get_slice(uint8_t *p, size_t size) {
|
||||
::grpc::Slice& get_slice(uint8_t* p, size_t size) {
|
||||
FLATBUFFERS_ASSERT(p == slice_.begin());
|
||||
FLATBUFFERS_ASSERT(size == slice_.size());
|
||||
return slice_;
|
||||
@@ -153,24 +154,24 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
explicit MessageBuilder(uoffset_t initial_size = 1024)
|
||||
: FlatBufferBuilder(initial_size, &slice_allocator_, false) {}
|
||||
|
||||
MessageBuilder(const MessageBuilder &other) = delete;
|
||||
MessageBuilder &operator=(const MessageBuilder &other) = delete;
|
||||
MessageBuilder(const MessageBuilder& other) = delete;
|
||||
MessageBuilder& operator=(const MessageBuilder& other) = delete;
|
||||
|
||||
MessageBuilder(MessageBuilder &&other)
|
||||
MessageBuilder(MessageBuilder&& other)
|
||||
: FlatBufferBuilder(1024, &slice_allocator_, false) {
|
||||
// Default construct and swap idiom.
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
/// Create a MessageBuilder from a FlatBufferBuilder.
|
||||
explicit MessageBuilder(FlatBufferBuilder &&src,
|
||||
void (*dealloc)(void *,
|
||||
explicit MessageBuilder(FlatBufferBuilder&& src,
|
||||
void (*dealloc)(void*,
|
||||
size_t) = &DefaultAllocator::dealloc)
|
||||
: FlatBufferBuilder(1024, &slice_allocator_, false) {
|
||||
src.Swap(*this);
|
||||
src.SwapBufAllocator(*this);
|
||||
if (buf_.capacity()) {
|
||||
uint8_t *buf = buf_.scratch_data(); // pointer to memory
|
||||
uint8_t* buf = buf_.scratch_data(); // pointer to memory
|
||||
size_t capacity = buf_.capacity(); // size of memory
|
||||
slice_allocator_.slice_ = ::grpc::Slice(buf, capacity, dealloc);
|
||||
} else {
|
||||
@@ -181,21 +182,21 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
/// Move-assign a FlatBufferBuilder to a MessageBuilder.
|
||||
/// Only FlatBufferBuilder with default allocator (basically, nullptr) is
|
||||
/// supported.
|
||||
MessageBuilder &operator=(FlatBufferBuilder &&src) {
|
||||
MessageBuilder& operator=(FlatBufferBuilder&& src) {
|
||||
// Move construct a temporary and swap
|
||||
MessageBuilder temp(std::move(src));
|
||||
Swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
MessageBuilder &operator=(MessageBuilder &&other) {
|
||||
MessageBuilder& operator=(MessageBuilder&& other) {
|
||||
// Move construct a temporary and swap
|
||||
MessageBuilder temp(std::move(other));
|
||||
Swap(temp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Swap(MessageBuilder &other) {
|
||||
void Swap(MessageBuilder& other) {
|
||||
slice_allocator_.swap(other.slice_allocator_);
|
||||
FlatBufferBuilder::Swap(other);
|
||||
// After swapping the FlatBufferBuilder, we swap back the allocator, which
|
||||
@@ -209,8 +210,8 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
// Releases the ownership of the buffer pointer.
|
||||
// Returns the size, offset, and the original grpc_slice that
|
||||
// allocated the buffer. Also see grpc_slice_unref().
|
||||
uint8_t *ReleaseRaw(size_t &size, size_t &offset, ::grpc::Slice &slice) {
|
||||
uint8_t *buf = FlatBufferBuilder::ReleaseRaw(size, offset);
|
||||
uint8_t* ReleaseRaw(size_t& size, size_t& offset, ::grpc::Slice& slice) {
|
||||
uint8_t* buf = FlatBufferBuilder::ReleaseRaw(size, offset);
|
||||
slice = slice_allocator_.slice_;
|
||||
slice_allocator_.slice_ = ::grpc::Slice();
|
||||
return buf;
|
||||
@@ -221,7 +222,8 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
// GetMessage extracts the subslice of the buffer corresponding to the
|
||||
// flatbuffers-encoded region and wraps it in a `Message<T>` to handle buffer
|
||||
// ownership.
|
||||
template<class T> Message<T> GetMessage() {
|
||||
template <class T>
|
||||
Message<T> GetMessage() {
|
||||
auto buf_data = buf_.scratch_data(); // pointer to memory
|
||||
auto buf_size = buf_.capacity(); // size of memory
|
||||
auto msg_data = buf_.data(); // pointer to msg
|
||||
@@ -243,7 +245,8 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
return msg;
|
||||
}
|
||||
|
||||
template<class T> Message<T> ReleaseMessage() {
|
||||
template <class T>
|
||||
Message<T> ReleaseMessage() {
|
||||
Message<T> msg = GetMessage<T>();
|
||||
Reset();
|
||||
return msg;
|
||||
@@ -258,10 +261,11 @@ class MessageBuilder : private detail::SliceAllocatorMember,
|
||||
|
||||
namespace grpc {
|
||||
|
||||
template<class T> class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
||||
template <class T>
|
||||
class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
||||
public:
|
||||
static grpc::Status Serialize(const flatbuffers::grpc::Message<T> &msg,
|
||||
ByteBuffer *buffer, bool *own_buffer) {
|
||||
static grpc::Status Serialize(const flatbuffers::grpc::Message<T>& msg,
|
||||
ByteBuffer* buffer, bool* own_buffer) {
|
||||
// Package the single slice into a `ByteBuffer`,
|
||||
// incrementing the refcount in the process.
|
||||
*buffer = ByteBuffer(&msg.BorrowSlice(), 1);
|
||||
@@ -270,8 +274,8 @@ template<class T> class SerializationTraits<flatbuffers::grpc::Message<T>> {
|
||||
}
|
||||
|
||||
// Deserialize by pulling the
|
||||
static grpc::Status Deserialize(ByteBuffer *buf,
|
||||
flatbuffers::grpc::Message<T> *msg) {
|
||||
static grpc::Status Deserialize(ByteBuffer* buf,
|
||||
flatbuffers::grpc::Message<T>* msg) {
|
||||
Slice slice;
|
||||
if (!buf->TrySingleSlice(&slice).ok()) {
|
||||
if (!buf->DumpToSingleSlice(&slice).ok()) {
|
||||
|
||||
Reference in New Issue
Block a user