You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

slice.h 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. {
  27. /// A wrapper around \a grpc_slice.
  28. ///
  29. /// A slice represents a contiguous reference counted array of bytes.
  30. /// It is cheap to take references to a slice, and it is cheap to create a
  31. /// slice pointing to a subset of another slice.
  32. class Slice final
  33. {
  34. public:
  35. /// Construct an empty slice.
  36. Slice() :
  37. slice_(g_core_codegen_interface->grpc_empty_slice())
  38. {
  39. }
  40. /// Destructor - drops one reference.
  41. ~Slice()
  42. {
  43. g_core_codegen_interface->grpc_slice_unref(slice_);
  44. }
  45. enum AddRef
  46. {
  47. ADD_REF
  48. };
  49. /// Construct a slice from \a slice, adding a reference.
  50. Slice(grpc_slice slice, AddRef) :
  51. slice_(g_core_codegen_interface->grpc_slice_ref(slice))
  52. {
  53. }
  54. enum StealRef
  55. {
  56. STEAL_REF
  57. };
  58. /// Construct a slice from \a slice, stealing a reference.
  59. Slice(grpc_slice slice, StealRef) :
  60. slice_(slice)
  61. {
  62. }
  63. /// Allocate a slice of specified size
  64. explicit Slice(size_t len) :
  65. slice_(g_core_codegen_interface->grpc_slice_malloc(len))
  66. {
  67. }
  68. /// Construct a slice from a copied buffer
  69. Slice(const void* buf, size_t len) :
  70. slice_(g_core_codegen_interface->grpc_slice_from_copied_buffer(
  71. reinterpret_cast<const char*>(buf), len
  72. ))
  73. {
  74. }
  75. /// Construct a slice from a copied string
  76. /* NOLINTNEXTLINE(google-explicit-constructor) */
  77. Slice(const std::string& str) :
  78. slice_(g_core_codegen_interface->grpc_slice_from_copied_buffer(
  79. str.c_str(), str.length()
  80. ))
  81. {
  82. }
  83. enum StaticSlice
  84. {
  85. STATIC_SLICE
  86. };
  87. /// Construct a slice from a static buffer
  88. Slice(const void* buf, size_t len, StaticSlice) :
  89. slice_(g_core_codegen_interface->grpc_slice_from_static_buffer(
  90. reinterpret_cast<const char*>(buf), len
  91. ))
  92. {
  93. }
  94. /// Copy constructor, adds a reference.
  95. Slice(const Slice& other) :
  96. slice_(g_core_codegen_interface->grpc_slice_ref(other.slice_))
  97. {
  98. }
  99. /// Move constructor, steals a reference.
  100. Slice(Slice&& other) noexcept :
  101. slice_(other.slice_)
  102. {
  103. other.slice_ = g_core_codegen_interface->grpc_empty_slice();
  104. }
  105. /// Assignment, reference count is unchanged.
  106. Slice& operator=(Slice other)
  107. {
  108. std::swap(slice_, other.slice_);
  109. return *this;
  110. }
  111. /// Create a slice pointing at some data. Calls malloc to allocate a refcount
  112. /// for the object, and arranges that destroy will be called with the
  113. /// user data pointer passed in at destruction. Can be the same as buf or
  114. /// different (e.g., if data is part of a larger structure that must be
  115. /// destroyed when the data is no longer needed)
  116. Slice(void* buf, size_t len, void (*destroy)(void*), void* user_data) :
  117. slice_(g_core_codegen_interface->grpc_slice_new_with_user_data(
  118. buf, len, destroy, user_data
  119. ))
  120. {
  121. }
  122. /// Specialization of above for common case where buf == user_data
  123. Slice(void* buf, size_t len, void (*destroy)(void*)) :
  124. Slice(buf, len, destroy, buf)
  125. {
  126. }
  127. /// Similar to the above but has a destroy that also takes slice length
  128. Slice(void* buf, size_t len, void (*destroy)(void*, size_t)) :
  129. slice_(g_core_codegen_interface->grpc_slice_new_with_len(buf, len, destroy))
  130. {
  131. }
  132. /// Byte size.
  133. size_t size() const
  134. {
  135. return GRPC_SLICE_LENGTH(slice_);
  136. }
  137. /// Raw pointer to the beginning (first element) of the slice.
  138. const uint8_t* begin() const
  139. {
  140. return GRPC_SLICE_START_PTR(slice_);
  141. }
  142. /// Raw pointer to the end (one byte \em past the last element) of the slice.
  143. const uint8_t* end() const
  144. {
  145. return GRPC_SLICE_END_PTR(slice_);
  146. }
  147. /// Returns a substring of the `slice` as another slice.
  148. Slice sub(size_t begin, size_t end) const
  149. {
  150. return Slice(g_core_codegen_interface->grpc_slice_sub(slice_, begin, end), STEAL_REF);
  151. }
  152. /// Raw C slice. Caller needs to call grpc_slice_unref when done.
  153. grpc_slice c_slice() const
  154. {
  155. return g_core_codegen_interface->grpc_slice_ref(slice_);
  156. }
  157. private:
  158. friend class ByteBuffer;
  159. grpc_slice slice_;
  160. };
  161. inline grpc::string_ref StringRefFromSlice(const grpc_slice* slice)
  162. {
  163. return grpc::string_ref(
  164. reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(*slice)),
  165. GRPC_SLICE_LENGTH(*slice)
  166. );
  167. }
  168. inline std::string StringFromCopiedSlice(grpc_slice slice)
  169. {
  170. return std::string(reinterpret_cast<char*>(GRPC_SLICE_START_PTR(slice)), GRPC_SLICE_LENGTH(slice));
  171. }
  172. inline grpc_slice SliceReferencingString(const std::string& str)
  173. {
  174. return g_core_codegen_interface->grpc_slice_from_static_buffer(str.data(), str.length());
  175. }
  176. inline grpc_slice SliceFromCopiedString(const std::string& str)
  177. {
  178. return g_core_codegen_interface->grpc_slice_from_copied_buffer(str.data(), str.length());
  179. }
  180. } // namespace grpc
  181. #endif // GRPCPP_IMPL_CODEGEN_SLICE_H