12 #ifndef ROC_CORE_SLICE_H_
13 #define ROC_CORE_SLICE_H_
54 template <
class T>
class Slice {
67 data_ = buffer->data();
68 size_ = buffer->size();
78 roc_panic(
"slice: invalid range: [%lu,%lu)", (
unsigned long)from,
81 if (to > buffer.
size()) {
82 roc_panic(
"slice: out of bounds: available=[%lu,%lu) requested=[%lu,%lu)",
83 (
unsigned long)0, (
unsigned long)buffer.
size(), (
unsigned long)from,
87 data_ = buffer.
data() + from;
104 return data_ + size_;
117 return buffer_->size() - size_t(data_ - buffer_->data());
128 roc_panic(
"slice: invalid range: [%lu,%lu)", (
unsigned long)from,
132 roc_panic(
"slice: out of bounds: available=[%lu,%lu) requested=[%lu,%lu)",
133 (
unsigned long)0, (
unsigned long)cap, (
unsigned long)from,
137 data_ = data_ + from;
150 roc_panic(
"slice: extend with zero size");
152 T* ret = data_ + size_;
163 roc_panic(
"slice: invalid range: [%lu,%lu)", (
unsigned long)from,
167 roc_panic(
"slice: out of bounds: available=[%lu,%lu) requested=[%lu,%lu)",
168 (
unsigned long)0, (
unsigned long)size_, (
unsigned long)from,
172 ret.buffer_ = buffer_;
173 ret.data_ = data_ + from;
174 ret.size_ = to - from;
193 roc_panic(
"slice: out of bounds: available=[%lu,%lu) requested=%lu",
194 (
unsigned long)0, (
unsigned long)size_, (
unsigned long)i);
202 operator const struct unspecified_bool *()
const {
203 return (
const unspecified_bool*)data_;
Fixed-size dynamically-allocated buffer.
T * data()
Get buffer data.
size_t size() const
Get number of elements in buffer.
Shared ownership intrusive pointer.
Slice subslice(size_t from, size_t to) const
Construct a slice pointing to a part of this slice.
T * data_end() const
Pointer to the next after the last element in slice.
size_t capacity() const
Get maximum possible number of elements in slice.
T * data() const
Get slice data.
T * extend(const size_t add_sz)
Increase size() by add_sz.
T & operator[](const size_t i) const
Access to an element of the Slice with an array style.
Slice(const SharedPtr< Buffer< T > > &buffer)
Construct slice pointing to the whole buffer.
void reslice(size_t from, size_t to)
Change slice beginning and ending inside the buffer.
void print() const
Print slice to stderr.
Slice()
Construct empty slice.
size_t size() const
Get number of elements in slice.
Slice(Buffer< T > &buffer, size_t from, size_t to)
Construct slice pointing to a part of a buffer.
void print_buffer_slice(const uint8_t *inner, size_t inner_size, const uint8_t *outer, size_t outer_size)
Print a slice of a buffer.
#define roc_panic(...)
Print error message and terminate program gracefully.
Shared ownership intrusive pointer.