2.10.0
Freundlich's C++ toolkit
Classes | Functions
fcppt.io

Description

IO-related typedefs and functions.

Introduction

The io namespace mostly includes typedefs and functions related to the string module, see fcppt.string. There are some functions that deal with I/O and endianness at the same time, also living in the io namespace.

I/O and endianness

The functions dealing with I/O and endianness are fcppt::io::read and fcppt::io::write. They can be used to read or write objects of fundamental types from or to char based streams. All of these functions treat the data as binary data, which means they don't use the stream extractors and inserters, but they use read and write directly.

Here is an example:

// Write 42u into the stream, using big endianness
stream,
42u,
);
// Read the written unsigned int back
unsigned
>(
stream,
),
[](
unsigned const _result
)
{
<< _result
<< FCPPT_TEXT('\n');
}
);

Classes

class  fcppt::io::basic_scoped_rdbuf< Ch, Traits >
 Gives a streambuf from one stream to another, restoring the original state in the destructor. More...
 

Functions

template<typename Ch , typename Traits , typename Type >
std::basic_istream< Ch, Traits > & fcppt::io::expect (std::basic_istream< Ch, Traits > &_stream, Type const &_value)
 Fails a stream if it does not read the expected value. More...
 
template<typename Type , typename Ch , typename Traits >
fcppt::optional::object< Type > fcppt::io::extract (std::basic_istream< Ch, Traits > &_stream)
 Reads a value from a stream, returning an optional. More...
 
template<typename Ch , typename Traits >
fcppt::optional::object< Ch > fcppt::io::get (std::basic_istream< Ch, Traits > &_stream)
 Gets a character from a stream. More...
 
template<typename Ch , typename Traits >
fcppt::optional::object< Ch > fcppt::io::peek (std::basic_istream< Ch, Traits > &_stream)
 Peeks at a character from a stream. More...
 
template<typename Type >
fcppt::optional::object< Type > fcppt::io::read (std::istream &_stream, fcppt::endianness::format const _format)
 Reads an object of arithmetic type from a stream. More...
 
FCPPT_DETAIL_SYMBOL fcppt::io::optional_buffer fcppt::io::read_chars (std::istream &stream, std::size_t count)
 Reads a number of chars. More...
 
fcppt::io::detail::widen_string fcppt::io::widen_string (std::string _string)
 Creates a string that outputs each character by widening. More...
 
template<typename Type >
void fcppt::io::write (std::ostream &_stream, Type const &_value, fcppt::endianness::format const _format)
 Writes an object of arithmetic type to a stream. More...
 
FCPPT_DETAIL_SYMBOL bool fcppt::io::write_chars (std::ostream &stream, char const *data, std::size_t count) FCPPT_PP_WARN_UNUSED_RESULT
 Writes a number of chars. More...
 

Function Documentation

◆ expect()

template<typename Ch , typename Traits , typename Type >
std::basic_istream< Ch, Traits>& fcppt::io::expect ( std::basic_istream< Ch, Traits > &  _stream,
Type const &  _value 
)
inline

Fails a stream if it does not read the expected value.

Tries to read a value of type Type from _stream. If the value read is unequal to _value, the failbit is set for _stream.

Template Parameters
TypeMust be a fundamental type

◆ extract()

template<typename Type , typename Ch , typename Traits >
fcppt::optional::object< Type> fcppt::io::extract ( std::basic_istream< Ch, Traits > &  _stream)
inline

Reads a value from a stream, returning an optional.

Uses operator>> to extract a value of type Type from _stream. If extracting the value fails, an empty optional is returned.

Template Parameters
TypeMust have a default constructor or a constructor for fcppt::no_init.

◆ get()

template<typename Ch , typename Traits >
fcppt::optional::object< Ch> fcppt::io::get ( std::basic_istream< Ch, Traits > &  _stream)

Gets a character from a stream.

Reads a character from _stream. Returns an empty optional for end-of-file.

◆ peek()

template<typename Ch , typename Traits >
fcppt::optional::object< Ch> fcppt::io::peek ( std::basic_istream< Ch, Traits > &  _stream)

Peeks at a character from a stream.

Peeks at a character from _stream. Returns an empty optional for end-of-file.

◆ read()

template<typename Type >
fcppt::optional::object< Type> fcppt::io::read ( std::istream &  _stream,
fcppt::endianness::format const  _format 
)

Reads an object of arithmetic type from a stream.

Reads an arithmetic type Type from _stream using the endianness of _format. The read will be done binary. If it fails, an empty optional will be returned.

Template Parameters
TypeMust be an arithmetic type
Parameters
_streamThe stream to read from
_formatThe endianness to use
Returns
If the read is successful, an optional containing the read object will be returned. Otherwise an empty optional will be returned.

◆ read_chars()

FCPPT_DETAIL_SYMBOL fcppt::io::optional_buffer fcppt::io::read_chars ( std::istream &  stream,
std::size_t  count 
)

Reads a number of chars.

Tries to read count chars from stream.

◆ widen_string()

fcppt::io::detail::widen_string fcppt::io::widen_string ( std::string  _string)
inline

Creates a string that outputs each character by widening.

◆ write()

template<typename Type >
void fcppt::io::write ( std::ostream &  _stream,
Type const &  _value,
fcppt::endianness::format const  _format 
)

Writes an object of arithmetic type to a stream.

Writes _value to _stream using the endianness of _format. The write will be done binary.

Template Parameters
TypeMust be an arithmetic type
Parameters
_streamThe stream to write to
_valueThe value to write
_formatThe endianness to use

◆ write_chars()

FCPPT_DETAIL_SYMBOL bool fcppt::io::write_chars ( std::ostream &  stream,
char const *  data,
std::size_t  count 
)

Writes a number of chars.

Tries to write count chars from data to stream.

Returns
If the write operation succeeded.