PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
profinet::wire::Reader Class Reference

Lightweight cursor for parsing a big-endian byte buffer without copies. More...

#include <wire.h>

+ Collaboration diagram for profinet::wire::Reader:

Public Member Functions

 Reader (const std::uint8_t *data, std::size_t len)
 Construct a reader over a raw pointer/length pair.
 
 Reader (std::span< const std::uint8_t > bytes)
 Construct a reader over a byte span.
 
 Reader (const std::vector< std::uint8_t > &v)
 Construct a reader over an existing byte vector.
 
std::size_t Remaining () const
 Number of bytes not yet consumed.
 
std::size_t Position () const
 Current read offset from the start of the buffer.
 
const std::uint8_t * Cursor () const
 Pointer to the next unread byte.
 
void Need (std::size_t n) const
 Assert that at least n bytes remain.
 
std::uint8_t U8 ()
 Read and consume one byte.
 
std::uint16_t U16 ()
 Read and consume a big-endian 16-bit value.
 
std::uint32_t U32 ()
 Read and consume a big-endian 32-bit value.
 
std::vector< std::uint8_t > ReadBytes (std::size_t n)
 Read and consume a variable-length byte range.
 
template<std::size_t N>
std::array< std::uint8_t, N > Fixed ()
 Read and consume a fixed-size byte array.
 
void Skip (std::size_t n)
 Advance the read position without returning the skipped bytes.
 

Private Attributes

const std::uint8_t * data
 Pointer to the start of the underlying buffer (not owned).
 
std::size_t len
 Total length of the underlying buffer in bytes.
 
std::size_t pos = 0
 Current read offset from the start of the buffer.
 

Detailed Description

Lightweight cursor for parsing a big-endian byte buffer without copies.

Every read method advances the internal position and throws std::out_of_range if the buffer doesn't have enough remaining bytes.

Definition at line 66 of file wire.h.

Constructor & Destructor Documentation

◆ Reader() [1/3]

profinet::wire::Reader::Reader ( const std::uint8_t *  data,
std::size_t  len 
)
inline

Construct a reader over a raw pointer/length pair.

Parameters
dataPointer to the first byte of the buffer. Must outlive the Reader.
lenNumber of bytes available starting at Data.

Definition at line 72 of file wire.h.

◆ Reader() [2/3]

profinet::wire::Reader::Reader ( std::span< const std::uint8_t >  bytes)
inlineexplicit

Construct a reader over a byte span.

Parameters
bytesSpan containing the bytes to read. Must outlive the Reader.

Definition at line 79 of file wire.h.

◆ Reader() [3/3]

profinet::wire::Reader::Reader ( const std::vector< std::uint8_t > &  v)
inlineexplicit

Construct a reader over an existing byte vector.

Parameters
vVector to read from. Must outlive the Reader.

Definition at line 86 of file wire.h.

Member Function Documentation

◆ Cursor()

const std::uint8_t * profinet::wire::Reader::Cursor ( ) const
inline

Pointer to the next unread byte.

Returns
Pointer into the underlying buffer at the current position.

Definition at line 107 of file wire.h.

References data, and pos.

◆ Fixed()

template<std::size_t N>
std::array< std::uint8_t, N > profinet::wire::Reader::Fixed ( )
inline

Read and consume a fixed-size byte array.

Template Parameters
NNumber of bytes to read.
Returns
A copy of the next N bytes as a fixed-size array.

Definition at line 169 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNIODReleaseBlock::Parse(), profinet::PNInM0::Parse(), profinet::PNInM1::Parse(), profinet::PNInM2::Parse(), profinet::PNInM3::Parse(), profinet::PNInM4::Parse(), profinet::PNInM5::Parse(), profinet::PNInMReserved< Idx >::Parse(), profinet::PNIOCRBlockRes::Parse(), profinet::PNIOCRBlockReqHeader::Parse(), profinet::PNAlarmCRBlockRes::Parse(), profinet::PNAlarmNotificationPDU::Parse(), and profinet::PNIODWriteRes::Parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Need()

void profinet::wire::Reader::Need ( std::size_t  n) const
inline

Assert that at least n bytes remain.

Parameters
nNumber of bytes required.

Throws std::out_of_range if fewer than n bytes remain.

Definition at line 115 of file wire.h.

References Remaining().

Referenced by Fixed(), ReadBytes(), Skip(), U16(), U32(), and U8().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Position()

std::size_t profinet::wire::Reader::Position ( ) const
inline

Current read offset from the start of the buffer.

Returns
Number of bytes already consumed.

Definition at line 100 of file wire.h.

References pos.

◆ ReadBytes()

std::vector< std::uint8_t > profinet::wire::Reader::ReadBytes ( std::size_t  n)
inline

Read and consume a variable-length byte range.

Parameters
nNumber of bytes to read.
Returns
A copy of the next n bytes.

Definition at line 157 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNNRDData::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNInM0::Parse(), profinet::PNInM1::Parse(), profinet::PNInM2::Parse(), profinet::PNInM3::Parse(), profinet::PNInM5::Parse(), profinet::PNInMReserved< Idx >::Parse(), profinet::PNAlarmNotificationPDU::Parse(), and profinet::PNRTAHeader::Parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Remaining()

std::size_t profinet::wire::Reader::Remaining ( ) const
inline

Number of bytes not yet consumed.

Returns
Byte count remaining between the current position and the end of the buffer.

Definition at line 93 of file wire.h.

References len, and pos.

Referenced by Need(), profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNNRDData::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNInMReserved< Idx >::Parse(), profinet::PNAlarmNotificationPDU::Parse(), profinet::PNRTAHeader::Parse(), profinet::blocks::ParseModuleDiffBlock(), and profinet::blocks::ParseRealIdentificationData().

+ Here is the caller graph for this function:

◆ Skip()

void profinet::wire::Reader::Skip ( std::size_t  n)
inline

Advance the read position without returning the skipped bytes.

Parameters
nNumber of bytes to skip.

Definition at line 180 of file wire.h.

References Need(), and pos.

Referenced by profinet::blocks::ParseModuleDiffBlock(), profinet::blocks::ParseMultipleBlockHeader(), and profinet::blocks::ParseWriteMultipleResponse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ U16()

◆ U32()

std::uint32_t profinet::wire::Reader::U32 ( )
inline

Read and consume a big-endian 32-bit value.

Returns
The decoded value.

Definition at line 143 of file wire.h.

References data, Need(), profinet::OneOctetShift, pos, profinet::ThreeOctetsShift, and profinet::TwoOctetsShift.

Referenced by profinet::PNDCPHeader::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNNRDData::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNIOCRBlockReqHeader::Parse(), profinet::PNAlarmNotificationPDU::Parse(), profinet::PNIODWriteRes::Parse(), profinet::ParseAlarmNotification(), profinet::blocks::ParseModuleDiffBlock(), profinet::blocks::ParseMultipleBlockHeader(), profinet::blocks::ParsePortStatistics(), profinet::blocks::ParseRealIdentificationData(), and profinet::blocks::ParseWriteMultipleResponse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ U8()

std::uint8_t profinet::wire::Reader::U8 ( )
inline

Read and consume one byte.

Returns
The byte at the current position.

Definition at line 125 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNBlockHeader::Parse(), profinet::PNInM0::Parse(), profinet::PNRTAHeader::Parse(), and profinet::blocks::ParseBlockHeader().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ data

const std::uint8_t* profinet::wire::Reader::data
private

Pointer to the start of the underlying buffer (not owned).

Definition at line 188 of file wire.h.

Referenced by Cursor(), Fixed(), ReadBytes(), U16(), U32(), and U8().

◆ len

std::size_t profinet::wire::Reader::len
private

Total length of the underlying buffer in bytes.

Definition at line 191 of file wire.h.

Referenced by Remaining().

◆ pos

std::size_t profinet::wire::Reader::pos = 0
private

Current read offset from the start of the buffer.

Definition at line 194 of file wire.h.

Referenced by Cursor(), Fixed(), Position(), ReadBytes(), Remaining(), Skip(), U16(), U32(), and U8().


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