OpenTTD Source 20241224-master-gee860a5c8e
EndianBufferReader Class Reference

Endian-aware buffer adapter that always reads values in little endian order. More...

#include <endian_buffer.hpp>

Public Member Functions

 EndianBufferReader (std::span< const uint8_t > buffer)
 
void rewind ()
 
EndianBufferReaderoperator>> (std::string &data)
 
EndianBufferReaderoperator>> (bool &data)
 
template<typename T >
EndianBufferReaderoperator>> (OverflowSafeInt< T > &data)
 
template<typename... Targs>
EndianBufferReaderoperator>> (std::tuple< Targs... > &data)
 
template<class T , std::enable_if_t< std::disjunction_v< std::negation< std::is_class< T > >, std::is_base_of< StrongTypedefBase, T > >, int > = 0>
EndianBufferReaderoperator>> (T &data)
 

Static Public Member Functions

template<typename Tvalue >
static Tvalue ToValue (std::span< const uint8_t > buffer)
 

Private Member Functions

template<class Ttuple , size_t... Tindices>
void ReadTuple (Ttuple &values, std::index_sequence< Tindices... >)
 Helper function to read a tuple from the buffer.
 
std::string ReadStr ()
 Read overload for string data.
 
template<class T >
Read ()
 Fundamental read function.
 

Private Attributes

std::span< const uint8_t > buffer
 Reference to storage buffer.
 
size_t read_pos = 0
 Current read position.
 

Detailed Description

Endian-aware buffer adapter that always reads values in little endian order.

Note
This class uses operator overloading (>>, just like streams) for reading as this allows providing custom operator overloads for more complex types like e.g. structs without needing to modify this class.

Definition at line 119 of file endian_buffer.hpp.

Constructor & Destructor Documentation

◆ EndianBufferReader()

EndianBufferReader::EndianBufferReader ( std::span< const uint8_t >  buffer)
inline

Definition at line 126 of file endian_buffer.hpp.

Member Function Documentation

◆ operator>>() [1/5]

EndianBufferReader & EndianBufferReader::operator>> ( bool &  data)
inline

Definition at line 131 of file endian_buffer.hpp.

◆ operator>>() [2/5]

template<typename T >
EndianBufferReader & EndianBufferReader::operator>> ( OverflowSafeInt< T > &  data)
inline

Definition at line 134 of file endian_buffer.hpp.

◆ operator>>() [3/5]

EndianBufferReader & EndianBufferReader::operator>> ( std::string &  data)
inline

Definition at line 130 of file endian_buffer.hpp.

◆ operator>>() [4/5]

template<typename... Targs>
EndianBufferReader & EndianBufferReader::operator>> ( std::tuple< Targs... > &  data)
inline

Definition at line 137 of file endian_buffer.hpp.

◆ operator>>() [5/5]

template<class T , std::enable_if_t< std::disjunction_v< std::negation< std::is_class< T > >, std::is_base_of< StrongTypedefBase, T > >, int > = 0>
EndianBufferReader & EndianBufferReader::operator>> ( T &  data)
inline

Definition at line 144 of file endian_buffer.hpp.

◆ Read()

template<class T >
T EndianBufferReader::Read ( )
inlineprivate

Fundamental read function.

Definition at line 187 of file endian_buffer.hpp.

References read_pos.

◆ ReadStr()

std::string EndianBufferReader::ReadStr ( )
inlineprivate

Read overload for string data.

Definition at line 174 of file endian_buffer.hpp.

References buffer.

◆ ReadTuple()

template<class Ttuple , size_t... Tindices>
void EndianBufferReader::ReadTuple ( Ttuple &  values,
std::index_sequence< Tindices... >   
)
inlineprivate

Helper function to read a tuple from the buffer.

Definition at line 168 of file endian_buffer.hpp.

◆ rewind()

void EndianBufferReader::rewind ( )
inline

Definition at line 128 of file endian_buffer.hpp.

◆ ToValue()

template<typename Tvalue >
static Tvalue EndianBufferReader::ToValue ( std::span< const uint8_t >  buffer)
inlinestatic

Definition at line 157 of file endian_buffer.hpp.

Field Documentation

◆ buffer

std::span<const uint8_t> EndianBufferReader::buffer
private

Reference to storage buffer.

Definition at line 121 of file endian_buffer.hpp.

Referenced by ReadStr().

◆ read_pos

size_t EndianBufferReader::read_pos = 0
private

Current read position.

Definition at line 123 of file endian_buffer.hpp.

Referenced by Read().


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