GRPC C++  0.10.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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_BYTE_BUFFER_H
35 #define GRPCXX_BYTE_BUFFER_H
36 
37 #include <grpc/grpc.h>
38 #include <grpc/byte_buffer.h>
39 #include <grpc/support/log.h>
40 #include <grpc++/config.h>
41 #include <grpc++/slice.h>
42 #include <grpc++/status.h>
44 
45 #include <vector>
46 
47 namespace grpc {
48 
50  public:
51  ByteBuffer() : buffer_(nullptr) {}
52 
53  ByteBuffer(const Slice* slices, size_t nslices);
54 
56  if (buffer_) {
57  grpc_byte_buffer_destroy(buffer_);
58  }
59  }
60 
61  void Dump(std::vector<Slice>* slices) const;
62 
63  void Clear();
64  size_t Length() const;
65 
66  private:
67  friend class SerializationTraits<ByteBuffer, void>;
68 
69  ByteBuffer(const ByteBuffer&);
70  ByteBuffer& operator=(const ByteBuffer&);
71 
72  // takes ownership
73  void set_buffer(grpc_byte_buffer* buf) {
74  if (buffer_) {
75  gpr_log(GPR_ERROR, "Overriding existing buffer");
76  Clear();
77  }
78  buffer_ = buf;
79  }
80 
81  grpc_byte_buffer* buffer() const { return buffer_; }
82 
83  grpc_byte_buffer* buffer_;
84 };
85 
86 template <>
88  public:
89  static Status Deserialize(grpc_byte_buffer* byte_buffer, ByteBuffer* dest,
90  int max_message_size) {
91  dest->set_buffer(byte_buffer);
92  return Status::OK;
93  }
94  static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer,
95  bool* own_buffer) {
96  *buffer = source.buffer();
97  *own_buffer = false;
98  return Status::OK;
99  }
100 };
101 
102 } // namespace grpc
103 
104 #endif // GRPCXX_BYTE_BUFFER_H
void Clear()
Definition: byte_buffer.cc:48
static const Status & OK
Definition: status.h:49
Definition: slice.h:42
#define GRPC_FINAL
Definition: config.h:71
static Status Deserialize(grpc_byte_buffer *byte_buffer, ByteBuffer *dest, int max_message_size)
Definition: byte_buffer.h:89
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
size_t Length() const
Definition: byte_buffer.cc:68
void Dump(std::vector< Slice > *slices) const
Definition: byte_buffer.cc:55
Definition: status.h:42
~ByteBuffer()
Definition: byte_buffer.h:55
static Status Serialize(const ByteBuffer &source, grpc_byte_buffer **buffer, bool *own_buffer)
Definition: byte_buffer.h:94
ByteBuffer()
Definition: byte_buffer.h:51
Definition: byte_buffer.h:49