• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <grpc/byte_buffer.h>
20 #include <grpc/byte_buffer_reader.h>
21 #include <grpcpp/impl/grpc_library.h>
22 #include <grpcpp/support/byte_buffer.h>
23 
24 namespace grpc {
25 
26 static internal::GrpcLibraryInitializer g_gli_initializer;
27 
Dump(std::vector<Slice> * slices) const28 Status ByteBuffer::Dump(std::vector<Slice>* slices) const {
29   slices->clear();
30   if (!buffer_) {
31     return Status(StatusCode::FAILED_PRECONDITION, "Buffer not initialized");
32   }
33   grpc_byte_buffer_reader reader;
34   if (!grpc_byte_buffer_reader_init(&reader, buffer_)) {
35     return Status(StatusCode::INTERNAL,
36                   "Couldn't initialize byte buffer reader");
37   }
38   grpc_slice s;
39   while (grpc_byte_buffer_reader_next(&reader, &s)) {
40     slices->push_back(Slice(s, Slice::STEAL_REF));
41   }
42   grpc_byte_buffer_reader_destroy(&reader);
43   return Status::OK;
44 }
45 
ByteBuffer(const ByteBuffer & buf)46 ByteBuffer::ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) {
47   operator=(buf);
48 }
49 
operator =(const ByteBuffer & buf)50 ByteBuffer& ByteBuffer::operator=(const ByteBuffer& buf) {
51   if (this != &buf) {
52     Clear();  // first remove existing data
53   }
54   if (buf.buffer_) {
55     buffer_ = grpc_byte_buffer_copy(buf.buffer_);  // then copy
56   }
57   return *this;
58 }
59 
60 }  // namespace grpc
61