ArrayView
An ArrayView
references a range of typed items in memory. It consists of a pointer items
and an
integer numItems
. An ArrayView
does not own the memory it points to, and no heap memory is freed
when the ArrayView
is destroyed.
When an ArrayView
is const
, the items its references are immutable. When an ArrayView
is not
const
, the items its references are mutable only if the item type T
is not const
.
ArrayView<T>
is implicitly convertible to ArrayView<const T>
and can be passed as an argument to
any function that expects ArrayView<const T>
.
Header File
#include <ply-runtime/container/ArrayView.h>
Also included from <ply-runtime/Base.h>
.
Data Members
-
A pointer to the first item in the
ArrayView
. -
The number of items in the
ArrayView
.
Member Functions
-
Constructs an empty
ArrayView
. -
Constructs an
ArrayView
from the given pointer and number of items. -
Constructs an
ArrayView
from a braced initializer list. Lets you pass an initializer list to any function expecting anArrayView
, as in the following:void foo(ArrayView<u32>); void test() { foo({1, 2, 3, 4, 5}); }
Use this carefully; the underlying array only exists for the lifetime of the statement calling this constructor.
ArrayView::operator ArrayView<const T>() const
[code]-
Conversion operator. Makes
ArrayView<T>
implicitly convertible toArrayView<const T>
. -
Explicitly create an
ArrayView
from aStringView
orMutableStringView
. -
Explicitly convert the
ArrayView
to aStringView
orMutableStringView
. T& ArrayView::operator[](u32 index)
[code]const T& ArrayView::operator[](u32 index) const
[code]-
Subscript operator with runtime bounds checking.
-
Reverse subscript operator with runtime bound checking. Expects a negative index.
-1
returns the last item in the view;-2
returns the second-last item, etc. -
Increases
items
and decreasesnumItems
byofs
. Equivalent to:*this = this->subView(ofs);
-
Adds
ofs
, which must be less than or equal to zero, tonumItems
. Equivalent to:*this = this->subView(0, this->numItems + ofs);
explicit ArrayView::operator bool() const
[code]-
Explicit conversion to
bool
. Returnstrue
ifnumItems
is greater than 0. Allows you to use anArrayView
object inside anif
condition.if (view) { ... }
-
Returns
true
ifnumItems
is 0. -
Returns the total size, in bytes, of the items in the view. Equivalent to
this->numItems * sizeof(T)
. -
Returns a subview that starts at the offset given by
start
. The optionalnumItems
argument determines the number of items in the subview. IfnumItems
is not specified, the subview continues to the end of the view. -
Returns a subview with the last
numItems
items of the view omitted. -
Required functions to support range-for syntax. Allows you to iterate over all the items in the view as follows:
for (const T& item : view) { ... }
-
template <typename Arr0, typename Arr1, typename T0 = details::ArrayViewType<Arr0>, typename T1 = details::ArrayViewType<Arr1>>
bool operator==(Arr0&& a, Arr1&& b)
[code] -
Compares two array-like objects.
a
andb
can have different item types. Returnstrue
if and only ifa
andb
have the same number of items anda[i] == b[i]
evaluates totrue
at every indexi
.