2015-02-24 18:34:47 +00:00
|
|
|
/*
|
|
|
|
*
|
2017-06-07 20:57:36 +00:00
|
|
|
* Copyright 2015 gRPC authors.
|
2015-02-24 18:34:47 +00:00
|
|
|
*
|
2017-06-07 20:57:36 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-02-24 18:34:47 +00:00
|
|
|
*
|
2017-06-07 20:57:36 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-24 18:34:47 +00:00
|
|
|
*
|
2017-06-07 20:57:36 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-02-24 18:34:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-08-21 22:35:03 +00:00
|
|
|
#ifndef GRPCXX_SUPPORT_SLICE_H
|
|
|
|
#define GRPCXX_SUPPORT_SLICE_H
|
2015-02-24 18:34:47 +00:00
|
|
|
|
2015-08-21 22:35:03 +00:00
|
|
|
#include <grpc++/support/config.h>
|
2016-10-26 23:16:35 +00:00
|
|
|
#include <grpc/slice.h>
|
2015-02-24 18:34:47 +00:00
|
|
|
|
|
|
|
namespace grpc {
|
|
|
|
|
2016-10-26 23:16:06 +00:00
|
|
|
/// A wrapper around \a grpc_slice.
|
2015-09-03 16:37:02 +00:00
|
|
|
///
|
|
|
|
/// A slice represents a contiguous reference counted array of bytes.
|
|
|
|
/// It is cheap to take references to a slice, and it is cheap to create a
|
|
|
|
/// slice pointing to a subset of another slice.
|
2016-11-01 23:31:56 +00:00
|
|
|
class Slice final {
|
2015-02-24 18:34:47 +00:00
|
|
|
public:
|
2015-09-03 16:37:02 +00:00
|
|
|
/// Construct an empty slice.
|
2015-02-24 18:34:47 +00:00
|
|
|
Slice();
|
2017-05-08 06:06:01 +00:00
|
|
|
/// Destructor - drops one reference.
|
2015-02-24 18:34:47 +00:00
|
|
|
~Slice();
|
2015-09-03 16:37:02 +00:00
|
|
|
|
2015-02-24 18:34:47 +00:00
|
|
|
enum AddRef { ADD_REF };
|
2015-09-03 16:37:02 +00:00
|
|
|
/// Construct a slice from \a slice, adding a reference.
|
2016-10-26 23:16:06 +00:00
|
|
|
Slice(grpc_slice slice, AddRef);
|
2015-09-03 16:37:02 +00:00
|
|
|
|
2015-02-24 18:34:47 +00:00
|
|
|
enum StealRef { STEAL_REF };
|
2015-09-03 16:37:02 +00:00
|
|
|
/// Construct a slice from \a slice, stealing a reference.
|
2016-10-26 23:16:06 +00:00
|
|
|
Slice(grpc_slice slice, StealRef);
|
2015-09-03 16:37:02 +00:00
|
|
|
|
|
|
|
/// Copy constructor, adds a reference.
|
2015-02-24 18:34:47 +00:00
|
|
|
Slice(const Slice& other);
|
2015-09-03 16:37:02 +00:00
|
|
|
|
|
|
|
/// Assignment, reference count is unchanged.
|
2015-03-10 19:42:18 +00:00
|
|
|
Slice& operator=(Slice other) {
|
|
|
|
std::swap(slice_, other.slice_);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-02-24 18:34:47 +00:00
|
|
|
|
2015-09-03 16:37:02 +00:00
|
|
|
/// Byte size.
|
2016-10-27 04:08:10 +00:00
|
|
|
size_t size() const { return GRPC_SLICE_LENGTH(slice_); }
|
2015-09-03 16:37:02 +00:00
|
|
|
|
|
|
|
/// Raw pointer to the beginning (first element) of the slice.
|
2016-10-27 04:08:10 +00:00
|
|
|
const uint8_t* begin() const { return GRPC_SLICE_START_PTR(slice_); }
|
2015-09-03 16:37:02 +00:00
|
|
|
|
|
|
|
/// Raw pointer to the end (one byte \em past the last element) of the slice.
|
2016-10-27 04:08:10 +00:00
|
|
|
const uint8_t* end() const { return GRPC_SLICE_END_PTR(slice_); }
|
2015-02-24 18:34:47 +00:00
|
|
|
|
2016-10-26 23:16:06 +00:00
|
|
|
/// Raw C slice. Caller needs to call grpc_slice_unref when done.
|
|
|
|
grpc_slice c_slice() const { return grpc_slice_ref(slice_); }
|
2016-06-24 18:57:07 +00:00
|
|
|
|
2015-02-24 18:34:47 +00:00
|
|
|
private:
|
2015-03-10 19:42:18 +00:00
|
|
|
friend class ByteBuffer;
|
|
|
|
|
2016-10-26 23:16:06 +00:00
|
|
|
grpc_slice slice_;
|
2015-02-24 18:34:47 +00:00
|
|
|
};
|
|
|
|
|
2015-03-10 19:42:18 +00:00
|
|
|
} // namespace grpc
|
2015-02-24 18:34:47 +00:00
|
|
|
|
2016-01-14 23:27:08 +00:00
|
|
|
#endif // GRPCXX_SUPPORT_SLICE_H
|