GRPC C++  1.11.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:
38  Slice();
40  ~Slice();
41 
42  enum AddRef { ADD_REF };
44  Slice(grpc_slice slice, AddRef);
45 
46  enum StealRef { STEAL_REF };
48  Slice(grpc_slice slice, StealRef);
49 
51  Slice(size_t len);
52 
54  Slice(const void* buf, size_t len);
55 
57  Slice(const grpc::string& str);
58 
60 
62  Slice(const void* buf, size_t len, StaticSlice);
63 
65  Slice(const Slice& other);
66 
68  Slice& operator=(Slice other) {
69  std::swap(slice_, other.slice_);
70  return *this;
71  }
72 
78  Slice(void* buf, size_t len, void (*destroy)(void*), void* user_data);
79 
81  Slice(void* buf, size_t len, void (*destroy)(void*))
82  : Slice(buf, len, destroy, buf) {}
83 
85  Slice(void* buf, size_t len, void (*destroy)(void*, size_t));
86 
88  size_t size() const { return GRPC_SLICE_LENGTH(slice_); }
89 
91  const uint8_t* begin() const { return GRPC_SLICE_START_PTR(slice_); }
92 
94  const uint8_t* end() const { return GRPC_SLICE_END_PTR(slice_); }
95 
97  grpc_slice c_slice() const;
98 
99  private:
100  friend class ByteBuffer;
101 
102  grpc_slice slice_;
103 };
104 
106  return grpc::string_ref(
107  reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(*slice)),
108  GRPC_SLICE_LENGTH(*slice));
109 }
110 
112  return grpc::string(reinterpret_cast<char*>(GRPC_SLICE_START_PTR(slice)),
113  GRPC_SLICE_LENGTH(slice));
114 }
115 
118  str.length());
119 }
120 
123  str.length());
124 }
125 
126 } // namespace grpc
127 
128 #endif // GRPCPP_IMPL_CODEGEN_SLICE_H
grpc_slice c_slice() const
Raw C slice. Caller needs to call grpc_slice_unref when done.
~Slice()
Destructor - drops one reference.
StaticSlice
Definition: slice.h:59
grpc::string StringFromCopiedSlice(grpc_slice slice)
Definition: slice.h:111
std::string string
Definition: config.h:35
StealRef
Definition: slice.h:46
grpc::string_ref StringRefFromSlice(const grpc_slice *slice)
Definition: slice.h:105
const uint8_t * end() const
Raw pointer to the end (one byte past the last element) of the slice.
Definition: slice.h:94
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:80
const uint8_t * begin() const
Raw pointer to the beginning (first element) of the slice.
Definition: slice.h:91
grpc_slice SliceReferencingString(const grpc::string &str)
Definition: slice.h:116
#define GRPC_SLICE_START_PTR(slice)
Definition: slice.h:116
#define GRPC_SLICE_END_PTR(slice)
Definition: slice.h:125
size_t size() const
Byte size.
Definition: slice.h:88
grpc_slice SliceFromCopiedString(const grpc::string &str)
Definition: slice.h:121
Definition: slice.h:42
Definition: slice.h:46
virtual grpc_slice grpc_slice_from_static_buffer(const void *buffer, size_t length)=0
Slice()
Construct an empty slice.
Definition: slice.h:59
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
Slice(void *buf, size_t len, void(*destroy)(void *))
Specialization of above for common case where buf == user_data.
Definition: slice.h:81
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
This class is a non owning reference to a string.
Definition: string_ref.h:41
#define GRPC_SLICE_LENGTH(slice)
Definition: slice.h:119
Slice & operator=(Slice other)
Assignment, reference count is unchanged.
Definition: slice.h:68
AddRef
Definition: slice.h:42
A sequence of bytes.
Definition: byte_buffer.h:50