Kouta
A small application framework based on Boost
kouta::io::Parser Class Reference

Binary data parser. More...

#include <parser.hpp>

Collaboration diagram for kouta::io::Parser:

Public Types

using View = std::span< const std::uint8_t >
 Underlying view data type. More...
 
using Order = boost::endian::order
 Endian ordering. More...
 

Public Member Functions

 Parser ()=delete
 
 Parser (const View &view)
 Constructor. More...
 
 Parser (const Parser &)=default
 
Parseroperator= (const Parser &)=default
 
 Parser (Parser &&)=default
 
Parseroperator= (Parser &&)=default
 
virtual ~Parser ()=default
 
const Viewview () const
 Obtain a reference to the internal data view. More...
 
std::size_t size () const
 Obtain the size of the internal data view. More...
 
template<std::integral TValue, std::size_t N = sizeof(TValue), Order Endian = Order::big>
TValue extract_integral (std::size_t offset) const
 Extract an integral value from the view. More...
 
template<std::floating_point TValue, Order Endian = Order::big>
TValue extract_floating_point (std::size_t offset) const
 Extract a floating point value from the view. More...
 
virtual std::string extract_string (std::size_t offset, std::size_t count) const
 Extract a string value from the view. More...
 

Private Member Functions

void check_bounds (std::size_t offset, std::size_t count) const
 Check that a specific range is within bounds. More...
 

Private Attributes

View m_view
 

Detailed Description

Binary data parser.

This class exposes a read-only API that facilitates extracting different data types from a byte sequence. It is backed by std::span, hence it is cheap to create it from an existing data container, or copy it from an existing parser (which could be useful when writing custom parsers).

Note
The parser does not own the memory, hence it is not thread-safe.

Member Typedef Documentation

◆ Order

using kouta::io::Parser::Order = boost::endian::order

Endian ordering.

◆ View

using kouta::io::Parser::View = std::span<const std::uint8_t>

Underlying view data type.

Constructor & Destructor Documentation

◆ Parser() [1/4]

kouta::io::Parser::Parser ( )
delete

◆ Parser() [2/4]

kouta::io::Parser::Parser ( const View view)
inlineexplicit

Constructor.

Parameters
[in]viewView from which to construct the parser.

◆ Parser() [3/4]

kouta::io::Parser::Parser ( const Parser )
default

◆ Parser() [4/4]

kouta::io::Parser::Parser ( Parser &&  )
default

◆ ~Parser()

virtual kouta::io::Parser::~Parser ( )
virtualdefault

Member Function Documentation

◆ check_bounds()

void kouta::io::Parser::check_bounds ( std::size_t  offset,
std::size_t  count 
) const
inlineprivate

Check that a specific range is within bounds.

Parameters
[in]offsetStarting offset.
[in]countNumber of bytes in the range.
Exceptions
std::out_of_rangewhen there are not enough bytes in the data view.

◆ extract_floating_point()

template<std::floating_point TValue, Order Endian = Order::big>
TValue kouta::io::Parser::extract_floating_point ( std::size_t  offset) const
inline

Extract a floating point value from the view.

The offset must be within bounds, considering the size of type

Template Parameters
TValue.Otherwise, an std::out_of_range exception will be thrown.

It is recommended to check the size() before attempting to extract a value.

Template Parameters
TValueThe numerical type to extract from the data view.
EndianEndian order of the value to extract.
Parameters
offsetThe offset from which to start extracting data.
Returns
Floating point value
Exceptions
std::out_of_rangewhen there are not enough bytes in the data view.

◆ extract_integral()

template<std::integral TValue, std::size_t N = sizeof(TValue), Order Endian = Order::big>
TValue kouta::io::Parser::extract_integral ( std::size_t  offset) const
inline

Extract an integral value from the view.

The offset must be within bounds, considering the size

Template Parameters
N.Otherwise, an std::out_of_range exception will be thrown.

It is recommended to check the size() before attempting to extract a value.

Template Parameters
TValueThe numerical type to extract from the data view.
NNumber of bytes to extract.
EndianEndian order of the value to extract.
Parameters
offsetThe offset from which to start extracting data.
Returns
Integral value
Exceptions
std::out_of_rangewhen there are not enough bytes in the data view.

◆ extract_string()

virtual std::string kouta::io::Parser::extract_string ( std::size_t  offset,
std::size_t  count 
) const
inlinevirtual

Extract a string value from the view.

The offset must be within bounds, considering the character count specified.

It is recommended to check the size() before attempting to extract a value.

This method is marked as virtual to allow overriding it in order to implement custmo behaviour such as interrupting the parsing whenever a null-character is found.

Parameters
offsetThe starting offset from which to start extracting data.
countThe number of characters/bytes to extract from the view.
Returns
String extracted from the view.
Exceptions
std::out_of_rangewhen there are not enough bytes in the data view.

◆ operator=() [1/2]

Parser& kouta::io::Parser::operator= ( const Parser )
default

◆ operator=() [2/2]

Parser& kouta::io::Parser::operator= ( Parser &&  )
default

◆ size()

std::size_t kouta::io::Parser::size ( ) const
inline

Obtain the size of the internal data view.

Note
This also corresponds to the number of bytes.

◆ view()

const View& kouta::io::Parser::view ( ) const
inline

Obtain a reference to the internal data view.

Member Data Documentation

◆ m_view

View kouta::io::Parser::m_view
private

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