slice.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef GRPCPP_IMPL_CODEGEN_SLICE_H
  19. #define GRPCPP_IMPL_CODEGEN_SLICE_H
  20. // IWYU pragma: private, include <grpcpp/support/slice.h>
  21. #include <grpc/impl/codegen/slice.h>
  22. #include <grpcpp/impl/codegen/config.h>
  23. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  24. #include <grpcpp/impl/codegen/string_ref.h>
  25. namespace grpc {
  26. /// A wrapper around \a grpc_slice.
  27. ///
  28. /// A slice represents a contiguous reference counted array of bytes.
  29. /// It is cheap to take references to a slice, and it is cheap to create a
  30. /// slice pointing to a subset of another slice.
  31. class Slice final {
  32. public:
  33. /// Construct an empty slice.
  34. Slice() : slice_(g_core_codegen_interface->grpc_empty_slice()) {}
  35. /// Destructor - drops one reference.
  36. ~Slice() { g_core_codegen_interface->grpc_slice_unref(slice_); }
  37. enum AddRef { ADD_REF };
  38. /// Construct a slice from \a slice, adding a reference.
  39. Slice(grpc_slice slice, AddRef)
  40. : slice_(g_core_codegen_interface->grpc_slice_ref(slice)) {}
  41. enum StealRef { STEAL_REF };
  42. /// Construct a slice from \a slice, stealing a reference.
  43. Slice(grpc_slice slice, StealRef) : slice_(slice) {}
  44. /// Allocate a slice of specified size
  45. explicit Slice(size_t len)
  46. : slice_(g_core_codegen_interface->grpc_slice_malloc(len)) {}
  47. /// Construct a slice from a copied buffer
  48. Slice(const void* buf, size_t len)
  49. : slice_(g_core_codegen_interface->grpc_slice_from_copied_buffer(
  50. reinterpret_cast<const char*>(buf), len)) {}
  51. /// Construct a slice from a copied string
  52. /* NOLINTNEXTLINE(google-explicit-constructor) */
  53. Slice(const std::string& str)
  54. : slice_(g_core_codegen_interface->grpc_slice_from_copied_buffer(
  55. str.c_str(), str.length())) {}
  56. enum StaticSlice { STATIC_SLICE };
  57. /// Construct a slice from a static buffer
  58. Slice(const void* buf, size_t len, StaticSlice)
  59. : slice_(g_core_codegen_interface->grpc_slice_from_static_buffer(
  60. reinterpret_cast<const char*>(buf), len)) {}
  61. /// Copy constructor, adds a reference.
  62. Slice(const Slice& other)
  63. : slice_(g_core_codegen_interface->grpc_slice_ref(other.slice_)) {}
  64. /// Move constructor, steals a reference.
  65. Slice(Slice&& other) noexcept : slice_(other.slice_) {
  66. other.slice_ = g_core_codegen_interface->grpc_empty_slice();
  67. }
  68. /// Assignment, reference count is unchanged.
  69. Slice& operator=(Slice other) {
  70. std::swap(slice_, other.slice_);
  71. return *this;
  72. }
  73. /// Create a slice pointing at some data. Calls malloc to allocate a refcount
  74. /// for the object, and arranges that destroy will be called with the
  75. /// user data pointer passed in at destruction. Can be the same as buf or
  76. /// different (e.g., if data is part of a larger structure that must be
  77. /// destroyed when the data is no longer needed)
  78. Slice(void* buf, size_t len, void (*destroy)(void*), void* user_data)
  79. : slice_(g_core_codegen_interface->grpc_slice_new_with_user_data(
  80. buf, len, destroy, user_data)) {}
  81. /// Specialization of above for common case where buf == user_data
  82. Slice(void* buf, size_t len, void (*destroy)(void*))
  83. : Slice(buf, len, destroy, buf) {}
  84. /// Similar to the above but has a destroy that also takes slice length
  85. Slice(void* buf, size_t len, void (*destroy)(void*, size_t))
  86. : slice_(g_core_codegen_interface->grpc_slice_new_with_len(buf, len,
  87. destroy)) {}
  88. /// Byte size.
  89. size_t size() const { return GRPC_SLICE_LENGTH(slice_); }
  90. /// Raw pointer to the beginning (first element) of the slice.
  91. const uint8_t* begin() const { return GRPC_SLICE_START_PTR(slice_); }
  92. /// Raw pointer to the end (one byte \em past the last element) of the slice.
  93. const uint8_t* end() const { return GRPC_SLICE_END_PTR(slice_); }
  94. /// Returns a substring of the `slice` as another slice.
  95. Slice sub(size_t begin, size_t end) const {
  96. return Slice(g_core_codegen_interface->grpc_slice_sub(slice_, begin, end),
  97. STEAL_REF);
  98. }
  99. /// Raw C slice. Caller needs to call grpc_slice_unref when done.
  100. grpc_slice c_slice() const {
  101. return g_core_codegen_interface->grpc_slice_ref(slice_);
  102. }
  103. private:
  104. friend class ByteBuffer;
  105. grpc_slice slice_;
  106. };
  107. inline grpc::string_ref StringRefFromSlice(const grpc_slice* slice) {
  108. return grpc::string_ref(
  109. reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(*slice)),
  110. GRPC_SLICE_LENGTH(*slice));
  111. }
  112. inline std::string StringFromCopiedSlice(grpc_slice slice) {
  113. return std::string(reinterpret_cast<char*>(GRPC_SLICE_START_PTR(slice)),
  114. GRPC_SLICE_LENGTH(slice));
  115. }
  116. inline grpc_slice SliceReferencingString(const std::string& str) {
  117. return g_core_codegen_interface->grpc_slice_from_static_buffer(str.data(),
  118. str.length());
  119. }
  120. inline grpc_slice SliceFromCopiedString(const std::string& str) {
  121. return g_core_codegen_interface->grpc_slice_from_copied_buffer(str.data(),
  122. str.length());
  123. }
  124. } // namespace grpc
  125. #endif // GRPCPP_IMPL_CODEGEN_SLICE_H