writing_devices.html 5.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Tutorial</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../../boost.css">
  6. <LINK REL="stylesheet" HREF="../theme/iostreams.css">
  7. </HEAD>
  8. <BODY>
  9. <!-- Begin Banner -->
  10. <H1 CLASS="title">Tutorial</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <!-- Begin Nav -->
  14. <DIV CLASS='nav'>
  15. <A><IMG WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev_disabled.png'></A>
  16. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  17. <A HREF='container_source.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  18. </DIV>
  19. <!-- End Nav -->
  20. <A NAME="device_overview"></A>
  21. <H2 CLEAR='right'>2.1.1. Overview: Devices, <CODE>stream_buffer</CODE> and <CODE>stream</CODE></H2>
  22. <P>Writing a new stream or stream buffer class using the Boost Iostreams library is easy: you 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"><CODE>stream</CODE></A> or <A HREF="../guide/generic_streams.html#stream_buffer"><CODE>stream_buffer</CODE></A>:
  23. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/stream.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/stream.hpp&gt;</SPAN></A>
  24. <SPAN CLASS='preprocessor'>#include</SPAN> <A HREF="../../../../boost/iostreams/stream_buffer.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/stream_buffer.hpp&gt;</SPAN></A>
  25. <SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
  26. <SPAN CLASS='keyword'>class</SPAN> my_device { <SPAN CLASS='comment'>/* */</SPAN> };
  27. <SPAN CLASS='keyword'>typedef</SPAN> io::stream&lt;my_device&gt; my_stream;
  28. <SPAN CLASS='keyword'>typedef</SPAN> io::stream_buffer&lt;my_device&gt; my_streambuf;</PRE>
  29. <P>Here <CODE>io::stream_buffer&lt;my_device&gt;</CODE> is a derived class of <CODE>std::basic_streambuf</CODE>, and <CODE>io::stream&lt;my_device&gt;</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.
  30. <P>The template <CODE>io::stream</CODE> is provided as a convenience. It's always possible to avoid <CODE>io::stream</CODE> and simply use <CODE>io::stream_buffer</CODE> together with one of the standard library stream templates. <I>E.g.</I>,
  31. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS='literal'>&lt;ostream&gt;</SPAN>
  32. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/device/file.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/device/file.hpp&gt;</SPAN></A>
  33. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/stream.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/stream.hpp&gt;</SPAN></A>
  34. <SPAN CLASS='keyword'>namespace</SPAN> io = boost::iostreams;
  35. <SPAN CLASS='keyword'>int</SPAN> main()
  36. {
  37. io::stream_buffer&lt;io::file_sink&gt; buf(<SPAN CLASS='literal'>"log.txt"</SPAN>);
  38. std::ostream out(&amp;buf);
  39. <SPAN CLASS='comment'>// out writes to log.txt</SPAN>
  40. }</PRE>
  41. <P>In this example, the <CODE>ostream</CODE> <CODE>out</CODE> uses the <CODE>stream_buffer</CODE> <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(&amp;buf)</CODE>.
  42. <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>.
  43. <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">&lt;<CODE>libs/iostreams/example/container_device.hpp</CODE>&gt;</A></P>
  44. <!-- Begin Nav -->
  45. <DIV CLASS='nav'>
  46. <IMG WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev_disabled.png'>
  47. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  48. <A HREF='container_source.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  49. </DIV>
  50. <!-- End Nav -->
  51. <!-- Begin Footer -->
  52. <HR>
  53. <P CLASS="copyright">&copy; Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>&copy; Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P>
  54. <P CLASS="copyright">
  55. Use, modification, and distribution are subject to the Boost Software License, Version 2.0. (See accompanying file <A HREF="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
  56. </P>
  57. <!-- End Footer -->
  58. </BODY>