The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 42 of file buffered_istream.hpp.

Constructor & Destructor Documentation

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

Definition at line 46 of file buffered_istream.hpp.

Member Function Documentation

bool buffered_istream::eof ( ) const
inline
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 156 of file buffered_istream.hpp.

References UNLIKELY.

Referenced by get(), and peek().

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 61 of file buffered_istream.hpp.

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

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

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 88 of file buffered_istream.hpp.

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

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

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

Returns the owned stream.

Definition at line 107 of file buffered_istream.hpp.

References stream_.

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

Member Data Documentation

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 124 of file buffered_istream.hpp.

Referenced by get(), and peek().

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 145 of file buffered_istream.hpp.

Referenced by get(), and peek().

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 134 of file buffered_istream.hpp.

bool buffered_istream::eof_
private

Is the end of input reached?

Definition at line 148 of file buffered_istream.hpp.

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

std::istream& buffered_istream::stream_
private

The input to read from.

Definition at line 115 of file buffered_istream.hpp.

Referenced by stream().


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