#include <Containers/ArrayView.h>
template<class T>
ArrayView class
Array view.
A non-owning wrapper around a sized continuous range of data, similar to a dynamic std::
Usage
The class is implicitly convertible from compile-time-sized C arrays and Array / StaticArray instances of the same underlying type; it's also possible to implicitly create a const
view on a mutable array. Besides that, a view can be created manually from a pointer and size.
In addition there are ArrayView<void> / ArrayView<const void> specializations, which are meant to be used in cases where APIs accept opaque blobs of data, such as writing data to a file. These provide no element access, only data pointer and size in bytes, and an ArrayView of any type is convertible to them.
Data access
The class provides the usual C++ container interface — data(), size() and empty(); subscript access via operator T*(), range access via begin() / end(), and their overloads and acess to the front() and back() element, if the view is non-empty. The view itself is immutable and thus all member functions are const
, but if the underlying type is mutable the references and pointers returned from these are mutable as well.
View slicing
Except for the usual element access via begin(), end() and operator T*() that provides also access via []
, there's a collection of slicing functions — slice(), sliceSize(), prefix(), suffix(), exceptPrefix() and exceptSuffix(). The slice(), sliceSize(), prefix(), suffix() APIs accept also a pointer as the begin/end arguments. As a special case, if prefix() and suffix() are called with nullptr
, they return a zero-sized nullptr
view.
Finally, the slicing operations provide a conversion to StaticArrayView. Compile-time-sized arrays are useful for APIs that want to enforce a particular number of elements at compile time.
All slice operations fire an assert if the arguments are out of range, for StaticArrayView conversions the checks are done at compile time when possible.
Convenience functions and type conversion
To avoid having to specify the full type when constructing a view or when you need to disambiguate in a function call, the arrayView() (or staticArrayView()) function can be used. These also provide a safe conversion from std::reinterpret_cast
, arrayCast() performs a cast, recalculates the view size accordingly and additionally checks that the type change makes sense for given type combination and array size. Finally, arraySize() can be used to query size of compile-time C arrays in a failproof way.
Public types
- using Type = T
- Element type.
Constructors, destructors, conversion operators
-
ArrayView(std::
nullptr_t = nullptr) constexpr noexcept - Default constructor.
-
ArrayView(T* data,
std::
size_t size) constexpr noexcept - Construct a view on an array with explicit size.
-
template<class U, std::ArrayView(U(&data)[size]) constexpr noexcept
size_t size> - Construct a view on a fixed-size array.
-
template<class U>ArrayView(ArrayView<U> view) constexpr noexcept
- Construct a view on an ArrayView.
-
template<std::ArrayView(StaticArrayView<size, U> view) constexpr noexcept
size_t size, class U> - Construct a view on a StaticArrayView.
-
template<class U, class = decltype(Implementation::ArrayViewConverter<T, typename std::ArrayView(U&& other) constexpr noexcept
decay<U && >::type>::from(std:: declval<U && >()))> - Construct a view on an external type / from an external representation.
-
template<class U, class = decltype(Implementation::ArrayViewConverter<T, U>::to(std::operator U() const constexpr
declval<ArrayView<T>>()))> - Convert the view to external representation.
- operator bool() const explicit constexpr
- Whether the view is non-empty.
- operator T*() const constexpr
- Conversion to the underlying type.
Public functions
- auto data() const -> T* constexpr
- View data.
-
auto size() const -> std::
size_t constexpr - View size.
- auto empty() const -> bool constexpr
- Whether the view is empty.
- auto begin() const -> T* constexpr
- Pointer to the first element.
- auto cbegin() const -> T* constexpr
- auto end() const -> T* constexpr
- Pointer to (one item after) the last element.
- auto cend() const -> T* constexpr
- auto front() const -> T& constexpr
- First element.
- auto back() const -> T& constexpr
- Last element.
-
auto operator[](std::
size_t i) const -> T& constexpr - Element access.
- auto slice(T* begin, T* end) const -> ArrayView<T> constexpr
- View slice.
-
auto slice(std::
size_t begin, std:: size_t end) const -> ArrayView<T> constexpr -
auto sliceSize(T* begin,
std::
size_t size) const -> ArrayView<T> constexpr - View slice of given size.
-
auto sliceSize(std::
size_t begin, std:: size_t size) const -> ArrayView<T> constexpr -
template<std::auto slice(T* begin) const -> StaticArrayView<size_, T> constexpr
size_t size_> - Fixed-size view slice.
-
template<std::auto slice(std::
size_t viewSize> size_t begin) const -> StaticArrayView<viewSize, T> constexpr -
template<std::auto slice() const -> StaticArrayView<end_ - begin_, T> constexpr
size_t begin_, std:: size_t end_> - Fixed-size view slice.
-
template<std::auto sliceSize() const -> StaticArrayView<size_, T> constexpr
size_t begin_, std:: size_t size_> - Fixed-size view slice of given size.
- auto prefix(T* end) const -> ArrayView<T> constexpr
- View prefix until a pointer.
- auto suffix(T* begin) const -> ArrayView<T> constexpr
- View suffix after a pointer.
-
auto prefix(std::
size_t size) const -> ArrayView<T> constexpr - View on the first
size
items. -
template<std::auto prefix() const -> StaticArrayView<size_, T> constexpr
size_t size_> - Fixed-size view on the first
size_
items. -
template<std::auto suffix() const -> StaticArrayView<size_, T> constexpr
size_t size_> - Fixed-size view on the last
size_
items. -
auto exceptPrefix(std::
size_t size) const -> ArrayView<T> constexpr - View except the first
size
items. -
auto exceptSuffix(std::
size_t size) const -> ArrayView<T> constexpr - View except the last
size
items. -
template<std::auto slice(std::
size_t size_> size_t begin) const -> StaticArrayView<size_, T> constexpr
Function documentation
template<class T>
Death:: Containers:: ArrayView<T>:: ArrayView(std:: nullptr_t = nullptr) constexpr noexcept
Default constructor.
Creates an empty nullptr
view. Copy a non-empty Array or ArrayView onto the instance to make it useful.
template<class T>
Death:: Containers:: ArrayView<T>:: ArrayView(T* data,
std:: size_t size) constexpr noexcept
Construct a view on an array with explicit size.
Parameters | |
---|---|
data | Data pointer |
size | Data size |
template<class T>
template<class U, std:: size_t size>
Death:: Containers:: ArrayView<T>:: ArrayView(U(&data)[size]) constexpr noexcept
Construct a view on a fixed-size array.
Parameters | |
---|---|
data | Fixed-size array |
Enabled only if T*
is implicitly convertible to U*
. Expects that both types have the same size.
template<class T>
template<std:: size_t size, class U>
Death:: Containers:: ArrayView<T>:: ArrayView(StaticArrayView<size, U> view) constexpr noexcept
Construct a view on a StaticArrayView.
Enabled only if T*
is implicitly convertible to U*
. Expects that both types have the same size.
template<class T>
T* Death:: Containers:: ArrayView<T>:: cbegin() const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
T* Death:: Containers:: ArrayView<T>:: cend() const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
T& Death:: Containers:: ArrayView<T>:: front() const constexpr
First element.
Expects there is at least one element.
template<class T>
T& Death:: Containers:: ArrayView<T>:: back() const constexpr
Last element.
Expects there is at least one element.
template<class T>
T& Death:: Containers:: ArrayView<T>:: operator[](std:: size_t i) const constexpr
Element access.
Expects that i
is less than size().
template<class T>
ArrayView<T> Death:: Containers:: ArrayView<T>:: slice(std:: size_t begin,
std:: size_t end) const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
ArrayView<T> Death:: Containers:: ArrayView<T>:: sliceSize(T* begin,
std:: size_t size) const constexpr
View slice of given size.
Equivalent to data.slice(begin, begin + size)
.
template<class T>
ArrayView<T> Death:: Containers:: ArrayView<T>:: sliceSize(std:: size_t begin,
std:: size_t size) const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
template<std:: size_t size_>
StaticArrayView<size_, T> Death:: Containers:: ArrayView<T>:: slice(T* begin) const constexpr
Fixed-size view slice.
Both begin
and begin + size_
are expected to be in range.
template<class T>
template<std:: size_t viewSize>
StaticArrayView<viewSize, T> Death:: Containers:: ArrayView<T>:: slice(std:: size_t begin) const constexpr
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
template<std:: size_t begin_, std:: size_t end_>
StaticArrayView<end_ - begin_, T> Death:: Containers:: ArrayView<T>:: slice() const constexpr
Fixed-size view slice.
At compile time expects that begin < end_
, at runtime that end_
is not larger than size().
template<class T>
template<std:: size_t begin_, std:: size_t size_>
StaticArrayView<size_, T> Death:: Containers:: ArrayView<T>:: sliceSize() const constexpr
Fixed-size view slice of given size.
Expects that begin_ + size_
is not larger than size().
template<class T>
ArrayView<T> Death:: Containers:: ArrayView<T>:: prefix(std:: size_t size) const constexpr
View on the first size
items.
Equivalent to data.slice(0, size)
.
template<class T>
template<std:: size_t size_>
StaticArrayView<size_, T> Death:: Containers:: ArrayView<T>:: prefix() const constexpr
Fixed-size view on the first size_
items.
Equivalent to data.slice<0, size_>()
.
template<class T>
template<std:: size_t size_>
StaticArrayView<size_, T> Death:: Containers:: ArrayView<T>:: suffix() const constexpr
Fixed-size view on the last size_
items.
Equivalent to data.slice<size_>(data.size() - size_)
.
template<class T>
ArrayView<T> Death:: Containers:: ArrayView<T>:: exceptPrefix(std:: size_t size) const constexpr
View except the first size
items.
Equivalent to data.slice(size, data.size())
.
template<class T>
ArrayView<T> Death:: Containers:: ArrayView<T>:: exceptSuffix(std:: size_t size) const constexpr
View except the last size
items.
Equivalent to data.slice(0, data.size() - size)
.
template<class T>
template<class T>
ArrayView<T> arrayView(T* data,
std:: size_t size) constexpr
Make a view on an array of specific length.
Convenience alternative to ArrayView::
template<class T>
template<std:: size_t size, class T>
ArrayView<T> arrayView(T(&data)[size]) constexpr
Make a view on fixed-size array.
Convenience alternative to ArrayView::
template<class T>
template<class T>
ArrayView<const T> arrayView(std:: initializer_list<T> list)
Make a view on an initializer list.
Not present as a constructor in order to avoid accidental dangling references with r-value initializer lists.
template<class T>
template<std:: size_t size, class T>
ArrayView<T> arrayView(StaticArrayView<size, T> view) constexpr
Make a view on StaticArrayView.
Convenience alternative to ArrayView::
template<class T>
template<class T>
ArrayView<T> arrayView(ArrayView<T> view) constexpr
Make a view on a view.
Equivalent to the implicit ArrayView copy constructor — it shouldn't be an error to call arrayView() on itself.
template<class T>
template<class U, class T>
ArrayView<U> arrayCast(ArrayView<T> view)
Reinterpret-cast an array view.
Size of the new array is calculated as view.size()*sizeof(T)/sizeof(U)
. Expects that both types are standard layout and the total byte size doesn't change.
template<class T>
template<class U>
ArrayView<U> arrayCast(ArrayView<const void> view)
Reinterpret-cast a void array view.
Size of the new array is calculated as view.size()/sizeof(U)
. Expects that the target type is standard layout and the total byte size doesn't change.
template<class T>
template<class T>
std:: size_t arraySize(ArrayView<T> view) constexpr
Array view size.
Alias to ArrayView::