The Battle for Wesnoth  1.19.5+dev
Public Member Functions | Private Member Functions | Private Attributes | List of all members
buffered_istream Class Reference

Helper class for buffering a std::istream. More...

#include <buffered_istream.hpp>

Public Member Functions

 buffered_istream (std::istream &in)
 
int get ()
 Gets and consumes a character from the buffer. More...
 
int peek ()
 Gets a character from the buffer. More...
 
bool eof () const
 Is the end of input reached? More...
 
std::istream & stream ()
 Returns the owned stream. More...
 

Private Member Functions

void fill_buffer ()
 Fills the buffer. More...
 

Private Attributes

std::istream & stream_
 The input to read from. More...
 
char buffer_ [1024]
 Buffer to store the data read from std::istream. More...
 
unsigned buffer_size_
 The size of buffer_. More...
 
unsigned buffer_offset_
 The offset of the current character in the buffer. More...
 
bool eof_
 Is the end of input reached? More...
 

Detailed Description

Helper class for buffering a std::istream.

This class is used to buffer a std::istream which is used for small reads; a character at a time. The std::istream needs to create a sentinel object for every read and profiling showed the std::istream class was causing a lot of overhead when parsing WML. This class helps by reading chunks from the std::stream and store them in an internal buffer. Then the next request can deliver data from this buffer.

Since the class is only designed for small reads it only offers the get() and the peek() to get data and eof() to signal the end of data. The original stream should not be used from, while being owned by this class.

Definition at line 41 of file buffered_istream.hpp.

Constructor & Destructor Documentation

◆ buffered_istream()

buffered_istream::buffered_istream ( std::istream &  in)
inlineexplicit

Definition at line 45 of file buffered_istream.hpp.

Member Function Documentation

◆ eof()

bool buffered_istream::eof ( ) const
inline

◆ fill_buffer()

void buffered_istream::fill_buffer ( )
inlineprivate

Fills the buffer.

Warning
This function must be called before peek() and get() to make sure the buffer state is valid before accessing it.

Definition at line 155 of file buffered_istream.hpp.

References buffer_, buffer_offset_, buffer_size_, eof_, and stream_.

Referenced by get(), and peek().

◆ get()

int buffered_istream::get ( )
inline

Gets and consumes a character from the buffer.

Returns
The character read.
Return values
EOFThe end of input has been read.

Definition at line 60 of file buffered_istream.hpp.

References buffer_, buffer_offset_, c, eof_, and fill_buffer().

Referenced by preprocessor_data::get_chunk(), tokenizer::next_char_skip_cr(), preprocessor_data::read_line(), preprocessor_data::read_rest_of_line(), preprocessor_data::read_word(), preprocessor_data::skip_eol(), and preprocessor_data::skip_spaces().

◆ peek()

int buffered_istream::peek ( )
inline

Gets a character from the buffer.

This version only gets a character, but doesn't consume it.

Returns
The character read.
Return values
EOFThe end of input has been read.

Definition at line 87 of file buffered_istream.hpp.

References buffer_, buffer_offset_, eof_, and fill_buffer().

Referenced by preprocessor_data::get_chunk(), tokenizer::peek_char(), preprocessor_data::read_rest_of_line(), preprocessor_data::read_word(), and preprocessor_data::skip_spaces().

◆ stream()

std::istream& buffered_istream::stream ( )
inline

Returns the owned stream.

Definition at line 106 of file buffered_istream.hpp.

References stream_.

Referenced by tokenizer::tokenizer(), and tokenizer::~tokenizer().

Member Data Documentation

◆ buffer_

char buffered_istream::buffer_[1024]
private

Buffer to store the data read from std::istream.

Reading from std::istream isn't to fast, especially not a byte at a time. This buffer is used to buffer x bytes at a time. The size of the buffer is determined experimentally.

Definition at line 123 of file buffered_istream.hpp.

Referenced by fill_buffer(), get(), and peek().

◆ buffer_offset_

unsigned buffered_istream::buffer_offset_
private

The offset of the current character in the buffer.

buffer_[buffer_offset_] is the current character, and can be peaked or consumed.

Note
the buffer_offset_ may be beyond the buffer_ so functions should test before directly using this variable.

Definition at line 144 of file buffered_istream.hpp.

Referenced by fill_buffer(), get(), and peek().

◆ buffer_size_

unsigned buffered_istream::buffer_size_
private

The size of buffer_.

When buffering the data there might be less data in the stream as in the buffer. This variable contains the exact size of the buffer. For example the last chunk read from the stream is unlikely to have the same size a buffer_.

Definition at line 133 of file buffered_istream.hpp.

Referenced by fill_buffer().

◆ eof_

bool buffered_istream::eof_
private

Is the end of input reached?

Definition at line 147 of file buffered_istream.hpp.

Referenced by eof(), fill_buffer(), get(), and peek().

◆ stream_

std::istream& buffered_istream::stream_
private

The input to read from.

Definition at line 114 of file buffered_istream.hpp.

Referenced by fill_buffer(), and stream().


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