GRPC C++  1.26.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_IMPL_CODEGEN_SLICE_H
20 #define GRPCPP_IMPL_CODEGEN_SLICE_H
21 
25 
27 
28 namespace grpc {
29 
35 class Slice final {
36  public:
41 
42  enum AddRef { ADD_REF };
45  : slice_(g_core_codegen_interface->grpc_slice_ref(slice)) {}
46 
47  enum StealRef { STEAL_REF };
49  Slice(grpc_slice slice, StealRef) : slice_(slice) {}
50 
52  Slice(size_t len)
54 
56  Slice(const void* buf, size_t len)
58  reinterpret_cast<const char*>(buf), len)) {}
59 
61  Slice(const grpc::string& str)
63  str.c_str(), str.length())) {}
64 
66 
68  Slice(const void* buf, size_t len, StaticSlice)
70  reinterpret_cast<const char*>(buf), len)) {}
71 
73  Slice(const Slice& other)
74  : slice_(g_core_codegen_interface->grpc_slice_ref(other.slice_)) {}
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)
89  buf, len, destroy, user_data)) {}
90 
92  Slice(void* buf, size_t len, void (*destroy)(void*))
93  : Slice(buf, len, destroy, buf) {}
94 
96  Slice(void* buf, size_t len, void (*destroy)(void*, size_t))
98  destroy)) {}
99 
101  size_t size() const { return GRPC_SLICE_LENGTH(slice_); }
102 
104  const uint8_t* begin() const { return GRPC_SLICE_START_PTR(slice_); }
105 
107  const uint8_t* end() const { return GRPC_SLICE_END_PTR(slice_); }
108 
110  grpc_slice c_slice() const {
112  }
113 
114  private:
115  friend class ByteBuffer;
116 
117  grpc_slice slice_;
118 };
119 
121  return grpc::string_ref(
122  reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(*slice)),
123  GRPC_SLICE_LENGTH(*slice));
124 }
125 
127  return grpc::string(reinterpret_cast<char*>(GRPC_SLICE_START_PTR(slice)),
128  GRPC_SLICE_LENGTH(slice));
129 }
130 
133  str.length());
134 }
135 
138  str.length());
139 }
140 
141 } // namespace grpc
142 
143 #endif // GRPCPP_IMPL_CODEGEN_SLICE_H
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...
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 c_slice() const
Raw C slice. Caller needs to call grpc_slice_unref when done.
Definition: slice.h:110
~Slice()
Destructor - drops one reference.
Definition: slice.h:40
StaticSlice
Definition: slice.h:65
grpc::string StringFromCopiedSlice(grpc_slice slice)
Definition: slice.h:126
std::string string
Definition: config.h:35
StealRef
Definition: slice.h:47
grpc::string_ref StringRefFromSlice(const grpc_slice *slice)
Definition: slice.h:120
const uint8_t * end() const
Raw pointer to the end (one byte past the last element) of the slice.
Definition: slice.h:107
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Create a slice by copying a buffer.
Slice(const grpc::string &str)
Construct a slice from a copied string.
Definition: slice.h:61
Slice(const void *buf, size_t len)
Construct a slice from a copied buffer.
Definition: slice.h:56
GPRAPI grpc_slice grpc_slice_from_static_buffer(const void *source, size_t len)
Create a slice pointing to constant memory.
virtual grpc_slice grpc_slice_from_copied_buffer(const void *buffer, size_t length)=0
A wrapper around grpc_slice.
Definition: slice.h:35
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:60
Slice(const Slice &other)
Copy constructor, adds a reference.
Definition: slice.h:73
const uint8_t * begin() const
Raw pointer to the beginning (first element) of the slice.
Definition: slice.h:104
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 SliceReferencingString(const grpc::string &str)
Definition: slice.h:131
#define GRPC_SLICE_START_PTR(slice)
Definition: slice.h:96
#define GRPC_SLICE_END_PTR(slice)
Definition: slice.h:105
size_t size() const
Byte size.
Definition: slice.h:101
grpc_slice SliceFromCopiedString(const grpc::string &str)
Definition: slice.h:136
Definition: slice.h:42
Slice(const void *buf, size_t len, StaticSlice)
Construct a slice from a static buffer.
Definition: slice.h:68
Definition: slice.h:47
Slice(void *buf, size_t len, void(*destroy)(void *), void *user_data)
Create a slice pointing at some data.
Definition: slice.h:87
virtual grpc_slice grpc_slice_from_static_buffer(const void *buffer, size_t length)=0
Slice()
Construct an empty slice.
Definition: slice.h:38
Definition: slice.h:65
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Slice(grpc_slice slice, AddRef)
Construct a slice from slice, adding a reference.
Definition: slice.h:44
Slice(void *buf, size_t len, void(*destroy)(void *))
Specialization of above for common case where buf == user_data.
Definition: slice.h:92
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:90
virtual grpc_slice grpc_slice_ref(grpc_slice slice)=0
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:96
This class is a non owning reference to a string.
Definition: string_ref.h:41
virtual void grpc_slice_unref(grpc_slice slice)=0
GPRAPI grpc_slice grpc_slice_ref(grpc_slice s)
Increment the refcount of s.
#define GRPC_SLICE_LENGTH(slice)
Definition: slice.h:99
Slice & operator=(Slice other)
Assignment, reference count is unchanged.
Definition: slice.h:77
AddRef
Definition: slice.h:42
A sequence of bytes.
Definition: byte_buffer.h:67
Slice(size_t len)
Allocate a slice of specified size.
Definition: slice.h:52
Slice(grpc_slice slice, StealRef)
Construct a slice from slice, stealing a reference.
Definition: slice.h:49
GPRAPI grpc_slice grpc_empty_slice(void)