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