Debian Codegraph @

spm-cpp-include/grpcpp/support/slice.h

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