CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::MemoryView< 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>

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 = T *
 
using const_iterator = const T *
 
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

 MemoryView ()=default
 Creates a new memory view with length 0.
 
 MemoryView (std::nullptr_t)
 Creates a new memory view with length 0.
 
 MemoryView (T *ptr, const SizeType size)
 Creates a new memory view from a raw pointer and length.
 
template<typename U >
 MemoryView (const MemoryView< U > &view)
 Creates a new memory view by casting the value of another.
 
template<typename Container >
 MemoryView (Container &container, SizeType offset=0)
 Creates a new memory view from an STL-compatible contiguous container.
 
 MemoryView (const MemoryView &)=default
 
 MemoryView (MemoryView &&)=default
 
MemoryViewoperator= (const MemoryView &)=default
 
MemoryViewoperator= (MemoryView &&)=default
 
void reset (Pointer ptr=nullptr, const SizeType size=0)
 Resets the memory view and sets it's value a new pointer.
 
void reset (const SizeType size)
 Resets the memory view size.
 
MemoryView< Tslice (SizeType offset, SizeType length=~0ul) const noexcept
 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 index)
 Gets the element at the given index in the memory view.
 
ConstReference at (SizeType index) const
 Gets the element at the given index in the memory view.
 
ConstReference operator[] (SizeType index) const
 
Reference operator[] (SizeType index)
 
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.
 
ConstPointer data () const noexcept
 Gets a pointer to the first element in the view.
 
Pointer data () const 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.
 
template<typename U >
MemoryView< Uas () const noexcept
 Casts the memory view from type T to type U.
 
String hexdump () const
 
void hexdump (std::ostream &stream) const
 
String toHex () const
 
void toHex (std::ostream &stream) const
 

Static Public Attributes

static constexpr bool isMutable = !std::is_const_v<T>
 

Private Attributes

TmPointer = nullptr
 A raw pointer that points to the beginning of the memory view.
 
SizeType mSize = 0
 The length of the view, in multiples of sizeof(T).
 

Detailed Description

template<typename T>
class CeresEngine::MemoryView< 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: MemoryView, and MemoryView<const T>. To automatically generate the appropriate pointer, use make_memory_view(ptr, size). where size is the number of elements your view will have

make_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_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* const buffer = new int[10];
auto view = make_memory_view(buffer, 10);
for (int& i : view) {
i = 0; // fill buf with zeroes
}
MemoryView< T > make_memory_view(T *ptr, size_t size)
Makes a new memory from a raw pointer and length.
Definition MemoryView.hpp:359
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25

Member Typedef Documentation

◆ const_iterator

template<typename T >
using CeresEngine::MemoryView< T >::const_iterator = const T*

◆ const_pointer

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

◆ const_reference

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

◆ const_reverse_iterator

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

◆ ConstIterator

template<typename T >
using CeresEngine::MemoryView< T >::ConstIterator = const_iterator

◆ ConstPointer

template<typename T >
using CeresEngine::MemoryView< T >::ConstPointer = const_pointer

◆ ConstReference

template<typename T >
using CeresEngine::MemoryView< T >::ConstReference = const_reference

◆ ConstReverseIterator

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

◆ difference_type

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

◆ DifferenceType

template<typename T >
using CeresEngine::MemoryView< T >::DifferenceType = difference_type

◆ iterator

template<typename T >
using CeresEngine::MemoryView< T >::iterator = T*

◆ Iterator

template<typename T >
using CeresEngine::MemoryView< T >::Iterator = iterator

◆ pointer

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

◆ Pointer

template<typename T >
using CeresEngine::MemoryView< T >::Pointer = pointer

◆ reference

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

◆ Reference

template<typename T >
using CeresEngine::MemoryView< T >::Reference = reference

◆ reverse_iterator

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

◆ ReverseIterator

template<typename T >
using CeresEngine::MemoryView< T >::ReverseIterator = reverse_iterator

◆ size_type

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

◆ SizeType

template<typename T >
using CeresEngine::MemoryView< T >::SizeType = size_type

◆ value_type

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

◆ ValueType

template<typename T >
using CeresEngine::MemoryView< T >::ValueType = value_type

Constructor & Destructor Documentation

◆ MemoryView() [1/7]

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

Creates a new memory view with length 0.

◆ MemoryView() [2/7]

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

Creates a new memory view with length 0.

◆ MemoryView() [3/7]

template<typename T >
CeresEngine::MemoryView< T >::MemoryView ( T ptr,
const SizeType  size 
)
inline

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

◆ MemoryView() [4/7]

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

Creates a new 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.

◆ MemoryView() [5/7]

template<typename T >
template<typename Container >
CeresEngine::MemoryView< T >::MemoryView ( Container &  container,
SizeType  offset = 0 
)
inline

Creates a new memory view from an STL-compatible contiguous container.

Parameters
containerThe container to create a view from.
offsetThe number of elements to skip from the start of the container.

◆ MemoryView() [6/7]

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

◆ MemoryView() [7/7]

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

Member Function Documentation

◆ as()

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

Casts the memory view from type T to type U.

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::MemoryView< T >::at ( SizeType  index)
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::MemoryView< T >::at ( SizeType  index) 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::MemoryView< 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::MemoryView< 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::MemoryView< T >::begin ( ) const
inlinenoexcept

Returns an iterator to the first element in the view.

◆ begin() [2/2]

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

Returns an iterator to the first element in the view.

◆ cbegin()

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

Returns an iterator to the first element in the view.

◆ cend()

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

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

◆ crbegin()

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

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

◆ crend()

template<typename T >
ConstReverseIterator CeresEngine::MemoryView< 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 >
ConstPointer CeresEngine::MemoryView< T >::data ( ) const
inlinenoexcept

Gets a pointer to the first element in the view.

◆ data() [2/2]

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

Gets a pointer to the first element in the view.

◆ empty()

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

Checks if the view is empty and has zero elements.

◆ end() [1/2]

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

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

◆ end() [2/2]

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

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

◆ front() [1/2]

template<typename T >
Reference CeresEngine::MemoryView< 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::MemoryView< 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::MemoryView< T >::get ( ) const
inlinenoexcept

Gets a pointer to the first element in the view.

◆ get() [2/2]

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

Gets a pointer to the first element in the view.

◆ hexdump() [1/2]

template<typename T >
String CeresEngine::MemoryView< T >::hexdump ( ) const

◆ hexdump() [2/2]

template<typename T >
void CeresEngine::MemoryView< T >::hexdump ( std::ostream &  stream) const

◆ operator bool()

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

Checks if the memory view is valid.

◆ operator=() [1/2]

◆ operator=() [2/2]

template<typename T >
MemoryView & CeresEngine::MemoryView< T >::operator= ( MemoryView< T > &&  )
default

◆ operator[]() [1/2]

template<typename T >
Reference CeresEngine::MemoryView< T >::operator[] ( SizeType  index)
inline

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]

template<typename T >
ConstReference CeresEngine::MemoryView< T >::operator[] ( SizeType  index) const
inline
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::MemoryView< 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::MemoryView< 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::MemoryView< 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::MemoryView< 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::MemoryView< T >::reset ( const SizeType  size)
inline

Resets the memory view size.

Parameters
sizeThe new size to set the memory view to.

◆ reset() [2/2]

template<typename T >
void CeresEngine::MemoryView< T >::reset ( Pointer  ptr = nullptr,
const SizeType  size = 0 
)
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.

◆ size()

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

Gets the size of the view as number of elements.

◆ slice()

template<typename T >
MemoryView< T > CeresEngine::MemoryView< T >::slice ( SizeType  offset,
SizeType  length = ~0ul 
) const
inlinenoexcept

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.
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.

◆ toHex() [1/2]

template<typename T >
String CeresEngine::MemoryView< T >::toHex ( ) const

◆ toHex() [2/2]

template<typename T >
void CeresEngine::MemoryView< T >::toHex ( std::ostream &  stream) const

Member Data Documentation

◆ isMutable

template<typename T >
constexpr bool CeresEngine::MemoryView< T >::isMutable = !std::is_const_v<T>
staticconstexpr

◆ mPointer

template<typename T >
T* CeresEngine::MemoryView< T >::mPointer = nullptr
private

A raw pointer that points to the beginning of the memory view.

◆ mSize

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

The length of the view, in multiples of sizeof(T).


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