48
What is the type of std::cout? Where does a stream go?

What is the type of std::cout? Where does a stream go?

Embed Size (px)

Citation preview

Page 1: What is the type of std::cout? Where does a stream go?

What is the type of std::cout?

Where does a stream go?

Page 2: What is the type of std::cout? Where does a stream go?

Paul and Jez'sStream-a-poloza

Paul [email protected]

Jez [email protected]

Page 3: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

Page 4: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::stringIn the last three months,have you used ...

Page 5: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::string● std::vector

In the last three months,have you used ...

Page 6: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::string● std::vector● std::map

In the last three months,have you used ...

Page 7: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::string● std::vector● std::map● an iterator

In the last three months,have you used ...

Page 8: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::string● std::vector● std::map● an iterator● an algorithm

In the last three months,have you used ...

Page 9: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::string● std::vector● std::map● an iterator● an algorithm ● operator<<

In the last three months,have you used ...

Page 10: What is the type of std::cout? Where does a stream go?

Let's talk about you ...

● std::string● std::vector● std::map● an iterator● an algorithm ● operator<< ● a custom stream

In the last three months,have you used ...

Page 11: What is the type of std::cout? Where does a stream go?

ios_base

basic_ios< >

basic_iostream< >

basic_istream< > basic_ostream< >

virtual

Page 12: What is the type of std::cout? Where does a stream go?

● Streams the write

std::cout << “Hello, world!”;● Stream the read

int n;std::cin >> n;

● Streams that do both

std::stringsteam ss;...ss << “Hello, world!”;...ss >> some_string;

Page 13: What is the type of std::cout? Where does a stream go?

ios_base

basic_ios< >

basic_iostream< >

basic_istream< > basic_ostream< >

virtual

basic_ostringstream

Page 14: What is the type of std::cout? Where does a stream go?

ios_base

basic_ios< >

basic_iostream< >

basic_istream< > basic_ostream< >

virtual

basic_ostringstreambasic_istringstream

Page 15: What is the type of std::cout? Where does a stream go?

ios_base

basic_ios< >

basic_iostream< >

basic_istream< > basic_ostream< >

virtual

basic_stringstream

basic_ostringstreambasic_istringstream

Page 16: What is the type of std::cout? Where does a stream go?

27.6.2 - “The header <ostream> defines a type and several function signatures that control output to a stream buffer.”

explicit basic_ostream(basic_streambuf<char_type, traits>* sb);

Page 17: What is the type of std::cout? Where does a stream go?

ios_base

basic_ios< >

basic_iostream< >

basic_istream< > basic_ostream< >

virtual

basic_stringstream

basic_ostringstreambasic_istringstream

Page 18: What is the type of std::cout? Where does a stream go?

basic_streambuf< >

Page 19: What is the type of std::cout? Where does a stream go?

27.5 - “The header <streambuf> defines types that control input from and output to character sequences”

Page 20: What is the type of std::cout? Where does a stream go?

basic_streambuf< >

basic_stringbuf< >

Page 21: What is the type of std::cout? Where does a stream go?

This is all very well, but why?

● Known interface● All the formatting stuff works● Do you want printf/sprintf/fprintf/snprintf?● Write to something other than the console,

memory or a file?

Page 22: What is the type of std::cout? Where does a stream go?

Digging A DitchWriting a Custom Stream

A Logging Stream must:

• Buffer the characters streamed to it• Flush to another output stream• Send time and date characters to an output

stream

Page 23: What is the type of std::cout? Where does a stream go?

Stream BufferThe heart of a stream

● Inherit from std::basic_streambuf ● Implement a buffer● Override virtual functions:

– overflow– sync– xsputn

● Flush to another output stream

Page 24: What is the type of std::cout? Where does a stream go?

Stream BufferImplement a buffer

#include <streambuf>#include <vector>

template< class charT, class traits = std::char_traits< charT > >class logoutbuf : public std::basic_streambuf< charT, traits >{private: typedef std::vector< charT > buffer_type; buffer_type buffer_;

... };

Page 25: What is the type of std::cout? Where does a stream go?

Stream BufferAdding to the buffer

virtual int_type overflow( int_type c ) { if ( !traits::eq_int_type( c, traits::eof() ) ) { buffer_.push_back( c ); }

return traits::not_eof( c ); }

Page 26: What is the type of std::cout? Where does a stream go?

Stream BufferSetting the output stream

private: typedef typename std::basic_streambuf< charT, traits >::int_type int_type; std::basic_streambuf < charT, traits >* out_;

public: logoutbuf() : out_( 0 ), buffer_() { }

void init( std::basic_ostream< charT, traits >* out ) { out_ = out; }

Page 27: What is the type of std::cout? Where does a stream go?

Stream BufferFlushing the buffer

virtual int sync() { if ( !buffer_.empty() && out_ ) { out_->sputn( &buffer_[0], static_cast< std::streamsize >( buffer_.size() ) ); buffer_.clear(); } return 0; }

Page 28: What is the type of std::cout? Where does a stream go?

Stream BufferWhen to flush the buffer

template< class charT, class traits = std::char_traits< charT > >class logoutbuf : public std::basic_streambuf< charT, traits >{

...

public:

...

~logoutbuf() { sync(); }

...

};

Page 29: What is the type of std::cout? Where does a stream go?

Stream BufferGenerating time data string

private: std::basic_string< charT, traits > format_time() { // Get current time and date time_t ltime; time( &ltime );

// Convert time and date to string std::basic_stringstream< charT, traits > time; time << asctime( gmtime( &ltime ) );

// Remove LF from time date string and // add separator std::basic_stringstream< char_type > result; result << time.str().erase( time.str().length() - 1 ) << " - ";

return result.str(); }

Page 30: What is the type of std::cout? Where does a stream go?

Stream BufferPrepending the time date string

virtual int sync(){ if ( !buffer_.empty() && out_ ) { const std::basic_string< charT, traits > time = format_time(); out_->sputn( time.c_str(), static_cast< std::streamsize >( time.length() ) ); out_->sputn( &buffer_[0], static_cast< std::streamsize >( buffer_.size() ) ); buffer_.clear(); } return 0;}

Page 31: What is the type of std::cout? Where does a stream go?

Stream BufferIn the name of efficiency

virtual std::streamsize xsputn( const char_type* s, std::streamsize num ) { std::copy( s, s + num, std::back_inserter< buffer_type >( buffer_ ) ); return num; }

Page 32: What is the type of std::cout? Where does a stream go?

The StreamThe conventional Method

template< class charT, class traits = std::char_traits< charT > >class ostream : public std::basic_ostream< charT, traits >{private: logoutbuf< charT, traits > buf_;

public: ostream() : std::basic_ostream< charT, traits >( &buf_ ), buf_() { }

};

What’s wrong with this?

Page 33: What is the type of std::cout? Where does a stream go?

The StreamPrivate Base Class (1)

template< class charT, class traits = std::char_traits< charT > >struct logoutbuf_init{

private: logoutbuf< charT, traits > buf_;

public: logoutbuf< charT, traits >* buf() { return &buf_; }

};

Page 34: What is the type of std::cout? Where does a stream go?

The StreamPrivate Base Class (2)

template< class charT, class traits = std::char_traits< charT > >class logostream : private logoutbuf_init < charT, traits >, public std::basic_ostream< charT, traits >{

private: typedef outbuf_init< charT, traits > logoutbuf_init;

public: logostream() : logoutbuf_init(), std::basic_ostream< charT, traits >( logoutbuf_init::buf() ) { }

};

Page 35: What is the type of std::cout? Where does a stream go?

The Streambasic_ios

ios_base

basic_ios< >

basic_ostream< >

virtual

27.4.4/2 - Effects: Constructs an object of class basic_ios (27.4.2.7) leaving its member objects uninitialized. The object must be initialized by calling its init member function. If it is destroyed before it has been initialized the behavior is undefined.

Page 36: What is the type of std::cout? Where does a stream go?

The StreamInitialisation Order (1)

basic_ios

logoutbuf

logoutbuf_init

basic_ostream

logostream

Page 37: What is the type of std::cout? Where does a stream go?

The StreamInherit Virtually and Privately

template< class charT, class traits = std::char_traits< charT > >class logostream : private virtual logoutbuf_init <charT, traits>, public std::basic_ostream< charT, traits >{

private: typedef outbuf_init< charT, traits > logoutbuf_init;

public: logostream() : logoutbuf_init(), std::basic_ostream< charT, traits >( logoutbuf_init::buf ) ) { }

};

Page 38: What is the type of std::cout? Where does a stream go?

The StreamInitialisation Order (2)

logoutbuf

logoutbuf_init

basic_ios

basic_ostream

Logostream

Page 39: What is the type of std::cout? Where does a stream go?

The StreamInitialising the output stream

public: logostream( std::basic_ostream< charT, traits >& out ) : logoutbuf_init(), std::basic_ostream< charT, traits > ( logoutbuf_init::buf() )

{ logoutbuf_init::buf()->init( out.rdbuf() ); }

Page 40: What is the type of std::cout? Where does a stream go?

Using the Stream

typedef logostream< char > clogostream;

typedef logostream< wchar_t > wlogostream;

...

int main()

{

clogostream;out( std::cout );

out << "31 hexadecimal: " << std::hex << 31 << std::endl;

return 0;

}

Page 41: What is the type of std::cout? Where does a stream go?

Using the StreamOutput

Fri Apr 20 16:00:00 2005 - 31 hexadecimal: 1f

Page 42: What is the type of std::cout? Where does a stream go?

Custom IOStream Applications

● Click here to get audience to write slide

Page 43: What is the type of std::cout? Where does a stream go?

Transcoding

● ISO8859-2 to UTF-8● ASCII to UTF-16● Windows-1252 to ISO8859-1● Newline conversions● HTML entity escaping

Page 44: What is the type of std::cout? Where does a stream go?

Compression/Encryption

● Base64● zlib ● LZH● Error correction

Page 45: What is the type of std::cout? Where does a stream go?

Sources and Sinks

● sockets● a database● a CVS repository● memory-mapped file

● a vector● file descriptor● an array● a window

Page 46: What is the type of std::cout? Where does a stream go?

Wacky Stuff

● Occam2 style channels

Page 47: What is the type of std::cout? Where does a stream go?

Code you can use

● Boost::IOStreams http://boost.org/ http://home.comcast.net/~jturkanis/iostreams/

● Aeryn http://www.paulgrenyer.dyndns.org/aeryn/

● Arabica http://www.jezuk.co.uk/arabica

● Standard C++ IOStreams and Locales by Langer & Kreft

Page 48: What is the type of std::cout? Where does a stream go?

You. Again.

● a custom IOStreamIn the last three months,might you use ...