yat  0.18pre
Public Member Functions | List of all members
theplu::yat::utility::BasicStreamRedirect< charT, traits > Class Template Reference

Redirect a stream to another stream. More...

#include <yat/utility/StreamRedirect.h>

Public Member Functions

 BasicStreamRedirect (std::basic_istream< charT, traits > &is1, std::basic_istream< charT, traits > &is2, bool active=true)
 redirect istream to another istream More...
 
 BasicStreamRedirect (std::basic_istream< charT, traits > &is, const std::string &file, bool active=true)
 redirect istream to ifstream. More...
 
 BasicStreamRedirect (std::basic_ostream< charT, traits > &os1, std::basic_ostream< charT, traits > &os2, bool active=true)
 redirect ostream to another ostream More...
 
 BasicStreamRedirect (std::basic_ostream< charT, traits > &os, const std::string &file, bool active=true)
 redirect ostream to ofstream. More...
 
 ~BasicStreamRedirect (void)
 Destructor - resets the redirect. More...
 

Detailed Description

template<class charT, class traits = std::char_traits<charT>>
class theplu::yat::utility::BasicStreamRedirect< charT, traits >

Redirect a stream to another stream.

This class can be used to redirect a stream so it uses the buffer of another stream. This another stream is either a stream provided by the caller or if the caller provide a string the an internal fstream is created. In the destructor the stream is restored to use its own buffer. See documentation of CommandLine for an example of usage.

Note
It is important that an instance of this class is deallocated before the stream it holds (see BasicStreamRedirect::~BasicStreamRedirect).
See also
StreamRedirect and wStreamRedirect
Since
New in yat 0.6

Constructor & Destructor Documentation

template<class charT , class traits >
theplu::yat::utility::BasicStreamRedirect< charT, traits >::BasicStreamRedirect ( std::basic_istream< charT, traits > &  is1,
std::basic_istream< charT, traits > &  is2,
bool  active = true 
)

redirect istream to another istream

is1 is redirected to get its input from the buffer of is2 (rather than its own buffer). A reference to is1 and its buffer is stored, so is1 can be restored in the destructor.

If active is false, the class does nothing.

template<class charT , class traits >
theplu::yat::utility::BasicStreamRedirect< charT, traits >::BasicStreamRedirect ( std::basic_istream< charT, traits > &  is,
const std::string &  file,
bool  active = true 
)

redirect istream to ifstream.

An ifstream with path file is created, and is is redirected to get its input from the buffer of this ifstream.

If active is false, the class does nothing.

template<class charT , class traits >
theplu::yat::utility::BasicStreamRedirect< charT, traits >::BasicStreamRedirect ( std::basic_ostream< charT, traits > &  os1,
std::basic_ostream< charT, traits > &  os2,
bool  active = true 
)

redirect ostream to another ostream

os1 is redirected to send its output to the buffer of os2 (rather than its own buffer). A reference to os1 and its buffer is stored, so os1 can be restored in the destructor.

If active is false, the class does nothing.

template<class charT , class traits >
theplu::yat::utility::BasicStreamRedirect< charT, traits >::BasicStreamRedirect ( std::basic_ostream< charT, traits > &  os,
const std::string &  file,
bool  active = true 
)

redirect ostream to ofstream.

An ofstream with path file is created, and os is redirected to send its output to the buffer of this ofstream.

If active is false, the class does nothing.

template<class charT , class traits >
theplu::yat::utility::BasicStreamRedirect< charT, traits >::~BasicStreamRedirect ( void  )

Destructor - resets the redirect.

The stream (first argument in constructor) is restored to use its original buffer.

Note
It is important that the stream held is not deallocated before this object is destroyed. There are two reasons for this: 1) The behavior of this destructor is undefined. 2) The stream destroys the streambuf in its destructor, so if the stream is destroyed before it is restored, the streambuf will likely be deallocated twice. In addition, the original streambuf of the stream will not be closed and deallocated; hence a memory and resource leak.

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

Generated on Sun Sep 27 2020 02:26:14 for yat by  doxygen 1.8.11