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
ArrayViewfrom the given pointer and number of items. -
Constructs an
ArrayViewfrom 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
ArrayViewfrom aStringVieworMutableStringView. -
Explicitly convert the
ArrayViewto aStringVieworMutableStringView. 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.
-1returns the last item in the view;-2returns the second-last item, etc. -
Increases
itemsand decreasesnumItemsbyofs. 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. ReturnstrueifnumItemsis greater than 0. Allows you to use anArrayViewobject inside anifcondition.if (view) { ... } -
Returns
trueifnumItemsis 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 optionalnumItemsargument determines the number of items in the subview. IfnumItemsis not specified, the subview continues to the end of the view. -
Returns a subview with the last
numItemsitems 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.
aandbcan have different item types. Returnstrueif and only ifaandbhave the same number of items anda[i] == b[i]evaluates totrueat every indexi.