GRPC C++  1.62.0
slice.h
Go to the documentation of this file.
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 #ifndef GRPCPP_SUPPORT_SLICE_H
20 #define GRPCPP_SUPPORT_SLICE_H
21 
22 #include <grpc/slice.h>
23 #include <grpcpp/support/config.h>
25 
26 namespace grpc {
27 
33 class Slice final {
34  public:
36  Slice() : slice_(grpc_empty_slice()) {}
38  ~Slice() { grpc_slice_unref(slice_); }
39 
40  enum AddRef { ADD_REF };
42  Slice(grpc_slice slice, AddRef) : slice_(grpc_slice_ref(slice)) {}
43 
44  enum StealRef { STEAL_REF };
46  Slice(grpc_slice slice, StealRef) : slice_(slice) {}
47 
49  explicit Slice(size_t len) : slice_(grpc_slice_malloc(len)) {}
50 
52  Slice(const void* buf, size_t len)
53  : slice_(grpc_slice_from_copied_buffer(reinterpret_cast<const char*>(buf),
54  len)) {}
55 
57  // NOLINTNEXTLINE(google-explicit-constructor)
58  Slice(const std::string& str)
59  : slice_(grpc_slice_from_copied_buffer(str.c_str(), str.length())) {}
60 
62 
64  Slice(const void* buf, size_t len, StaticSlice)
65  : slice_(grpc_slice_from_static_buffer(reinterpret_cast<const char*>(buf),
66  len)) {}
67 
69  Slice(const Slice& other) : slice_(grpc_slice_ref(other.slice_)) {}
70 
72  Slice(Slice&& other) noexcept : slice_(other.slice_) {
73  other.slice_ = grpc_empty_slice();
74  }
75 
77  Slice& operator=(Slice other) {
78  std::swap(slice_, other.slice_);
79  return *this;
80  }
81 
87  Slice(void* buf, size_t len, void (*destroy)(void*), void* user_data)
88  : slice_(grpc_slice_new_with_user_data(buf, len, destroy, user_data)) {}
89 
91  Slice(void* buf, size_t len, void (*destroy)(void*))
92  : Slice(buf, len, destroy, buf) {}
93 
95  Slice(void* buf, size_t len, void (*destroy)(void*, size_t))
96  : slice_(grpc_slice_new_with_len(buf, len, destroy)) {}
97 
99  size_t size() const { return GRPC_SLICE_LENGTH(slice_); }
100 
102  const uint8_t* begin() const { return GRPC_SLICE_START_PTR(slice_); }
103 
105  const uint8_t* end() const { return GRPC_SLICE_END_PTR(slice_); }
106 
108  Slice sub(size_t begin, size_t end) const {
109  return Slice(grpc_slice_sub(slice_, begin, end), STEAL_REF);
110  }
111 
113  grpc_slice c_slice() const { return grpc_slice_ref(slice_); }
114 
115  private:
116  friend class ByteBuffer;
117 
118  grpc_slice slice_;
119 };
120 
122  return grpc::string_ref(
123  reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(*slice)),
124  GRPC_SLICE_LENGTH(*slice));
125 }
126 
127 inline std::string StringFromCopiedSlice(grpc_slice slice) {
128  return std::string(reinterpret_cast<char*>(GRPC_SLICE_START_PTR(slice)),
129  GRPC_SLICE_LENGTH(slice));
130 }
131 
132 inline grpc_slice SliceReferencingString(const std::string& str) {
133  return grpc_slice_from_static_buffer(str.data(), str.length());
134 }
135 
136 inline grpc_slice SliceFromCopiedString(const std::string& str) {
137  return grpc_slice_from_copied_buffer(str.data(), str.length());
138 }
139 
140 } // namespace grpc
141 
142 #endif // GRPCPP_SUPPORT_SLICE_H
grpc_slice_new_with_user_data
GPRAPI grpc_slice grpc_slice_new_with_user_data(void *p, size_t len, void(*destroy)(void *), void *user_data)
Equivalent to grpc_slice_new, but with a separate pointer that is passed to the destroy function.
grpc::StringRefFromSlice
grpc::string_ref StringRefFromSlice(const grpc_slice *slice)
Definition: slice.h:121
grpc::string_ref
This class is a non owning reference to a string.
Definition: string_ref.h:41
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
grpc::Slice::STATIC_SLICE
@ STATIC_SLICE
Definition: slice.h:61
grpc::Slice::Slice
Slice(size_t len)
Allocate a slice of specified size.
Definition: slice.h:49
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: slice_type.h:99
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
GRPC_SLICE_END_PTR
#define GRPC_SLICE_END_PTR(slice)
Definition: slice_type.h:108
grpc_slice_new_with_len
GPRAPI grpc_slice grpc_slice_new_with_len(void *p, size_t len, void(*destroy)(void *, size_t))
Equivalent to grpc_slice_new, but with a two argument destroy function that also takes the slice leng...
grpc::Slice::begin
const uint8_t * begin() const
Raw pointer to the beginning (first element) of the slice.
Definition: slice.h:102
grpc::SliceFromCopiedString
grpc_slice SliceFromCopiedString(const std::string &str)
Definition: slice.h:136
grpc::Slice::sub
Slice sub(size_t begin, size_t end) const
Returns a substring of the slice as another slice.
Definition: slice.h:108
grpc_slice_malloc
GPRAPI grpc_slice grpc_slice_malloc(size_t length)
Equivalent to grpc_slice_new(malloc(len), len, free), but saves one malloc() call.
grpc::Slice::ADD_REF
@ ADD_REF
Definition: slice.h:40
grpc::Slice::Slice
Slice(const void *buf, size_t len)
Construct a slice from a copied buffer.
Definition: slice.h:52
grpc::Slice::end
const uint8_t * end() const
Raw pointer to the end (one byte past the last element) of the slice.
Definition: slice.h:105
grpc::Slice::Slice
Slice(const Slice &other)
Copy constructor, adds a reference.
Definition: slice.h:69
grpc::Slice::STEAL_REF
@ STEAL_REF
Definition: slice.h:44
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Create a slice by copying a buffer.
grpc::Slice::Slice
Slice(void *buf, size_t len, void(*destroy)(void *, size_t))
Similar to the above but has a destroy that also takes slice length.
Definition: slice.h:95
grpc_slice_ref
GPRAPI grpc_slice grpc_slice_ref(grpc_slice s)
Increment the refcount of s.
grpc::Slice::size
size_t size() const
Byte size.
Definition: slice.h:99
grpc::Slice::StealRef
StealRef
Definition: slice.h:44
grpc::Slice::operator=
Slice & operator=(Slice other)
Assignment, reference count is unchanged.
Definition: slice.h:77
grpc::Slice::Slice
Slice()
Construct an empty slice.
Definition: slice.h:36
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:60
grpc_slice
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice_type.h:63
grpc::Slice::AddRef
AddRef
Definition: slice.h:40
grpc_slice_sub
GPRAPI grpc_slice grpc_slice_sub(grpc_slice s, size_t begin, size_t end)
Return a result slice derived from s, which shares a ref count with s, where result....
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:33
grpc::Slice::c_slice
grpc_slice c_slice() const
Raw C slice. Caller needs to call grpc_slice_unref when done.
Definition: slice.h:113
grpc::Slice::~Slice
~Slice()
Destructor - drops one reference.
Definition: slice.h:38
slice.h
grpc_slice_from_static_buffer
GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len)
Create a slice pointing to constant memory.
grpc::Slice::StaticSlice
StaticSlice
Definition: slice.h:61
config.h
grpc::Slice::Slice
Slice(const std::string &str)
Construct a slice from a copied string.
Definition: slice.h:58
grpc::Slice::Slice
Slice(grpc_slice slice, StealRef)
Construct a slice from slice, stealing a reference.
Definition: slice.h:46
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Decrement the ref count of s.
grpc::Slice::Slice
Slice(const void *buf, size_t len, StaticSlice)
Construct a slice from a static buffer.
Definition: slice.h:64
grpc::Slice::Slice
Slice(grpc_slice slice, AddRef)
Construct a slice from slice, adding a reference.
Definition: slice.h:42
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: slice_type.h:102
grpc::Slice::Slice
Slice(Slice &&other) noexcept
Move constructor, steals a reference.
Definition: slice.h:72
grpc::Slice::Slice
Slice(void *buf, size_t len, void(*destroy)(void *), void *user_data)
Create a slice pointing at some data.
Definition: slice.h:87
grpc::Slice::Slice
Slice(void *buf, size_t len, void(*destroy)(void *))
Specialization of above for common case where buf == user_data.
Definition: slice.h:91
string_ref.h
grpc::StringFromCopiedSlice
std::string StringFromCopiedSlice(grpc_slice slice)
Definition: slice.h:127
grpc::SliceReferencingString
grpc_slice SliceReferencingString(const std::string &str)
Definition: slice.h:132