slice
is a template over one type. Its declaration looks like this:
template <typename T>
class slice
-
slice
is formattable. -
slice
is not default constructible. -
slice
can be constructed from a STL container such as astd::vector
orstd::array
. In order for something to be passed into theslice
for construction, it must havedata()
andsize()
functions available. -
slice
has a "subslice" constructor, where you pass in something (astd::vector
, or anotherzl::slice
) and then provide two numbers:from
andto
.from
is the beginning index of the subslice in the container, inclusive, andto
is the ending index, exclusive. -
A
slice
can be iterated over, both const and nonconst. For example:
std::vector ints;
for (size_t i = 0; i < 20; i ++) {
ints.push_back(i);
}
zl::slice ints_pointer(ints);
for (int integer : ints_pointer) {
// do something with the integer
}
zl::slice half_the_ints(ints, 0, int.size() / 2);
for (int integer : half_the_ints) {
// do something with the integer
}
-
T* data()
- Returns a non-const pointer to the head of the data pointed at by the
slice
. This pointer is guaranteed to not be null.
- Returns a non-const pointer to the head of the data pointed at by the
-
const T* data() const
- const variant of previous function.
-
size_t size() const
- Returns the number of items the slice is pointing at.
-
slice<T> raw_slice(T& data, size_t size)
- Takes in
data
, which is a reference to the first item in an array ofT
.size
is how many of those items you want to point at. Returns a slice that points at those things. - This is the only memory-unsafe part of
slice
(besides double-free and use-after-free potential due to raw pointer storage).
- Takes in