byte_buffer_cc.cc 1.7 KB

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