GRPC C++  1.3.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, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_SUPPORT_BYTE_BUFFER_H
35 #define GRPCXX_SUPPORT_BYTE_BUFFER_H
36 
38 #include <grpc++/support/config.h>
39 #include <grpc++/support/slice.h>
40 #include <grpc++/support/status.h>
41 #include <grpc/byte_buffer.h>
42 #include <grpc/grpc.h>
43 #include <grpc/support/log.h>
44 
45 #include <vector>
46 
47 namespace grpc {
48 
50 class ByteBuffer final {
51  public:
53  ByteBuffer() : buffer_(nullptr) {}
54 
56  ByteBuffer(const Slice* slices, size_t nslices);
57 
60  ByteBuffer(const ByteBuffer& buf);
61 
62  ~ByteBuffer();
63 
65 
67  Status Dump(std::vector<Slice>* slices) const;
68 
70  void Clear();
71 
73  size_t Length() const;
74 
76  void Swap(ByteBuffer* other);
77 
78  private:
79  friend class SerializationTraits<ByteBuffer, void>;
80 
81  // takes ownership
82  void set_buffer(grpc_byte_buffer* buf) {
83  if (buffer_) {
84  Clear();
85  }
86  buffer_ = buf;
87  }
88 
89  // For \a SerializationTraits's usage.
90  grpc_byte_buffer* buffer() const { return buffer_; }
91 
92  grpc_byte_buffer* buffer_;
93 };
94 
95 template <>
97  public:
98  static Status Deserialize(grpc_byte_buffer* byte_buffer, ByteBuffer* dest) {
99  dest->set_buffer(byte_buffer);
100  return Status::OK;
101  }
102  static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer,
103  bool* own_buffer) {
104  *buffer = grpc_byte_buffer_copy(source.buffer());
105  *own_buffer = true;
106  return Status::OK;
107  }
108 };
109 
110 } // namespace grpc
111 
112 #endif // GRPCXX_SUPPORT_BYTE_BUFFER_H
void Clear()
Remove all data.
A wrapper around grpc_slice.
Definition: slice.h:47
Definition: grpc_types.h:55
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
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:98
Did it work? If it didn't, why?
Definition: status.h:45
static Status Serialize(const ByteBuffer &source, grpc_byte_buffer **buffer, bool *own_buffer)
Definition: byte_buffer.h:102
static const Status & OK
An OK pre-defined instance.
Definition: status.h:63
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:53
A sequence of bytes.
Definition: byte_buffer.h:50