pflib v3.0.0-rc1-29-g3a901ac
Pretty Fine HGCROC Interaction Library
All Classes Namespaces Files Functions Variables Typedefs Pages
pflib::packing::Reader Class Referenceabstract

Reading a raw data stream with some underlying backend. More...

#include <Reader.h>

Inheritance diagram for pflib::packing::Reader:
[legend]

Public Member Functions

 Reader ()=default
 default constructor
 
virtual ~Reader ()=default
 virtual destructor for inheritance
 
virtual void seek (int off)=0
 Go ("seek") a specific position in the stream.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
void seek (int off)
 Seek by number of words.
 
virtual int tell ()=0
 Tell us where the reader is.
 
template<typename WordType >
int tell ()
 Tell by number of words.
 
virtual Readerread (char *w, std::size_t count)=0
 Read the next 'count' bytes into the input handle w.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Readerread (WordType *w, std::size_t count)
 Read the next 'count' words into the input handle.
 
template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Readeroperator>> (WordType &w)
 Stream the next word into the input handle.
 
template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Readeroperator>> (ObjectType &o)
 Stream into a class object.
 
template<typename ContentType >
Readerread (std::vector< ContentType > &vec, std::size_t count, std::size_t offset=0)
 Read the next 'count' objects into the input vector.
 
virtual bool good () const =0
 Check if reader is in a good state.
 
virtual bool operator! () const
 Check if reader is in a fail state.
 
virtual operator bool () const
 Check if reader is in good/bad state.
 
virtual bool eof ()=0
 check if file is done
 

Detailed Description

Reading a raw data stream with some underlying backend.

This abstract base class assumes a derived class defines how to extract the next byte of data from some input data stream (e.g. either a in-memory buffer or a binary file). Then this interface has several templated methods that deduce how many bytes to extract and calls read methods on objects that are not integral types.

Constructor & Destructor Documentation

◆ Reader()

pflib::packing::Reader::Reader ( )
default

default constructor

We make sure that our input file stream will not skip white space.

Member Function Documentation

◆ eof()

virtual bool pflib::packing::Reader::eof ( )
pure virtual

check if file is done

Returns
true if we have reached the end of file.

Implemented in pflib::packing::FileReader.

◆ good()

virtual bool pflib::packing::Reader::good ( ) const
pure virtual

Check if reader is in a good state.

Returns
bool true if reader is good and can keep reading

Implemented in pflib::packing::BufferReader, and pflib::packing::FileReader.

◆ operator bool()

virtual pflib::packing::Reader::operator bool ( ) const
inlinevirtual

Check if reader is in good/bad state.

Defining this operator allows us to do the following.

// r is some concreate instance of a Reader
if (r) {
std::cout << "r can read more data" << std::endl;
}
T endl(T... args)
Returns
bool true if reader is in good state

◆ operator!()

virtual bool pflib::packing::Reader::operator! ( ) const
inlinevirtual

Check if reader is in a fail state.

Returns
bool true if reader is in fail state

◆ operator>>() [1/2]

template<typename ObjectType , std::enable_if_t< std::is_class< ObjectType >::value, bool > = true>
Reader & pflib::packing::Reader::operator>> ( ObjectType & o)
inline

Stream into a class object.

We assume that the class we are streaming to has a specific method defined.

Reader& read(Reader&)

This can be satisfied by the classes that we own and operate.

Template Parameters

in] ObjectType class type to read

Parameters
[in]oinstance of object to read into
Returns
*this

◆ operator>>() [2/2]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Reader & pflib::packing::Reader::operator>> ( WordType & w)
inline

Stream the next word into the input handle.

This implementation of the stream operator is only available to handles of integral types. Helps for shorthand of only grabbing a single word from the reader.

See also
read
Template Parameters

in] WordType integral-type word to read out

Parameters
[in]wreference to word to read into
Returns
handle to modified reader

◆ read() [1/3]

virtual Reader & pflib::packing::Reader::read ( char * w,
std::size_t count )
pure virtual

Read the next 'count' bytes into the input handle w.

Parameters
[in]wpointer to array to write data into
[in]countnumber of bytes to read
Returns
(*this)

Implemented in pflib::packing::BufferReader, and pflib::packing::FileReader.

◆ read() [2/3]

template<typename ContentType >
Reader & pflib::packing::Reader::read ( std::vector< ContentType > & vec,
std::size_t count,
std::size_t offset = 0 )
inline

Read the next 'count' objects into the input vector.

This is common enough, I wanted to specialize the read function.

The std::vector::resize is helpful for avoiding additional copies while the vector is being expanded. After allocating the space for each entry in the vector, we call the stream operator from *this into each entry in order, leaving early if a failure occurs.

Template Parameters

in] ContentType type of object inside the vector

Parameters
[in]vecobject vector to read into
[in]countnumber of objects to read
Returns
*this

◆ read() [3/3]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
Reader & pflib::packing::Reader::read ( WordType * w,
std::size_t count )
inline

Read the next 'count' words into the input handle.

This implementation of read is only available to pointers to integral types. We assume that whatever space pointed to by w already has the space reserved necessary for the input words.

Template Parameters

in] WordType integral-type word to read out

Parameters
[in]wpointer to WordType array to write data to
[in]countnumber of words to read
Returns
(*this)

◆ seek() [1/2]

template<typename WordType , std::enable_if_t< std::is_integral< WordType >::value, bool > = true>
void pflib::packing::Reader::seek ( int off)
inline

Seek by number of words.

This template version of seek uses the input word type to move around by the count of the input words rather than the count of bytes.

Template Parameters

in] WordType Integral-type to count by

Parameters
[in]offnumber of words to move relative to dir
[in]dirlocation flag for the file, default is beginning

◆ seek() [2/2]

virtual void pflib::packing::Reader::seek ( int off)
pure virtual

Go ("seek") a specific position in the stream.

This non-template version of seek uses the default meaning of the "off" parameter in which it counts bytes.

Parameters
[in]offnumber of bytes to move relative to dir
[in]dirlocation flag for the file, default is beginning

Implemented in pflib::packing::BufferReader, and pflib::packing::FileReader.

◆ tell() [1/2]

template<typename WordType >
int pflib::packing::Reader::tell ( )
inline

Tell by number of words.

Returns
int number of words relative to beginning of file

◆ tell() [2/2]

virtual int pflib::packing::Reader::tell ( )
pure virtual

Tell us where the reader is.

Returns
int number of bytes relative to beginning of file

Implemented in pflib::packing::BufferReader, and pflib::packing::FileReader.


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