20#if defined(CE_MEMORY_VIEW_NO_DEBUG_BOUNDS_CHECK)
21#define CE_MEMORY_VIEW_BOUNDS_CHECK(i)
24#define CE_MEMORY_VIEW_BOUNDS_CHECK(i) CE_ASSERT((i) < this->size())
88 static constexpr bool isMutable = !std::is_const_v<T>;
146 offset = std::clamp(offset,
size_t(0),
mSize);
147 length = std::clamp(length,
size_t(0),
mSize - offset);
309 void toHex(std::ostream& stream)
const;
321 if constexpr(std::is_same_v<T, Byte>) {
329 if constexpr(std::is_same_v<T, Byte>) {
337 if constexpr(std::is_same_v<T, Byte>) {
345 if constexpr(std::is_same_v<T, Byte>) {
406 return reinterpret_cast<T*
>(
reinterpret_cast<ByteType*
>(pointer) +
bytes);
467 static constexpr bool isConst = std::is_const_v<T>;
530 }
else if((offset + length) >
mSize) {
531 length =
mSize - offset;
671 using Self = std::conditional_t<IsConstIterator, const StridedMemoryView, StridedMemoryView>;
706 return &(*mMemoryView)[
mIndex];
710 return &(*mMemoryView)[
mIndex];
794 template<
typename Container>
#define CE_ASSERT(...)
Definition Macros.hpp:323
#define CE_MEMORY_VIEW_BOUNDS_CHECK(i)
Definition MemoryView.hpp:24
A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally c...
Definition MemoryView.hpp:62
static constexpr bool isMutable
Definition MemoryView.hpp:88
String hexdump() const
Definition MemoryView.hpp:320
ConstReference back() const
Gets the last element on the memory view.
Definition MemoryView.hpp:218
ConstIterator begin() const noexcept
Returns an iterator to the first element in the view.
Definition MemoryView.hpp:246
const_reference ConstReference
Definition MemoryView.hpp:80
MemoryView(std::nullptr_t)
Creates a new memory view with length 0.
Definition MemoryView.hpp:102
T value_type
Definition MemoryView.hpp:64
ConstIterator cend() const noexcept
Returns an iterator to the past-the-last element in the view.
Definition MemoryView.hpp:261
const T & const_reference
Definition MemoryView.hpp:68
MemoryView & operator=(const MemoryView &)=default
difference_type DifferenceType
Definition MemoryView.hpp:78
SizeType mSize
The length of the view, in multiples of sizeof(T).
Definition MemoryView.hpp:95
ConstPointer get() const noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:170
MemoryView(const MemoryView &)=default
T * mPointer
A raw pointer that points to the beginning of the memory view.
Definition MemoryView.hpp:92
ConstReverseIterator crend() const noexcept
Returns an iterator to the past-the-last element in the view while iterating in reverse.
Definition MemoryView.hpp:287
bool empty() const noexcept
Checks if the view is empty and has zero elements.
Definition MemoryView.hpp:291
void hexdump(std::ostream &stream) const
Definition MemoryView.hpp:328
ConstReverseIterator rbegin() const noexcept
Returns an iterator to the first element in the view while iterating in reverse.
Definition MemoryView.hpp:271
MemoryView()=default
Creates a new memory view with length 0.
iterator Iterator
Definition MemoryView.hpp:83
SizeType size() const noexcept
Gets the size of the view as number of elements.
Definition MemoryView.hpp:294
MemoryView(T *ptr, const SizeType size)
Creates a new memory view from a raw pointer and length.
Definition MemoryView.hpp:105
Reference back()
Gets the last element on the memory view.
Definition MemoryView.hpp:221
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition MemoryView.hpp:74
std::reverse_iterator< iterator > reverse_iterator
Definition MemoryView.hpp:73
value_type ValueType
Definition MemoryView.hpp:76
ConstIterator cbegin() const noexcept
Returns an iterator to the first element in the view.
Definition MemoryView.hpp:249
String toHex() const
Definition MemoryView.hpp:336
Reference at(SizeType index)
Gets the element at the given index in the memory view.
Definition MemoryView.hpp:179
MemoryView< U > as() const noexcept
Casts the memory view from type T to type U.
Definition MemoryView.hpp:300
Iterator begin() noexcept
Returns an iterator to the first element in the view.
Definition MemoryView.hpp:240
ReverseIterator rend() noexcept
Returns an iterator to the past-the-last element in the view while iterating in reverse.
Definition MemoryView.hpp:278
Iterator end() noexcept
Returns an iterator to the past-the-last element in the view.
Definition MemoryView.hpp:252
const_iterator ConstIterator
Definition MemoryView.hpp:84
MemoryView(const MemoryView< U > &view)
Creates a new memory view by casting the value of another.
Definition MemoryView.hpp:110
ConstReverseIterator crbegin() const noexcept
Returns an iterator to the first element in the view while iterating in reverse.
Definition MemoryView.hpp:274
const_pointer ConstPointer
Definition MemoryView.hpp:82
size_t size_type
Definition MemoryView.hpp:65
T * pointer
Definition MemoryView.hpp:69
void toHex(std::ostream &stream) const
Definition MemoryView.hpp:344
const T * const_iterator
Definition MemoryView.hpp:72
MemoryView(Container &container, SizeType offset=0)
Creates a new memory view from an STL-compatible contiguous container.
Definition MemoryView.hpp:116
MemoryView & operator=(MemoryView &&)=default
pointer Pointer
Definition MemoryView.hpp:81
void reset(const SizeType size)
Resets the memory view size.
Definition MemoryView.hpp:134
MemoryView< T > slice(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 gi...
Definition MemoryView.hpp:145
reverse_iterator ReverseIterator
Definition MemoryView.hpp:85
void reset(Pointer ptr=nullptr, const SizeType size=0)
Resets the memory view and sets it's value a new pointer.
Definition MemoryView.hpp:127
const T * const_pointer
Definition MemoryView.hpp:70
ConstPointer data() const noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:227
T & reference
Definition MemoryView.hpp:67
ConstIterator end() const noexcept
Returns an iterator to the past-the-last element in the view.
Definition MemoryView.hpp:258
MemoryView(MemoryView &&)=default
ConstReference at(SizeType index) const
Gets the element at the given index in the memory view.
Definition MemoryView.hpp:186
ConstReference front() const
Gets the first element on the memory view.
Definition MemoryView.hpp:208
ConstReverseIterator rend() const noexcept
Returns an iterator to the past-the-last element in the view while iterating in reverse.
Definition MemoryView.hpp:284
ReverseIterator rbegin() noexcept
Returns an iterator to the first element in the view while iterating in reverse.
Definition MemoryView.hpp:265
Pointer get() noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:164
size_type SizeType
Definition MemoryView.hpp:77
Pointer data() const noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:233
ptrdiff_t difference_type
Definition MemoryView.hpp:66
reference Reference
Definition MemoryView.hpp:79
Reference operator[](SizeType index)
Definition MemoryView.hpp:201
Reference front()
Gets the first element on the memory view.
Definition MemoryView.hpp:211
const_reverse_iterator ConstReverseIterator
Definition MemoryView.hpp:86
ConstReference operator[](SizeType index) const
Definition MemoryView.hpp:198
T * iterator
Definition MemoryView.hpp:71
Definition MemoryView.hpp:669
IteratorImpl(Self *memoryView, const SizeType index=0)
Definition MemoryView.hpp:693
IteratorImpl operator+(const DifferenceType n) noexcept
Definition MemoryView.hpp:724
typename Self::value_type value_type
Definition MemoryView.hpp:675
const_reference operator*() const noexcept
Definition MemoryView.hpp:699
IteratorImpl & operator++() noexcept
Definition MemoryView.hpp:719
IteratorImpl operator++(int) noexcept
Definition MemoryView.hpp:713
IteratorImpl operator-(const DifferenceType n) noexcept
Definition MemoryView.hpp:748
const_reference ConstReference
Definition MemoryView.hpp:686
pointer Pointer
Definition MemoryView.hpp:683
std::random_access_iterator_tag iterator_category
Definition MemoryView.hpp:673
IteratorImpl(Self &memoryView, const SizeType index=0)
Definition MemoryView.hpp:692
value_type ValueType
Definition MemoryView.hpp:682
const_pointer ConstPointer
Definition MemoryView.hpp:684
reference operator*() const noexcept
Definition MemoryView.hpp:695
IteratorImpl & operator--() noexcept
Definition MemoryView.hpp:740
difference_type DifferenceType
Definition MemoryView.hpp:681
typename Self::const_reference const_reference
Definition MemoryView.hpp:679
IteratorImpl operator--(int) noexcept
Definition MemoryView.hpp:730
typename Self::reference reference
Definition MemoryView.hpp:678
typename Self::const_pointer const_pointer
Definition MemoryView.hpp:677
SizeType mIndex
Definition MemoryView.hpp:689
friend bool operator==(const IteratorImpl &lhs, const IteratorImpl &rhs)
Definition MemoryView.hpp:754
std::ptrdiff_t difference_type
Definition MemoryView.hpp:674
std::conditional_t< IsConstIterator, const StridedMemoryView, StridedMemoryView > Self
Definition MemoryView.hpp:671
pointer operator->() const noexcept
Definition MemoryView.hpp:704
typename Self::pointer pointer
Definition MemoryView.hpp:676
friend bool operator!=(const IteratorImpl &lhs, const IteratorImpl &rhs)
Definition MemoryView.hpp:756
reference Reference
Definition MemoryView.hpp:685
const_pointer operator->() const noexcept
Definition MemoryView.hpp:708
Self * mMemoryView
Definition MemoryView.hpp:688
A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally c...
Definition MemoryView.hpp:439
ConstReverseIterator rend() const noexcept
Returns an iterator to the past-the-last element in the view while iterating in reverse.
Definition MemoryView.hpp:640
size_t size_type
Definition MemoryView.hpp:444
const T & const_reference
Definition MemoryView.hpp:447
ConstIterator cend() const noexcept
Returns an iterator to the past-the-last element in the view.
Definition MemoryView.hpp:619
Pointer data() noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:594
IteratorImpl< false > iterator
Definition MemoryView.hpp:450
StridedMemoryView(std::nullptr_t)
Creates a new strided memory view with length 0.
Definition MemoryView.hpp:480
bool empty() const noexcept
Checks if the view is empty and has zero elements.
Definition MemoryView.hpp:647
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.
Definition MemoryView.hpp:501
Pointer get() noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:542
SizeType byteSize() const noexcept
Returns the size of view in bytes.
Definition MemoryView.hpp:656
StridedMemoryView(StridedMemoryView &&)=default
ConstIterator begin() const noexcept
Returns an iterator to the first element in the view.
Definition MemoryView.hpp:604
std::reverse_iterator< iterator > reverse_iterator
Definition MemoryView.hpp:452
T value_type
Definition MemoryView.hpp:443
ConstIterator end() const noexcept
Returns an iterator to the past-the-last element in the view.
Definition MemoryView.hpp:616
StridedMemoryView & operator=(StridedMemoryView &&)=default
Reference front()
Gets the first element on the memory view.
Definition MemoryView.hpp:576
Reference at(SizeType i)
Gets the element at the given index in the memory view.
Definition MemoryView.hpp:551
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.
Definition MemoryView.hpp:484
StridedMemoryView()=default
Creates a new strided memory view with length 0.
StridedMemoryView(const StridedMemoryView< U > &view)
Creates a new strided memory view by casting the value of another.
Definition MemoryView.hpp:489
ReverseIterator rbegin() noexcept
Returns an iterator to the first element in the view while iterating in reverse.
Definition MemoryView.hpp:622
SizeType size() const noexcept
Gets the size of the view as number of elements.
Definition MemoryView.hpp:650
const_reference ConstReference
Definition MemoryView.hpp:459
Pointer mPointer
Definition MemoryView.hpp:471
void reset(const SizeType size, const SizeType stride=defaultStride)
Resets the memory view size and stride.
Definition MemoryView.hpp:510
size_type SizeType
Definition MemoryView.hpp:456
ConstReverseIterator rbegin() const noexcept
Returns an iterator to the first element in the view while iterating in reverse.
Definition MemoryView.hpp:628
reference Reference
Definition MemoryView.hpp:458
StridedMemoryView(const StridedMemoryView &)=default
StridedMemoryView< T > slice(SizeType offset, SizeType length) const
Slices the memory view by taking the element that starts at offset and returns a new view with the gi...
Definition MemoryView.hpp:526
Reference back()
Gets the last element on the memory view.
Definition MemoryView.hpp:585
const_pointer ConstPointer
Definition MemoryView.hpp:461
ConstIterator cbegin() const noexcept
Returns an iterator to the first element in the view.
Definition MemoryView.hpp:607
Reference operator[](const SizeType i)
Definition MemoryView.hpp:567
value_type ValueType
Definition MemoryView.hpp:455
SizeType mStride
Definition MemoryView.hpp:473
static constexpr bool isConst
Definition MemoryView.hpp:467
StridedMemoryView< U > as() const noexcept
Casts the memory view from type T to type U.
Definition MemoryView.hpp:665
SizeType stride() const noexcept
Returns the stride of view, in bytes.
Definition MemoryView.hpp:653
ConstReference operator[](const SizeType i) const
Definition MemoryView.hpp:564
Pointer data() const noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:591
T * pointer
Definition MemoryView.hpp:448
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition MemoryView.hpp:453
difference_type DifferenceType
Definition MemoryView.hpp:457
Iterator begin() noexcept
Returns an iterator to the first element in the view.
Definition MemoryView.hpp:598
ConstPointer get() const noexcept
Gets a pointer to the first element in the view.
Definition MemoryView.hpp:548
static constexpr SizeType defaultStride
Definition MemoryView.hpp:468
const_reverse_iterator ConstReverseIterator
Definition MemoryView.hpp:465
ReverseIterator rend() noexcept
Returns an iterator to the past-the-last element in the view while iterating in reverse.
Definition MemoryView.hpp:634
IteratorImpl< true > const_iterator
Definition MemoryView.hpp:451
ConstReference front() const
Gets the first element on the memory view.
Definition MemoryView.hpp:573
Iterator end() noexcept
Returns an iterator to the past-the-last element in the view.
Definition MemoryView.hpp:610
ConstReverseIterator crend() const noexcept
Returns an iterator to the past-the-last element in the view while iterating in reverse.
Definition MemoryView.hpp:643
pointer Pointer
Definition MemoryView.hpp:460
SizeType mSize
Definition MemoryView.hpp:472
T & reference
Definition MemoryView.hpp:446
StridedMemoryView & operator=(const StridedMemoryView &)=default
const T * const_pointer
Definition MemoryView.hpp:449
ConstReference back() const
Gets the last element on the memory view.
Definition MemoryView.hpp:582
ConstReverseIterator crbegin() const noexcept
Returns an iterator to the first element in the view while iterating in reverse.
Definition MemoryView.hpp:631
reverse_iterator ReverseIterator
Definition MemoryView.hpp:464
ptrdiff_t difference_type
Definition MemoryView.hpp:445
ConstReference at(SizeType i) const
Gets the element at the given index in the memory view.
Definition MemoryView.hpp:558
T * advanceBytes(T *pointer, std::ptrdiff_t bytes)
Utility function that advances a pointer by a fixed number of bytes, independent of the size of T.
Definition MemoryView.hpp:404
String memoryViewToHex(const MemoryView< Byte > &memoryView)
String memoryViewHexdump(const MemoryView< Byte > &memoryView)
Definition Application.hpp:19
Byte
Definition DataTypes.hpp:40
MemoryView< T > make_memory_view(T *ptr, size_t size)
Makes a new memory from a raw pointer and length.
Definition MemoryView.hpp:359
StridedMemoryView< T > make_strided_memory_view(T *ptr, size_t size, size_t stride=StridedMemoryView< T >::defaultStride)
Makes a new strided memory from a raw pointer and length.
Definition MemoryView.hpp:768
void copy(const A &a, B &b, T &&t=T())
Copies values from one container to another.
Definition Iterator.hpp:564
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25