MAGE  v0.171.0
Matthias Advanced Game Engine
mage::LineReader Class Referenceabstract

#include <line_reader.hpp>

Inheritance diagram for mage::LineReader:
mage::loader::VARReader mage::rendering::loader::MDLReader< VertexT, IndexT > mage::rendering::loader::MTLReader mage::rendering::loader::OBJReader< VertexT, IndexT >

Public Types

using SelectionFunction = std::function< const std::ssub_match(const std::smatch &) >
 

Public Member Functions

LineReaderoperator= (const LineReader &reader)=delete
 
LineReaderoperator= (LineReader &&reader) noexcept
 
void ReadFromFile (std::filesystem::path path, std::regex regex=s_default_regex, SelectionFunction selection_function=s_default_selection_function)
 
void ReadFromMemory (const std::string &input, std::regex regex=s_default_regex, SelectionFunction selection_function=s_default_selection_function)
 

Static Public Attributes

static const std::regex s_default_regex = std::regex(R"((\"([^\"]*)\")|(\S+))")
 
static const SelectionFunction s_default_selection_function
 

Protected Member Functions

 LineReader ()
 
 LineReader (const LineReader &reader)=delete
 
 LineReader (LineReader &&reader) noexcept
 
 ~LineReader ()
 
const std::filesystem::path & GetPath () const noexcept
 
U32 GetCurrentLineNumber () const noexcept
 
template<typename T >
const T Read ()
 
template<typename T , std::size_t N, size_t A = alignof(T)>
const Array< T, N, A > Read ()
 
void ReadRemainingTokens ()
 
template<typename T >
bool Contains () const noexcept
 
bool ContainsTokens () const noexcept
 

Private Member Functions

virtual void Preprocess ()
 
void Process (std::istream &stream)
 
virtual void ReadLine ()=0
 
virtual void Postprocess ()
 
const std::string_view GetCurrentToken () const noexcept
 

Private Attributes

std::regex m_regex
 
SelectionFunction m_selection_function
 
std::filesystem::path m_path
 
std::sregex_iterator m_iterator
 
U32 m_line_number
 

Detailed Description

A class of line readers for reading (non-binary) text files line by line.

Member Typedef Documentation

◆ SelectionFunction

using mage::LineReader::SelectionFunction = std::function< const std::ssub_match(const std::smatch&) >

The selection function type for extracting one std::ssub_match from a given std::smatch.

Constructor & Destructor Documentation

◆ LineReader() [1/3]

mage::LineReader::LineReader ( )
protected

Constructs a line reader.

◆ LineReader() [2/3]

mage::LineReader::LineReader ( const LineReader reader)
protecteddelete

Constructs a line reader from the given line reader.

Parameters
[in]readerA reference to the line reader to copy.

◆ LineReader() [3/3]

mage::LineReader::LineReader ( LineReader &&  reader)
protectednoexcept

Constructs a line reader by moving the given line reader.

Parameters
[in]readerA reference to the line reader to move.

◆ ~LineReader()

mage::LineReader::~LineReader ( )
protecteddefault

Destructs this line reader.

Member Function Documentation

◆ Contains()

template<typename T >
bool mage::LineReader::Contains ( ) const
protectednoexcept

Checks whether the current token of this line reader is a T value.

Template Parameters
TThe data type.
Returns
true if the current token of this line reader is a T value. false otherwise.

◆ ContainsTokens()

bool mage::LineReader::ContainsTokens ( ) const
protectednoexcept

Checks whether this line reader has a current token.

Returns
true if this line reader has a current token. false otherwise.

◆ GetCurrentLineNumber()

U32 mage::LineReader::GetCurrentLineNumber ( ) const
protectednoexcept

Returns the current line number of this line reader.

Returns
The current line number of this line reader.

◆ GetCurrentToken()

const std::string_view mage::LineReader::GetCurrentToken ( ) const
privatenoexcept

Returns the current token of this line reader.

Returns
The current token of this line reader.

◆ GetPath()

const std::filesystem::path& mage::LineReader::GetPath ( ) const
protectednoexcept

Returns the current path of this line reader.

Returns
A reference to the current path of this line reader.

◆ operator=() [1/2]

LineReader& mage::LineReader::operator= ( const LineReader reader)
delete

Copies the given line reader to this line reader.

Parameters
[in]readerA reference to a line reader to copy.
Returns
A reference to the copy of the given line reader (i.e. this line reader).

◆ operator=() [2/2]

LineReader & mage::LineReader::operator= ( LineReader &&  reader)
noexcept

Moves the given line reader to this line reader.

Parameters
[in]readerA reference to a line reader to move.
Returns
A reference to the moved line reader (i.e. this line reader).

◆ Postprocess()

void mage::LineReader::Postprocess ( )
privatevirtual

Post-processes after reading.

Exceptions
ExceptionFailed to finish post-processing successfully.

Reimplemented in mage::rendering::loader::OBJReader< VertexT, IndexT >, and mage::rendering::loader::MDLReader< VertexT, IndexT >.

◆ Preprocess()

void mage::LineReader::Preprocess ( )
privatevirtual

Pre-processes before reading.

Exceptions
ExceptionFailed to finish the pre-processing successfully.

Reimplemented in mage::rendering::loader::OBJReader< VertexT, IndexT >, and mage::rendering::loader::MDLReader< VertexT, IndexT >.

◆ Process()

void mage::LineReader::Process ( std::istream &  stream)
private

Processes the given input stream (line by line).

Parameters
[in]streamA reference to the input stream.
Exceptions
ExceptionFailed to process the given input stream.

◆ Read() [1/2]

template<typename T >
const T mage::LineReader::Read ( )
protected

Reads and converts the current token of this line reader to T value.

Template Parameters
TThe data type.
Returns
The T represented by the current token of this line reader.
Exceptions
ExceptionThere is no current token or the current token does not represent a T value.

◆ Read() [2/2]

template<typename T , std::size_t N, size_t A = alignof(T)>
const Array< T, N, A > mage::LineReader::Read ( )
protected

Reads and converts the current N tokens of this line reader to an Array.

@tparam T The data type.

Template Parameters
NThe number of values in the array.
AThe alignment of the array.
Returns
The Array represented by the current N tokens of this line reader.
Exceptions
ExceptionThere are no N current tokens or the current N tokens do not represent a T value.

◆ ReadFromFile()

void mage::LineReader::ReadFromFile ( std::filesystem::path  path,
std::regex  regex = s_default_regex,
SelectionFunction  selection_function = s_default_selection_function 
)

Reads from the file associated with the given path.

Parameters
[in]pathThe path.
[in]regexThe (line) regex.
[in]selection_functionThe selection function.
Exceptions
ExceptionFailed to read from the file.

◆ ReadFromMemory()

void mage::LineReader::ReadFromMemory ( const std::string &  input,
std::regex  regex = s_default_regex,
SelectionFunction  selection_function = s_default_selection_function 
)

Reads from the given input string.

Parameters
[in]inputA reference to the input string.
[in]regexThe (line) regex.
[in]selection_functionThe selection function.
Exceptions
ExceptionFailed to read from the given input string.

◆ ReadLine()

virtual void mage::LineReader::ReadLine ( )
privatepure virtual

Reads the current line of this line reader.

Exceptions
ExceptionFailed to the current line of this line reader.

Implemented in mage::rendering::loader::OBJReader< VertexT, IndexT >, mage::rendering::loader::MDLReader< VertexT, IndexT >, mage::rendering::loader::MTLReader, and mage::loader::VARReader.

◆ ReadRemainingTokens()

void mage::LineReader::ReadRemainingTokens ( )
protected

Reads the remaining tokens of the current line of this line reader.

Member Data Documentation

◆ m_iterator

std::sregex_iterator mage::LineReader::m_iterator
private

An iterator to the current token of the current line of this line reader.

◆ m_line_number

U32 mage::LineReader::m_line_number
private

The current line number of this line reader.

◆ m_path

std::filesystem::path mage::LineReader::m_path
private

The current path of this line reader.

◆ m_regex

std::regex mage::LineReader::m_regex
private

The current (line) regex of this line reader.

◆ m_selection_function

SelectionFunction mage::LineReader::m_selection_function
private

The function for selecting the target submatch from the match of the regular expression represented by the regex of this strategy.

◆ s_default_regex

const std::regex mage::LineReader::s_default_regex = std::regex(R"((\"([^\"]*)\")|(\S+))")
static

The default (line) regex for line readers.

◆ s_default_selection_function

const LineReader::SelectionFunction mage::LineReader::s_default_selection_function
static
Initial value:
= [](const std::smatch& match) {
return match[2].length() ? match[2] : match[3];
}

The default selection function for line readers.