GRPC C++  1.10.0
byte_buffer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
20 #define GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
21 
23 
29 
30 #include <vector>
31 
32 namespace grpc {
33 
34 namespace internal {
35 class CallOpSendMessage;
36 template <class R>
39 class MethodHandler;
40 template <class ServiceType, class RequestType, class ResponseType>
42 template <class ServiceType, class RequestType, class ResponseType>
44 template <class R>
46 } // namespace internal
48 class ByteBuffer final {
49  public:
51  ByteBuffer() : buffer_(nullptr) {}
52 
54  ByteBuffer(const Slice* slices, size_t nslices);
55 
58  ByteBuffer(const ByteBuffer& buf);
59 
61  if (buffer_) {
63  }
64  }
65 
66  ByteBuffer& operator=(const ByteBuffer&);
67 
69  Status Dump(std::vector<Slice>* slices) const;
70 
72  void Clear() {
73  if (buffer_) {
75  buffer_ = nullptr;
76  }
77  }
78 
82  void Duplicate() {
84  }
85 
88  void Release() { buffer_ = nullptr; }
89 
91  size_t Length() const;
92 
94  void Swap(ByteBuffer* other);
95 
97  bool Valid() const { return (buffer_ != nullptr); }
98 
99  private:
100  friend class SerializationTraits<ByteBuffer, void>;
102  template <class R>
106  template <class ServiceType, class RequestType, class ResponseType>
108  template <class ServiceType, class RequestType, class ResponseType>
110  template <class R>
112 
113  grpc_byte_buffer* buffer_;
114 
115  // takes ownership
116  void set_buffer(grpc_byte_buffer* buf) {
117  if (buffer_) {
118  Clear();
119  }
120  buffer_ = buf;
121  }
122 
123  grpc_byte_buffer* c_buffer() { return buffer_; }
124  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
125 
126  class ByteBufferPointer {
127  public:
128  ByteBufferPointer(const ByteBuffer* b)
129  : bbuf_(const_cast<ByteBuffer*>(b)) {}
130  operator ByteBuffer*() { return bbuf_; }
131  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
132  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
133 
134  private:
135  ByteBuffer* bbuf_;
136  };
137  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
138 };
139 
140 template <>
142  public:
143  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
144  dest->set_buffer(byte_buffer->buffer_);
145  return Status::OK;
146  }
147  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
148  bool* own_buffer) {
149  *buffer = source;
150  *own_buffer = true;
151  return Status::OK;
152  }
153 };
154 
155 } // namespace grpc
156 
157 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
void Clear()
Remove all data.
Definition: byte_buffer.h:72
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:43
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:147
A wrapper around grpc_slice.
Definition: slice.h:35
void Duplicate()
Make a duplicate copy of the internals of this byte buffer so that we have our own owned version of i...
Definition: byte_buffer.h:82
Definition: grpc_types.h:40
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:97
Definition: byte_buffer.h:45
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:88
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:143
Definition: call.h:268
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:41
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
Definition: byte_buffer.h:37
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
Base class for running an RPC handler.
Definition: rpc_service_method.h:38
Did it work? If it didn&#39;t, why?
Definition: status.h:30
~ByteBuffer()
Definition: byte_buffer.h:60
static const Status & OK
An OK pre-defined instance.
Definition: status.h:51
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:51
A sequence of bytes.
Definition: byte_buffer.h:48