GRPC C++  1.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
byte_buffer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 GRPCXX_SUPPORT_BYTE_BUFFER_H
20 #define GRPCXX_SUPPORT_BYTE_BUFFER_H
21 
23 #include <grpc++/support/config.h>
24 #include <grpc++/support/slice.h>
25 #include <grpc++/support/status.h>
26 #include <grpc/byte_buffer.h>
27 #include <grpc/grpc.h>
28 #include <grpc/support/log.h>
29 
30 #include <vector>
31 
32 namespace grpc {
33 
35 class ByteBuffer final {
36  public:
38  ByteBuffer() : buffer_(nullptr) {}
39 
41  ByteBuffer(const Slice* slices, size_t nslices);
42 
45  ByteBuffer(const ByteBuffer& buf);
46 
47  ~ByteBuffer();
48 
50 
52  Status Dump(std::vector<Slice>* slices) const;
53 
55  void Clear();
56 
58  size_t Length() const;
59 
61  void Swap(ByteBuffer* other);
62 
63  private:
64  friend class SerializationTraits<ByteBuffer, void>;
65 
66  // takes ownership
67  void set_buffer(grpc_byte_buffer* buf) {
68  if (buffer_) {
69  Clear();
70  }
71  buffer_ = buf;
72  }
73 
74  // For \a SerializationTraits's usage.
75  grpc_byte_buffer* buffer() const { return buffer_; }
76 
77  grpc_byte_buffer* buffer_;
78 };
79 
80 template <>
82  public:
83  static Status Deserialize(grpc_byte_buffer* byte_buffer, ByteBuffer* dest) {
84  dest->set_buffer(byte_buffer);
85  return Status::OK;
86  }
87  static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer,
88  bool* own_buffer) {
89  *buffer = grpc_byte_buffer_copy(source.buffer());
90  *own_buffer = true;
91  return Status::OK;
92  }
93 };
94 
95 } // namespace grpc
96 
97 #endif // GRPCXX_SUPPORT_BYTE_BUFFER_H
void Clear()
Remove all data.
A wrapper around grpc_slice.
Definition: slice.h:32
Definition: grpc_types.h:41
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:49
size_t Length() const
Buffer size in bytes.
Status Dump(std::vector< Slice > *slices) const
Dump (read) the buffer contents into slices.
GRPCAPI grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)
Copies input byte buffer bb.
ByteBuffer & operator=(const ByteBuffer &)
static Status Deserialize(grpc_byte_buffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:83
Did it work? If it didn't, why?
Definition: status.h:30
static Status Serialize(const ByteBuffer &source, grpc_byte_buffer **buffer, bool *own_buffer)
Definition: byte_buffer.h:87
static const Status & OK
An OK pre-defined instance.
Definition: status.h:51
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:38
A sequence of bytes.
Definition: byte_buffer.h:35