//Purpose: // Demonstrate that ostream's streambuf can be replaced // with boost::iostreams::stream_buffer. //Reference: // http://www.boost.org/doc/libs/1_37_0/libs/iostreams/doc/tutorial/writing_devices.html // http://www.boost.org/doc/libs/1_37_0/libs/iostreams/doc/classes/filtering_streambuf.html // #include #include typedef std::ostream::char_type std_ostrm_char_type; typedef std::basic_ostream std_ostrm; typedef std::basic_streambuf std_strmbuf; namespace io = boost::iostreams; struct toupper_output_filter : public io::output_filter //Reference: // http://www.boost.org/doc/libs/1_37_0/libs/iostreams/doc/concepts/output_filter.html // { template bool put(Sink& snk, char c) { return io::put(snk, toupper((unsigned char) c)); } }; struct push_filt_strmbuf : public io::filtering_ostreambuf { push_filt_strmbuf(std_strmbuf& a_buf) { this->push(my_filter); this->push(a_buf); } toupper_output_filter my_filter ; }; int main(void) { std_ostrm& sout=std::cout; sout<<"Hello World!\n"; std_strmbuf* oldbuf=sout.rdbuf(); push_filt_strmbuf* ppush_sbuf=new push_filt_strmbuf(*oldbuf); sout<<"is_complete="<is_complete()<<"\n"; sout.rdbuf(ppush_sbuf); sout<<"2nd Hello!\n"; sout.rdbuf(oldbuf); sout<<"3ird Hello!\n"; return 0; }