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