mirror of
https://github.com/boostorg/iostreams.git
synced 2026-02-22 15:32:20 +00:00
72 lines
4.9 KiB
HTML
Executable File
72 lines
4.9 KiB
HTML
Executable File
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>Tutorial</TITLE>
|
|
<LINK REL="stylesheet" HREF="../../../../boost.css">
|
|
<LINK REL="stylesheet" HREF="../theme/iostreams.css">
|
|
</HEAD>
|
|
<BODY>
|
|
|
|
<!-- Begin Banner -->
|
|
|
|
<H1 CLASS="title">Tutorial</H1>
|
|
<HR CLASS="banner">
|
|
|
|
<!-- End Banner -->
|
|
|
|
<A NAME="device_overview"></A>
|
|
<H3>1.1. Overview: Devices, <CODE>streambuf_facade</CODE> and <CODE>stream_facade</CODE></H3>
|
|
|
|
<P>Writing a new stream or stream buffer class using the Boost Iostreams library is easy: we simply write a class modeling the <A HREF="../concepts/device.html">Device</A> concept, then use that class as the template argument to <A HREF="../guide/generic_streams.html#stream_facadev><CODE>stream_facade</CODE></A> or <A HREF="../guide/generic_streams.html#streambuf_facade"><CODE>streambuf_facade</CODE></A>:
|
|
|
|
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/stream_facade.hpp"><SPAN CLASS='literal'><boost/iostreams/stream_facade.hpp></SPAN></A>
|
|
<SPAN CLASS='preprocessor'>#include</SPAN> <A HREF="../../../../boost/iostreams/streambuf_facade.hpp"><SPAN CLASS='literal'><boost/iostreams/streambuf_facade.hpp></SPAN></A>
|
|
|
|
<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
|
|
|
|
<SPAN CLASS='keyword'>class</SPAN> my_device { <SPAN CLASS='comment'>/* */</SPAN> };
|
|
|
|
<SPAN CLASS='keyword'>typedef</SPAN> io::stream_facade<my_device> my_stream;
|
|
<SPAN CLASS='keyword'>typedef</SPAN> io::streambuf_facade<my_device> my_streambuf;</PRE>
|
|
|
|
<P>Here <CODE>io::stream_facade<my_device></CODE> is a derived class of <CODE>std::basic_streambuf</CODE>, and <CODE>io::stream_facade<my_device></CODE> is a derived class of <CODE>std::basic_istream</CODE>, <CODE>std::basic_ostream</CODE> or <CODE>std::basic_iostream</CODE> depending on the <A HREF="../guide/modes.html">mode</A> of my_device, <I>i.e.</I>, depending on which of the fundamental i/o operations <A HREF="../functions/read.html"><CODE>read</CODE></A>, <A HREF="../functions/write.html"><CODE>write</CODE></A> and <A HREF="../functions/seek.html"><CODE>seek</CODE></A> it supports.
|
|
|
|
<P>The template <CODE>io::stream_facade</CODE> is provided as a convenience. It's always possibly to avoid <CODE>io::stream_facade<my_device></CODE> and simply use <CODE>io::stream_facade</CODE> together with one of the standard library stream templates. <I>E.g.</I>,
|
|
|
|
<PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'><ostream></SPAN>
|
|
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/device/file.hpp"><SPAN CLASS='literal'><boost/iostreams/device/file.hpp></SPAN></A>
|
|
<SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/stream_facade.hpp"><SPAN CLASS='literal'><boost/iostreams/stream_facade.hpp></SPAN></A>
|
|
|
|
<SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
|
|
|
|
<SPAN CLASS='keyword'>int</SPAN> main()
|
|
{
|
|
io::stream_facade<file_sink> buf(<SPAN CLASS='literal'>"log.txt"</SPAN>);
|
|
std::ostream out(&buf);
|
|
<SPAN CLASS='comment'>// out writes to log.txt</SPAN>
|
|
}
|
|
<SPAN CLASS='keyword'>typedef</SPAN> io::stream_facade<my_device> my_stream;
|
|
<SPAN CLASS='keyword'>typedef</SPAN> io::streambuf_facade<my_device> my_streambuf;</PRE>
|
|
|
|
<P>In this example, the <CODE>ostream</CODE> <CODE>out</CODE> uses the stream buffer <CODE>buf</CODE> as its underlying data sink, so that data written to <CODE>out</CODE> goes to the file <I>log.txt</I>. The same effect could be achieved by default constructing <CODE>out</CODE> and telling it to use stream buffer <CODE>buf</CODE> by invoking <CODE>out.rdbuf(&buf)</CODE>.
|
|
|
|
<P>Another way to define a new stream or stream buffer class using the Boost Iostreams library is to derive from <A HREF="../classes/filtering_stream.html"><CODE>filtering_stream</CODE></A> or <A HREF="../classes/filtering_streambuf.html"><CODE>filtering_streambuf</CODE></A>.
|
|
<P>The next three items will demonstrate how to write Devices for accessing STL-compatible containers. The source code for the examples can be found in the header <A HREF="../../example/container_device.hpp"><<CODE>libs/iostreams/example/container_device.hpp</CODE>></A></P>
|
|
|
|
<!-- Begin Footer -->
|
|
|
|
<HR>
|
|
|
|
<P CLASS="copyright">Revised
|
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
|
20 May, 2004
|
|
<!--webbot bot="Timestamp" endspan i-checksum="38504" -->
|
|
</P>
|
|
|
|
<P CLASS="copyright">© Copyright Jonathan Turkanis, 2004</P>
|
|
<P CLASS="copyright">
|
|
Use, modification, and distribution are subject to the Boost Software License, Version 2.0. (See accompanying file <A HREF="../../../../LICENSE_2_0.txt">LICENSE_2_0.txt</A> or copy at <A HREF="../http://www.boost.org/LICENSE_2_0.txt">http://www.boost.org/LICENSE_2_0.txt</A>)
|
|
</P>
|
|
<!-- End Footer -->
|
|
|
|
</BODY> |