| 31 | // and `GetRoot`. Since it is backed by a `grpc_slice`, the underlying buffer |
| 32 | // is refcounted and ownership is be managed automatically. |
| 33 | template<class T> class Message { |
| 34 | public: |
| 35 | Message() : slice_(grpc_empty_slice()) {} |
| 36 | |
| 37 | Message(grpc_slice slice, bool add_ref) |
| 38 | : slice_(add_ref ? grpc_slice_ref(slice) : slice) {} |
| 39 | |
| 40 | Message &operator=(const Message &other) = delete; |
| 41 | |
| 42 | Message(Message &&other) : slice_(other.slice_) { |
| 43 | other.slice_ = grpc_empty_slice(); |
| 44 | } |
| 45 | |
| 46 | Message(const Message &other) = delete; |
| 47 | |
| 48 | Message &operator=(Message &&other) { |
| 49 | grpc_slice_unref(slice_); |
| 50 | slice_ = other.slice_; |
| 51 | other.slice_ = grpc_empty_slice(); |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | ~Message() { grpc_slice_unref(slice_); } |
| 56 | |
| 57 | const uint8_t *mutable_data() const { return GRPC_SLICE_START_PTR(slice_); } |
| 58 | |
| 59 | const uint8_t *data() const { return GRPC_SLICE_START_PTR(slice_); } |
| 60 | |
| 61 | size_t size() const { return GRPC_SLICE_LENGTH(slice_); } |
| 62 | |
| 63 | bool Verify() const { |
| 64 | Verifier verifier(data(), size()); |
| 65 | return verifier.VerifyBuffer<T>(nullptr); |
| 66 | } |
| 67 | |
| 68 | T *GetMutableRoot() { return flatbuffers::GetMutableRoot<T>(mutable_data()); } |
| 69 | |
| 70 | const T *GetRoot() const { return flatbuffers::GetRoot<T>(data()); } |
| 71 | |
| 72 | // This is only intended for serializer use, or if you know what you're doing |
| 73 | const grpc_slice &BorrowSlice() const { return slice_; } |
| 74 | |
| 75 | private: |
| 76 | grpc_slice slice_; |
| 77 | }; |
| 78 | |
| 79 | class MessageBuilder; |
| 80 | |