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

T* items [code]

A pointer to the first item in the ArrayView.

u32 numItems [code]

The number of items in the ArrayView.

Member Functions

ArrayView() [code]

Constructs an empty ArrayView.

ArrayView(T* items, u32 numItems) [code]

Constructs an ArrayView from the given pointer and number of items.

ArrayView(std::initializer_list<T> init) [code]

Constructs an ArrayView from a std::initializer_list<T>. Lets you pass an initializer list to any function expecting an ArrayView, as in the following:

void foo(ArrayView<u32>);
void test() {
    foo({1, 2, 3, 4, 5});
}

Use this carefully; the std::initializer_list is a temporary object that only exists for the lifetime of the statement using it.

operator const ArrayView<const T>&() const [code]

Conversion operator. Makes ArrayView<T> implicitly convertible to ArrayView<const T>.

BufferView bufferView() [code]
ConstBufferView bufferView() const [code]

Explicitly convert the ArrayView to a BufferView or ConstBufferView depending on whether the ArrayView is const.

T& operator[](u32 index) [code]
const T& operator[](u32 index) const [code]

Subscript operator with runtime bounds checking.

T& back(s32 offset = -1) [code]
const T& back(s32 offset = -1) const [code]

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.

void offsetHead(u32 ofs) [code]

Increases items and decreases numItems by ofs. Equivalent to:

*this = this->subView(ofs);
void offsetBack(s32 ofs) [code]

Adds ofs, which must be less than or equal to zero, to numItems. Equivalent to:

*this = this->subView(0, this->numItems + ofs);
explicit operator bool() const [code]

Explicit conversion to bool. Returns true if numItems is greater than 0. Allows you to use an ArrayView object inside an if condition.

if (view) {
    ...
}
bool isEmpty() const [code]

Returns true if numItems is 0.

u32 sizeBytes() const [code]

Returns the total size, in bytes, of the items in the view. Equivalent to this->numItems * sizeof(T).

ArrayView subView(u32 start) const [code]
ArrayView subView(u32 start, u32 numItems) const [code]

Returns a subview that starts at the offset given by start. The optional numItems argument determines the number of items in the subview. If numItems is not specified, the subview continues to the end of the view.

T* begin() const [code]
T* end() const [code]

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) {
    ...
}