MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::EndianSlice< T, Endian > Struct Template Reference

A Slice with Endian-aware data which is just a pointer + length + endian direction Accessing elements through the array operator will do bounds checks and will convert to native endian by default (this can be overridden with access methods) Accessing out of bounds will terminate the program instead of throw Can also get sub slices. More...

#include <byte_endian.hpp>

Inheritance diagram for mtcore::EndianSlice< T, Endian >:

Classes

struct  ConstEndianWrapper
 Endian Wrapper for grabbing the endian value By default, will handle converting to and from the native architecture Can also manually specify the endian architecture to use. More...
 
struct  EndianWrapper
 Endian Wrapper for grabbing the endian value By default, will handle converting to and from the native architecture Can also manually specify the endian architecture to use. More...
 

Public Types

using Elem = T
 

Public Member Functions

decltype(auto) ptr_iter () noexcept
 
decltype(auto) ptr_iter () const noexcept
 
decltype(auto) iter () const noexcept
 
constexpr Slice< std::add_const_t< T > > to_const () const noexcept
 Converts to a const Slice.
 
constexpr void init (T *head, size_t len)
 Initializes a Slice Using init instead of a constructor so that slices allocated with malloc or an arena can be initialized after the fact.
 
constexpr ConstEndianWrapper operator[] (size_t i) const noexcept
 Access element at a specific index Const operator, returns a const reference.
 
EndianWrapper operator[] (size_t i) noexcept
 Access element at a specific index Non-const operator, returns a mutable reference.
 
EndianWrapper at (size_t i) noexcept
 Access element at a specific index Non-const operator, returns a mutable reference.
 
constexpr ConstEndianWrapper at (size_t i) const noexcept
 Access element at a specific index Const operator, returns a const reference.
 
constexpr size_t size () const noexcept
 Gets the size of a Slice.
 
constexpr bool empty () const noexcept
 Checks if a Slice is empty.
 
constexpr EndianSlice sub (size_t start) const noexcept
 Gets a sub Slice from start to end.
 
constexpr EndianSlice sub (size_t start, size_t len) const noexcept
 Gets a sub Slice from start up to a length.
 
template<std::endian E>
std::strong_ordering operator<=> (const EndianSlice< std::remove_const_t< T >, E > &other) const noexcept
 Compares against another Slice.
 
template<std::endian E>
std::strong_ordering operator<=> (const EndianSlice< std::add_const_t< T >, E > &other) const noexcept
 Compares against another Slice.
 
template<std::endian E>
bool operator== (const EndianSlice< std::remove_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator!= (const EndianSlice< std::remove_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator< (const EndianSlice< std::remove_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator> (const EndianSlice< std::remove_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator<= (const EndianSlice< std::remove_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator>= (const EndianSlice< std::remove_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator== (const EndianSlice< std::add_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator!= (const EndianSlice< std::add_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator< (const EndianSlice< std::add_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator> (const EndianSlice< std::add_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator<= (const EndianSlice< std::add_const_t< T >, E > &o) const noexcept
 
template<std::endian E>
bool operator>= (const EndianSlice< std::add_const_t< T >, E > &o) const noexcept
 

Public Attributes

T * head = nullptr
 
size_t len = 0
 

Detailed Description

template<class T, std::endian Endian = std::endian::native>
struct mtcore::EndianSlice< T, Endian >

A Slice with Endian-aware data which is just a pointer + length + endian direction Accessing elements through the array operator will do bounds checks and will convert to native endian by default (this can be overridden with access methods) Accessing out of bounds will terminate the program instead of throw Can also get sub slices.

Note: this is limited to char16_t and char32_t (const and non-const variants)!

Template Parameters
TType of element pointed to by the Slice

Definition at line 173 of file byte_endian.hpp.

Member Typedef Documentation

◆ Elem

template<class T, std::endian Endian = std::endian::native>
using mtcore::EndianSlice< T, Endian >::Elem = T

Definition at line 176 of file byte_endian.hpp.

Member Function Documentation

◆ at() [1/2]

template<class T, std::endian Endian = std::endian::native>
ConstEndianWrapper mtcore::EndianSlice< T, Endian >::at ( size_t i) const
inlinenodiscardconstexprnoexcept

Access element at a specific index Const operator, returns a const reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 319 of file byte_endian.hpp.

319{ return (*this)[i]; }
A Slice with Endian-aware data which is just a pointer + length + endian direction Accessing elements...

◆ at() [2/2]

template<class T, std::endian Endian = std::endian::native>
EndianWrapper mtcore::EndianSlice< T, Endian >::at ( size_t i)
inlinenoexcept

Access element at a specific index Non-const operator, returns a mutable reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 312 of file byte_endian.hpp.

312{ return (*this)[i]; }

◆ empty()

template<class T, std::endian Endian = std::endian::native>
bool mtcore::EndianSlice< T, Endian >::empty ( ) const
inlinenodiscardconstexprnoexcept

Checks if a Slice is empty.

Definition at line 329 of file byte_endian.hpp.

329{ return head == nullptr || len == 0; }

◆ init()

template<class T, std::endian Endian = std::endian::native>
void mtcore::EndianSlice< T, Endian >::init ( T * head,
size_t len )
inlineconstexpr

Initializes a Slice Using init instead of a constructor so that slices allocated with malloc or an arena can be initialized after the fact.

Parameters
headPointer to the starting memory address
lenNumber of elements of type T in the Slice

Definition at line 198 of file byte_endian.hpp.

198 {
199 this->head = head;
200 this->len = len;
201 }
Here is the caller graph for this function:

◆ iter()

template<class T, std::endian Endian = std::endian::native>
decltype(auto) mtcore::EndianSlice< T, Endian >::iter ( ) const
inlinenodiscardnoexcept

Definition at line 183 of file byte_endian.hpp.

183{ return iter::val(*this); }
ValIter< T > val(const T &r)
Generic value iterator that uses the operator[] and incrementing indexes to iterate over a collection...
Definition iter.hpp:114

◆ operator!=() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator!= ( const EndianSlice< std::add_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 444 of file byte_endian.hpp.

444 {
446 }

◆ operator!=() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator!= ( const EndianSlice< std::remove_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 412 of file byte_endian.hpp.

412 {
414 }

◆ operator<() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator< ( const EndianSlice< std::add_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 449 of file byte_endian.hpp.

449 {
451 }

◆ operator<() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator< ( const EndianSlice< std::remove_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 417 of file byte_endian.hpp.

417 {
419 }

◆ operator<=() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator<= ( const EndianSlice< std::add_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 459 of file byte_endian.hpp.

459 {
460 const auto cmp = *this <=> o;
462 }

◆ operator<=() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator<= ( const EndianSlice< std::remove_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 427 of file byte_endian.hpp.

427 {
428 const auto cmp = *this <=> o;
430 }

◆ operator<=>() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
std::strong_ordering mtcore::EndianSlice< T, Endian >::operator<=> ( const EndianSlice< std::add_const_t< T >, E > & other) const
inlinenoexcept

Compares against another Slice.

Parameters
otherConstant Slice to compare to
Returns
Relative ordering

Definition at line 376 of file byte_endian.hpp.

376 {
377 if (!head) {
378 if (other.head) {
380 }
382 }
383
384 for (size_t i = 0; i < len && i < other.len; ++i) {
385 auto tv = (*this)[i].native();
386 auto ov = other[i].native();
387 if (tv > ov) {
389 }
390 else if (tv < ov) {
392 }
393 }
394
395 if (len == other.len) {
397 }
398 else if (len < other.len) {
400 }
401 else {
403 }
404 }

◆ operator<=>() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
std::strong_ordering mtcore::EndianSlice< T, Endian >::operator<=> ( const EndianSlice< std::remove_const_t< T >, E > & other) const
inlinenoexcept

Compares against another Slice.

Parameters
otherMutable Slice to compare to
Returns
Relative ordering

Definition at line 366 of file byte_endian.hpp.

366 {
367 return *this <=> other.to_const();
368 }
constexpr Slice< std::add_const_t< T > > to_const() const noexcept
Converts to a const Slice.

◆ operator==() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator== ( const EndianSlice< std::add_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 439 of file byte_endian.hpp.

439 {
441 }

◆ operator==() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator== ( const EndianSlice< std::remove_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 407 of file byte_endian.hpp.

407 {
409 }

◆ operator>() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator> ( const EndianSlice< std::add_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 454 of file byte_endian.hpp.

454 {
456 }

◆ operator>() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator> ( const EndianSlice< std::remove_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 422 of file byte_endian.hpp.

422 {
424 }

◆ operator>=() [1/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator>= ( const EndianSlice< std::add_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 465 of file byte_endian.hpp.

465 {
466 const auto cmp = *this <=> o;
468 }

◆ operator>=() [2/2]

template<class T, std::endian Endian = std::endian::native>
template<std::endian E>
bool mtcore::EndianSlice< T, Endian >::operator>= ( const EndianSlice< std::remove_const_t< T >, E > & o) const
inlinenoexcept

Definition at line 433 of file byte_endian.hpp.

433 {
434 const auto cmp = *this <=> o;
436 }

◆ operator[]() [1/2]

template<class T, std::endian Endian = std::endian::native>
ConstEndianWrapper mtcore::EndianSlice< T, Endian >::operator[] ( size_t i) const
inlinenodiscardconstexprnoexcept

Access element at a specific index Const operator, returns a const reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 290 of file byte_endian.hpp.

290 {
291 ensure(i < len, "SLICE ACCESS OUT OF BOUNDS");
292 ensure(head, "NULL POINTER DEREFERENCE");
293 return ConstEndianWrapper{*this, i};
294 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Endian Wrapper for grabbing the endian value By default, will handle converting to and from the nativ...

◆ operator[]() [2/2]

template<class T, std::endian Endian = std::endian::native>
EndianWrapper mtcore::EndianSlice< T, Endian >::operator[] ( size_t i)
inlinenoexcept

Access element at a specific index Non-const operator, returns a mutable reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 301 of file byte_endian.hpp.

301 {
302 ensure(i < len, "SLICE ACCESS OUT OF BOUNDS");
303 ensure(head, "NULL POINTER DEREFERENCE");
304 return EndianWrapper{*this, i};
305 }
Endian Wrapper for grabbing the endian value By default, will handle converting to and from the nativ...

◆ ptr_iter() [1/2]

template<class T, std::endian Endian = std::endian::native>
decltype(auto) mtcore::EndianSlice< T, Endian >::ptr_iter ( ) const
inlinenodiscardnoexcept

Definition at line 182 of file byte_endian.hpp.

182{ return iter::const_ptr(*this); }
ConstPtrIter< T > const_ptr(const T &r)
Generic constant pointer iterator that uses the operator[] and incrementing indexes to iterate over a...
Definition iter.hpp:128

◆ ptr_iter() [2/2]

template<class T, std::endian Endian = std::endian::native>
decltype(auto) mtcore::EndianSlice< T, Endian >::ptr_iter ( )
inlinenodiscardnoexcept

Definition at line 181 of file byte_endian.hpp.

181{ return iter::ptr(*this); }
PtrIter< T > ptr(T &r)
Generic pointer iterator that uses the operator[] and incrementing indexes to iterate over a collecti...
Definition iter.hpp:101

◆ size()

template<class T, std::endian Endian = std::endian::native>
size_t mtcore::EndianSlice< T, Endian >::size ( ) const
inlinenodiscardconstexprnoexcept

Gets the size of a Slice.

Definition at line 324 of file byte_endian.hpp.

324{ return head != nullptr ? len : 0; }

◆ sub() [1/2]

template<class T, std::endian Endian = std::endian::native>
EndianSlice mtcore::EndianSlice< T, Endian >::sub ( size_t start) const
inlinenodiscardconstexprnoexcept

Gets a sub Slice from start to end.

Parameters
startThe starting index to get a sub Slice from

Definition at line 335 of file byte_endian.hpp.

335 {
337 if (start >= len) {
338 res.init(nullptr, 0);
339 return res;
340 }
341
342 res.init(head + start, len - start);
343 ensure(res.len + start == len, "BAD LENGTH MATH");
344 ensure(head + len == res.head + res.len, "BAD END POINTER");
345 return res;
346 }
constexpr void init(T *head, size_t len)
Initializes a Slice Using init instead of a constructor so that slices allocated with malloc or an ar...
Here is the caller graph for this function:

◆ sub() [2/2]

template<class T, std::endian Endian = std::endian::native>
EndianSlice mtcore::EndianSlice< T, Endian >::sub ( size_t start,
size_t len ) const
inlinenodiscardconstexprnoexcept

Gets a sub Slice from start up to a length.

Parameters
startThe starting index to get a sub Slice from
lenMaximum length of the Slice

Definition at line 353 of file byte_endian.hpp.

353 {
354 auto res = sub(start);
355 res.len = res.len > len ? len : res.len;
356 ensure(res.len <= len, "BAD LENGTH");
357 return res;
358 }
constexpr EndianSlice sub(size_t start) const noexcept
Gets a sub Slice from start to end.

◆ to_const()

template<class T, std::endian Endian = std::endian::native>
Slice< std::add_const_t< T > > mtcore::EndianSlice< T, Endian >::to_const ( ) const
inlinenodiscardconstexprnoexcept

Converts to a const Slice.

Definition at line 186 of file byte_endian.hpp.

186 {
187 return {static_cast<std::add_pointer_t<std::add_const_t<T>>>(head), len};
188 }

Member Data Documentation

◆ head

template<class T, std::endian Endian = std::endian::native>
T* mtcore::EndianSlice< T, Endian >::head = nullptr

Definition at line 178 of file byte_endian.hpp.

◆ len

template<class T, std::endian Endian = std::endian::native>
size_t mtcore::EndianSlice< T, Endian >::len = 0

Definition at line 179 of file byte_endian.hpp.


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