CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::StridedMemoryView< T > Class Template Reference

A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally changing its type) with a Vector-like interface, excluding the methods which change a vector's size. More...

#include <CeresEngine/Foundation/Container/MemoryView.hpp>

Classes

class  IteratorImpl
 

Public Types

using value_type = T
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 
using reference = T &
 
using const_reference = const T &
 
using pointer = T *
 
using const_pointer = const T *
 
using iterator = IteratorImpl< false >
 
using const_iterator = IteratorImpl< true >
 
using reverse_iterator = std::reverse_iterator< iterator >
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 
using ValueType = value_type
 
using SizeType = size_type
 
using DifferenceType = difference_type
 
using Reference = reference
 
using ConstReference = const_reference
 
using Pointer = pointer
 
using ConstPointer = const_pointer
 
using Iterator = iterator
 
using ConstIterator = const_iterator
 
using ReverseIterator = reverse_iterator
 
using ConstReverseIterator = const_reverse_iterator
 

Public Member Functions

 StridedMemoryView ()=default
 Creates a new strided memory view with length 0.
 
 StridedMemoryView (std::nullptr_t)
 Creates a new strided memory view with length 0.
 
 StridedMemoryView (const Pointer ptr, const SizeType size, const SizeType stride)
 Creates a new memory view from a raw pointer and length and a custom stride.
 
template<typename U >
 StridedMemoryView (const StridedMemoryView< U > &view)
 Creates a new strided memory view by casting the value of another.
 
 StridedMemoryView (const StridedMemoryView &)=default
 
 StridedMemoryView (StridedMemoryView &&)=default
 
StridedMemoryViewoperator= (const StridedMemoryView &)=default
 
StridedMemoryViewoperator= (StridedMemoryView &&)=default
 
void reset (T *ptr=nullptr, const SizeType size=0, const SizeType stride=defaultStride)
 Resets the memory view and sets it's value a new pointer.
 
void reset (const SizeType size, const SizeType stride=defaultStride)
 Resets the memory view size and stride.
 
StridedMemoryView< Tslice (SizeType offset, SizeType length) const
 Slices the memory view by taking the element that starts at offset and returns a new view with the given length.
 
 operator bool () const
 Checks if the memory view is valid.
 
Pointer get () noexcept
 Gets a pointer to the first element in the view.
 
ConstPointer get () const noexcept
 Gets a pointer to the first element in the view.
 
Reference at (SizeType i)
 Gets the element at the given index in the memory view.
 
ConstReference at (SizeType i) const
 Gets the element at the given index in the memory view.
 
ConstReference operator[] (const SizeType i) const
 
Reference operator[] (const SizeType i)
 
ConstReference front () const
 Gets the first element on the memory view.
 
Reference front ()
 Gets the first element on the memory view.
 
ConstReference back () const
 Gets the last element on the memory view.
 
Reference back ()
 Gets the last element on the memory view.
 
Pointer data () const noexcept
 Gets a pointer to the first element in the view.
 
Pointer data () noexcept
 Gets a pointer to the first element in the view.
 
Iterator begin () noexcept
 Returns an iterator to the first element in the view.
 
ConstIterator begin () const noexcept
 Returns an iterator to the first element in the view.
 
ConstIterator cbegin () const noexcept
 Returns an iterator to the first element in the view.
 
Iterator end () noexcept
 Returns an iterator to the past-the-last element in the view.
 
ConstIterator end () const noexcept
 Returns an iterator to the past-the-last element in the view.
 
ConstIterator cend () const noexcept
 Returns an iterator to the past-the-last element in the view.
 
ReverseIterator rbegin () noexcept
 Returns an iterator to the first element in the view while iterating in reverse.
 
ConstReverseIterator rbegin () const noexcept
 Returns an iterator to the first element in the view while iterating in reverse.
 
ConstReverseIterator crbegin () const noexcept
 Returns an iterator to the first element in the view while iterating in reverse.
 
ReverseIterator rend () noexcept
 Returns an iterator to the past-the-last element in the view while iterating in reverse.
 
ConstReverseIterator rend () const noexcept
 Returns an iterator to the past-the-last element in the view while iterating in reverse.
 
ConstReverseIterator crend () const noexcept
 Returns an iterator to the past-the-last element in the view while iterating in reverse.
 
bool empty () const noexcept
 Checks if the view is empty and has zero elements.
 
SizeType size () const noexcept
 Gets the size of the view as number of elements.
 
SizeType stride () const noexcept
 Returns the stride of view, in bytes.
 
SizeType byteSize () const noexcept
 Returns the size of view in bytes.
 
template<typename U >
StridedMemoryView< Uas () const noexcept
 Casts the memory view from type T to type U.
 

Static Public Attributes

static constexpr bool isConst = std::is_const_v<T>
 
static constexpr SizeType defaultStride = sizeof(T)
 

Private Attributes

Pointer mPointer = nullptr
 
SizeType mSize = 0
 
SizeType mStride = 0
 

Detailed Description

template<typename T>
class CeresEngine::StridedMemoryView< T >

A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally changing its type) with a Vector-like interface, excluding the methods which change a vector's size.

The library includes two classes, for viewing const and non-const memory: StridedMemoryView, and StridedMemoryView<const T>. To automatically generate the appropriate pointer, use make_strided_memory_view(ptr, size). where size is the number of elements your view will have

make_strided_memory_view also has an overload for Vector like types which can be useful if you want a view at a slice of the vector you can use it like this: make_strided_memory_view<OptionalNewType>(vector, offset = 0) where the offset is from the beginning of the vector.

It is the responsibility of the user to make sure the pointer given to a view remains valid throughout the view's lifetime. All methods of the library assume this.

Copying a memory view will copy the underlying pointer (and not its data), resulting in another view to the same memory.

To copy the data from one memory view to another, you can use std::copy

Example:

int* buf = new int[10]; auto view = make_strided_memory_view(buf, 10); for (auto& i : view) i = 0; // fill buf with zeroes

Member Typedef Documentation

◆ const_iterator

template<typename T >
using CeresEngine::StridedMemoryView< T >::const_iterator = IteratorImpl<true>

◆ const_pointer

template<typename T >
using CeresEngine::StridedMemoryView< T >::const_pointer = const T*

◆ const_reference

template<typename T >
using CeresEngine::StridedMemoryView< T >::const_reference = const T&

◆ const_reverse_iterator

template<typename T >
using CeresEngine::StridedMemoryView< T >::const_reverse_iterator = std::reverse_iterator<const_iterator>

◆ ConstIterator

◆ ConstPointer

◆ ConstReference

◆ ConstReverseIterator

template<typename T >
using CeresEngine::StridedMemoryView< T >::ConstReverseIterator = const_reverse_iterator

◆ difference_type

template<typename T >
using CeresEngine::StridedMemoryView< T >::difference_type = ptrdiff_t

◆ DifferenceType

◆ iterator

◆ Iterator

◆ pointer

template<typename T >
using CeresEngine::StridedMemoryView< T >::pointer = T*

◆ Pointer

◆ reference

template<typename T >
using CeresEngine::StridedMemoryView< T >::reference = T&

◆ Reference

◆ reverse_iterator

template<typename T >
using CeresEngine::StridedMemoryView< T >::reverse_iterator = std::reverse_iterator<iterator>

◆ ReverseIterator

◆ size_type

template<typename T >
using CeresEngine::StridedMemoryView< T >::size_type = size_t

◆ SizeType

◆ value_type

template<typename T >
using CeresEngine::StridedMemoryView< T >::value_type = T

◆ ValueType

Constructor & Destructor Documentation

◆ StridedMemoryView() [1/6]

template<typename T >
CeresEngine::StridedMemoryView< T >::StridedMemoryView ( )
default

Creates a new strided memory view with length 0.

◆ StridedMemoryView() [2/6]

template<typename T >
CeresEngine::StridedMemoryView< T >::StridedMemoryView ( std::nullptr_t  )
inline

Creates a new strided memory view with length 0.

◆ StridedMemoryView() [3/6]

template<typename T >
CeresEngine::StridedMemoryView< T >::StridedMemoryView ( const Pointer  ptr,
const SizeType  size,
const SizeType  stride 
)
inline

Creates a new memory view from a raw pointer and length and a custom stride.

◆ StridedMemoryView() [4/6]

template<typename T >
template<typename U >
CeresEngine::StridedMemoryView< T >::StridedMemoryView ( const StridedMemoryView< U > &  view)
inlineexplicit

Creates a new strided memory view by casting the value of another.

Template Parameters
UThe type of memory view to convert from.
Parameters
viewThe memory view to convert from.

◆ StridedMemoryView() [5/6]

template<typename T >
CeresEngine::StridedMemoryView< T >::StridedMemoryView ( const StridedMemoryView< T > &  )
default

◆ StridedMemoryView() [6/6]

template<typename T >
CeresEngine::StridedMemoryView< T >::StridedMemoryView ( StridedMemoryView< T > &&  )
default

Member Function Documentation

◆ as()

template<typename T >
template<typename U >
StridedMemoryView< U > CeresEngine::StridedMemoryView< T >::as ( ) const
inlinenoexcept

Casts the memory view from type T to type U.

Note
The returned view will have the same stride as the source one.
Template Parameters
UThe type to view the data in the memory view as.
Returns
A new memory view that views the same memory as a different type.

◆ at() [1/2]

template<typename T >
Reference CeresEngine::StridedMemoryView< T >::at ( SizeType  i)
inline

Gets the element at the given index in the memory view.

Note
Unlike operator[], this methods performs bounds checking.
Parameters
indexThe index of element to be returned.
Returns
A reference to the element at index.

◆ at() [2/2]

template<typename T >
ConstReference CeresEngine::StridedMemoryView< T >::at ( SizeType  i) const
inline

Gets the element at the given index in the memory view.

Note
Unlike operator[], this methods performs bounds checking.
Parameters
indexThe index of element to be returned.
Returns
A reference to the element at index.

◆ back() [1/2]

template<typename T >
Reference CeresEngine::StridedMemoryView< T >::back ( )
inline

Gets the last element on the memory view.

Returns
A reference to the last element on the view.

◆ back() [2/2]

template<typename T >
ConstReference CeresEngine::StridedMemoryView< T >::back ( ) const
inline

Gets the last element on the memory view.

Returns
A reference to the last element on the view.

◆ begin() [1/2]

template<typename T >
ConstIterator CeresEngine::StridedMemoryView< T >::begin ( ) const
inlinenoexcept

Returns an iterator to the first element in the view.

◆ begin() [2/2]

template<typename T >
Iterator CeresEngine::StridedMemoryView< T >::begin ( )
inlinenoexcept

Returns an iterator to the first element in the view.

◆ byteSize()

template<typename T >
SizeType CeresEngine::StridedMemoryView< T >::byteSize ( ) const
inlinenoexcept

Returns the size of view in bytes.

◆ cbegin()

template<typename T >
ConstIterator CeresEngine::StridedMemoryView< T >::cbegin ( ) const
inlinenoexcept

Returns an iterator to the first element in the view.

◆ cend()

template<typename T >
ConstIterator CeresEngine::StridedMemoryView< T >::cend ( ) const
inlinenoexcept

Returns an iterator to the past-the-last element in the view.

◆ crbegin()

template<typename T >
ConstReverseIterator CeresEngine::StridedMemoryView< T >::crbegin ( ) const
inlinenoexcept

Returns an iterator to the first element in the view while iterating in reverse.

◆ crend()

template<typename T >
ConstReverseIterator CeresEngine::StridedMemoryView< T >::crend ( ) const
inlinenoexcept

Returns an iterator to the past-the-last element in the view while iterating in reverse.

◆ data() [1/2]

template<typename T >
Pointer CeresEngine::StridedMemoryView< T >::data ( ) const
inlinenoexcept

Gets a pointer to the first element in the view.

◆ data() [2/2]

template<typename T >
Pointer CeresEngine::StridedMemoryView< T >::data ( )
inlinenoexcept

Gets a pointer to the first element in the view.

◆ empty()

template<typename T >
bool CeresEngine::StridedMemoryView< T >::empty ( ) const
inlinenoexcept

Checks if the view is empty and has zero elements.

◆ end() [1/2]

template<typename T >
ConstIterator CeresEngine::StridedMemoryView< T >::end ( ) const
inlinenoexcept

Returns an iterator to the past-the-last element in the view.

◆ end() [2/2]

template<typename T >
Iterator CeresEngine::StridedMemoryView< T >::end ( )
inlinenoexcept

Returns an iterator to the past-the-last element in the view.

◆ front() [1/2]

template<typename T >
Reference CeresEngine::StridedMemoryView< T >::front ( )
inline

Gets the first element on the memory view.

Returns
A reference to the first element on the view.

◆ front() [2/2]

template<typename T >
ConstReference CeresEngine::StridedMemoryView< T >::front ( ) const
inline

Gets the first element on the memory view.

Returns
A reference to the first element on the view.

◆ get() [1/2]

template<typename T >
ConstPointer CeresEngine::StridedMemoryView< T >::get ( ) const
inlinenoexcept

Gets a pointer to the first element in the view.

◆ get() [2/2]

template<typename T >
Pointer CeresEngine::StridedMemoryView< T >::get ( )
inlinenoexcept

Gets a pointer to the first element in the view.

◆ operator bool()

template<typename T >
CeresEngine::StridedMemoryView< T >::operator bool ( ) const
inlineexplicit

Checks if the memory view is valid.

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ operator[]() [1/2]

Note
Unlike at, this methods does not perform any bounds checking.
Parameters
indexThe index of element to be returned.
Returns
A reference to the element at index.

◆ operator[]() [2/2]

Note
Unlike at, this methods does not perform any bounds checking.
Parameters
indexThe index of element to be returned.
Returns
A reference to the element at index.

◆ rbegin() [1/2]

template<typename T >
ConstReverseIterator CeresEngine::StridedMemoryView< T >::rbegin ( ) const
inlinenoexcept

Returns an iterator to the first element in the view while iterating in reverse.

◆ rbegin() [2/2]

template<typename T >
ReverseIterator CeresEngine::StridedMemoryView< T >::rbegin ( )
inlinenoexcept

Returns an iterator to the first element in the view while iterating in reverse.

◆ rend() [1/2]

template<typename T >
ConstReverseIterator CeresEngine::StridedMemoryView< T >::rend ( ) const
inlinenoexcept

Returns an iterator to the past-the-last element in the view while iterating in reverse.

◆ rend() [2/2]

template<typename T >
ReverseIterator CeresEngine::StridedMemoryView< T >::rend ( )
inlinenoexcept

Returns an iterator to the past-the-last element in the view while iterating in reverse.

◆ reset() [1/2]

template<typename T >
void CeresEngine::StridedMemoryView< T >::reset ( const SizeType  size,
const SizeType  stride = defaultStride 
)
inline

Resets the memory view size and stride.

Parameters
sizeThe new size to set the memory view to.
strideThe new stride of the view. By default sizeof(T).

◆ reset() [2/2]

template<typename T >
void CeresEngine::StridedMemoryView< T >::reset ( T ptr = nullptr,
const SizeType  size = 0,
const SizeType  stride = defaultStride 
)
inline

Resets the memory view and sets it's value a new pointer.

Parameters
ptrThe pointer to set the memory view to. Default nullptr.
sizeThe length of the new view. Default 0.
strideThe new stride of the view. By default sizeof(T).

◆ size()

template<typename T >
SizeType CeresEngine::StridedMemoryView< T >::size ( ) const
inlinenoexcept

Gets the size of the view as number of elements.

◆ slice()

template<typename T >
StridedMemoryView< T > CeresEngine::StridedMemoryView< T >::slice ( SizeType  offset,
SizeType  length 
) const
inline

Slices the memory view by taking the element that starts at offset and returns a new view with the given length.

Note
If the source view doesn't have enough elements as requested, the returned view may be clamped or even empty.
The stride is taken into account when slicing and the returned view has the same stride as the source one.
Parameters
offsetThe offset of the view element of the new view.
lengthThe number of elements in the new view.
Returns
A new memory view that is a slice of this view.

◆ stride()

template<typename T >
SizeType CeresEngine::StridedMemoryView< T >::stride ( ) const
inlinenoexcept

Returns the stride of view, in bytes.

Member Data Documentation

◆ defaultStride

template<typename T >
constexpr SizeType CeresEngine::StridedMemoryView< T >::defaultStride = sizeof(T)
staticconstexpr

◆ isConst

template<typename T >
constexpr bool CeresEngine::StridedMemoryView< T >::isConst = std::is_const_v<T>
staticconstexpr

◆ mPointer

template<typename T >
Pointer CeresEngine::StridedMemoryView< T >::mPointer = nullptr
private

◆ mSize

template<typename T >
SizeType CeresEngine::StridedMemoryView< T >::mSize = 0
private

◆ mStride

template<typename T >
SizeType CeresEngine::StridedMemoryView< T >::mStride = 0
private

The documentation for this class was generated from the following file: