LineReader module

Module contents

template<class StringT = std::string>
class LineReader

Read data from a stream one line at a time.

This class is an adapter that takes a std::istream as input and provides an InputIterator that loops over the lines of the stream. In its current version it delegates to the getline() function to determine when a line has ended.

The behavior is meant to be the same as the Python idiom for line in file.

We do not expect you to instantiate this class directly. Instead, it shows up as part of PointReader where you put a stream in one end and get points out the other end.

Public Types

typedef StringT value_type
typedef StringT::value_type char_type
typedef StringT::traits_type traits_type
typedef std::basic_istream<char_type, traits_type> istream_type
typedef LineReaderIterator iterator
typedef LineReaderIterator const const_iterator

Public Functions

inline LineReader()

Instantiate an empty line reader.

inline LineReader(const LineReader &other)

Copy contructor, create a reader with a copy of another

Parameters:

other[in] Line reader to copy from

inline LineReader(istream_type &stream)

Instantiate a line reader with std::basic_istream

Parameters:

stream[in] Stream to read lines in

inline virtual ~LineReader()

Destructor for a line reader.

inline LineReader &operator=(LineReader const &other)

Assign a LineReader to the value of another.

Parameters:

other[in] LineReader to assign value of

Returns:

Stream with the new assigned value

inline void set_input(istream_type &stream)

Set the input for the LineReader stream to that of the given istream

Parameters:

stream[in] The input to use for input

inline istream_type &input() const

Get the input value of the stream

Returns:

The stream input value

inline bool operator==(LineReader const &other) const

Check whether one stream is equal to another by comparing all the items.

Two reader are equal if all of their streams are equal.

Parameters:

other[in] LineReader for comparison

Returns:

Boolean indicating equivalency

inline bool operator!=(LineReader const &other) const

Check whether two LineReaders are unequal.

Parameters:

other[in] LineReader for comparison

Returns:

Boolean indicating equivalency

inline iterator begin() const

Get an iterator pointing to the beginning of the stream

Returns:

Iterator pointing to current stream

inline iterator end() const

Get an iterator pointing to the end of the stream

Returns:

Iterator pointing to end of current stream

inline const_iterator const_begin() const

Get an iterator pointing to the beginning of the stream

Returns:

Iterator pointing to current stream

inline const_iterator const_end() const

Get an iterator pointing to the end of the stream

Returns:

Iterator pointing to end of current stream

Private Members

istream_type *Stream
class LineReaderIterator

Line Reader iterator class

Generates a iterator that can traverse the given parent LineReader

Public Types

using iterator_category = std::input_iterator_tag
using value_type = StringT
using difference_type = std::ptrdiff_t
using pointer = StringT*
using reference = StringT&
using char_type = typename StringT::value_type
using traits_type = typename StringT::traits_type

Public Functions

inline LineReaderIterator()

Instantiate an empty reader iterator.

inline LineReaderIterator(istream_type *stream)

Copy contructor, create a line reader iterator with a copy of a std::basic_istream

inline LineReaderIterator(LineReaderIterator const &other)

Copy contructor, create a line reader iterator with a copy of another.

inline ~LineReaderIterator()

Destructor for a line reader iterator.

inline LineReaderIterator &operator=(LineReaderIterator const &other)

Assign a LineReaderIterator to the value of another.

Parameters:

other[in] LineReaderIterator to assign value of

Returns:

Iterator with the new assigned value

inline value_type const &operator*() const

Multiply an iterator.

Returns:

Result of the multiplication

inline value_type *operator->() const

Get the current iterator value.

Returns:

Current iterator value

inline LineReaderIterator &operator++()

Advance the iterator to the next line.

Returns:

Pointer to the next line

inline LineReaderIterator &operator++(int)

Advance the iterator to the next line.

Returns:

Pointer to the next line

inline bool operator==(LineReaderIterator const &other) const

Check whether one iterator is equal to another by comparing the values and streams.

Two items are equal if all of their values and streams are equal.

Parameters:

other[in] Iterator for comparison

Returns:

Boolean indicating equivalency

inline bool operator!=(LineReaderIterator const &other) const

Check whether two iterators are unequal.

Parameters:

other[in] Iterator for comparison

Returns:

Boolean indicating equivalency

inline value_type get_value() const

Get the stored value

This is to handle the case where the stream is NULL but there wasn’t a new line at the end of the file. In that case we return an empty value so that comparisons evaluate properly.

Returns:

Value unless the Stream is Null, in which case we return an empty value

Public Members

istream_type *Stream
value_type Value
int Counter