byte_buffer_cc.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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)
  42. : buffer_(grpc_byte_buffer_copy(buf.buffer_)) {}
  43. ByteBuffer& ByteBuffer::operator=(const ByteBuffer& buf) {
  44. if (this != &buf) {
  45. Clear(); // first remove existing data
  46. }
  47. if (buf.buffer_) {
  48. buffer_ = grpc_byte_buffer_copy(buf.buffer_); // then copy
  49. }
  50. return *this;
  51. }
  52. } // namespace grpc