write.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Function Template write</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">Function Template <CODE>write</CODE></H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <DL class="page-index">
  14. <DT><A href="#overview">Overview</A></DT>
  15. <DT><A href="#example">Example</A></DT>
  16. <DT><A href="#headers">Headers</A></DT>
  17. <DT><A href="#reference">Reference</A></DT>
  18. </DL>
  19. <A NAME="overview"></A>
  20. <H2>Overview</H2>
  21. <P>
  22. The two overloads of the function template <CODE>write</CODE> provide a uniform interface for writing a sequence of characters to a <A HREF="../concepts/source.html">Sink</A> or <A HREF="../concepts/output_filter.html">OutputFilter</A>.
  23. <UL>
  24. <LI>The first overload can be used directly in the definitions of new Filter types (<I>see</I> <A HREF="#example">Example</A>), and figures in the specification of the <A HREF="../guide/concepts.html#device_concepts">Device</A> concepts.
  25. <LI>The second overload is primarily for internal use by the library.
  26. </UL>
  27. </P>
  28. <A NAME="example"></A>
  29. <H2>Example</H2>
  30. <P>
  31. The following code illustrates the use of the function <CODE>write</CODE> in the definition of an <A HREF="../concepts/output_filter.html">OutputFilter</A> which reverses its controlled sequence.
  32. </P>
  33. <PRE CLASS="broken_ie"> <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;algorithm&gt;</SPAN> <SPAN CLASS="comment">// reverse</SPAN>
  34. <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;vector&gt;</SPAN>
  35. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// output_filter</SPAN>
  36. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// write</SPAN>
  37. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
  38. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> boost::io;
  39. <SPAN CLASS="keyword">struct</SPAN> reversing_filter : <SPAN CLASS="keyword">public</SPAN> multichar_output_filter {
  40. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  41. std::streamsize write(Sink&amp; snk, <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">char</SPAN>* s, streamsize n)
  42. {
  43. data.insert(data.end(), s, s + n);
  44. <SPAN CLASS="keyword">return</SPAN> n;
  45. }
  46. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  47. <SPAN CLASS="keyword">void</SPAN> close(Sink&amp; snk)
  48. {
  49. std::reverse(data.begin(), data.end());
  50. boost::iostreams::write(&amp;data[0], (streamsize) data.size());
  51. data.clear();
  52. }
  53. std::vector&lt;<SPAN CLASS="keyword">char</SPAN>&gt; data;
  54. };</PRE>
  55. <A NAME="headers"></A>
  56. <H2>Headers</H2>
  57. <DL>
  58. <DT><A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><CODE>&lt;boost/iostreams/operations.hpp&gt;</CODE></A></DT>
  59. <DT><A CLASS="header" HREF="../../../../boost/iostreams/write.hpp"><CODE>&lt;boost/iostreams/write.hpp&gt;</CODE></A></DT>
  60. </DL>
  61. <A NAME="reference"></A>
  62. <H2>Reference</H2>
  63. <A NAME="description"></A>
  64. <H4>Description</H4>
  65. <P>Attempts to write a sequence of characters to a given instance of the template parameter <CODE>T</CODE>, returning the number of characters written.</P>
  66. <A NAME="synopsis"></A>
  67. <H4>Synopsis</H4>
  68. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams {
  69. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>&gt;
  70. std::streamsize
  71. <A CLASS="documented" HREF="#write_device">write</A>( T&amp; <A CLASS="documented" HREF="#function_params">t</A>,
  72. <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="../guide/traits.html#char_type_of_ref">char_type_of</A>&lt;T&gt;::type* <A CLASS="documented" HREF="#function_params">s</A>,
  73. std::streamsize <A CLASS="documented" HREF="#function_params">n</A> );
  74. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>, <SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">Sink</A>&gt;
  75. std::streamsize
  76. <A CLASS="documented" HREF="#write_filter">write</A>( T&amp; <A CLASS="documented" HREF="#function_params">t</A>,
  77. Sink&amp; <A CLASS="documented" HREF="#function_params">snk</A>,
  78. <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="../guide/traits.html#char_type_of_ref">char_type_of</A>&lt;T&gt;::type* <A CLASS="documented" HREF="#function_params">s</A>,
  79. std::streamsize <A CLASS="documented" HREF="#function_params">n</A> );
  80. } } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE>
  81. <A NAME="template_params"></A>
  82. <H4>Template Parameters</H4>
  83. <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2>
  84. <TR>
  85. <TR>
  86. <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  87. <TD>For the first overload, a model of <A HREF="../concepts/sink.html">Sink</A>. For the second overload, a model of <A HREF="../concepts/output_filter.html">OutputFilter</A>.
  88. </TR>
  89. <TR>
  90. <TD VALIGN="top"><I>Sink</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  91. <TD>A model of <A HREF="../concepts/sink.html">Sink</A> with the same <A HREF="../guide/traits.html#char_type">character type</A> as <CODE>T</CODE> whose <A HREF="../guide/modes.html">mode</A> refines that of <CODE>T</CODE>.
  92. </TR>
  93. </TABLE>
  94. <A NAME="function_params"></A>
  95. <H4>Function Parameters</H4>
  96. <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2>
  97. <TR>
  98. <TR>
  99. <TD VALIGN="top"><I>t</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  100. <TD>An instance of T</CODE></TD>
  101. </TR>
  102. <TR>
  103. <TD VALIGN="top"><I>s</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  104. <TD>A buffer containing characters to write</TD>
  105. </TR>
  106. <TR>
  107. <TD VALIGN="top"><I>n</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  108. <TD>The number of characters to write</TD>
  109. </TR>
  110. <TR>
  111. <TD VALIGN="top"><I>snk</I></TD><TD WIDTH="2em" VALIGN="top">-</TD>
  112. <TD>An instance of <CODE>Sink</CODE>.
  113. </TR>
  114. </TABLE>
  115. <A NAME="write_device"></A>
  116. <H4>Semantics &#8212; Device Types</H4>
  117. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> T&gt;
  118. std::streamsize
  119. write( T&amp; t,
  120. <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">typename</SPAN> char_type_of&lt;T&gt;::type* s,
  121. std::streamsize n );</PRE>
  122. <P>The semantics of <CODE>write</CODE> depends on the <A HREF="../guide/traits.html#category">category</A> of <CODE>T</CODE> as follows:</P>
  123. <TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4>
  124. <TR><TH><CODE>category_of&lt;T&gt;::type</CODE></TH><TH>semantics</TH></TR>
  125. <TR>
  126. <TD VALIGN="top">convertible to <CODE>ostream_tag</CODE></TD>
  127. <TD>returns <CODE>t.rdbuf()->sputn(s, n)</CODE></TD>
  128. </TR>
  129. <TR>
  130. <TD VALIGN="top">convertible to <CODE>streambuf_tag</CODE> but not to <CODE>ostream_tag</CODE></TD>
  131. <TD>returns <CODE>t.sputn(s, n)</CODE></TD>
  132. </TR>
  133. <TR>
  134. <TD VALIGN="top">otherwise</TD>
  135. <TD>returns <CODE>t.write(s, n)</CODE></TD>
  136. </TR>
  137. </TABLE>
  138. <A NAME="write_filter"></A>
  139. <H4>Semantics &#8212; Filter Types</H4>
  140. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> T, <SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  141. std::streamsize
  142. write( T&amp; t,
  143. Sink&amp; snk,
  144. <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">typename</SPAN> char_type_of&lt;T&gt;::type* s,
  145. std::streamsize n );</PRE>
  146. <P>The semantics of <CODE>write</CODE> depends on the <A HREF="../guide/traits.html#category">category</A> of <CODE>T</CODE> as follows:</P>
  147. <TABLE STYLE="margin:0,0,2em,2em" BORDER=1 CELLPADDING=4>
  148. <TR><TH><CODE>category_of&lt;T&gt;::type</CODE></TH><TH>semantics</TH></TR>
  149. <TR>
  150. <TD VALIGN="top">convertible to <CODE>multichar_tag</CODE></TD>
  151. <TD>returns <CODE>t.write(snk, s, n)</CODE></TD>
  152. </TR>
  153. <TR>
  154. <TD VALIGN="top"><I>otherwise</I></TD>
  155. <TD>Attempts to write <CODE>n</CODE> characters from <CODE>s</CODE> by invoking <CODE>t.put(snk, s[m])</CODE> for each value <CODE>m</CODE> in the interval <CODE>[0, n)</CODE>, halting if <CODE>put</CODE> returns <CODE>false</CODE>. Returns the number of characters written.</TD>
  156. </TR>
  157. </TABLE>
  158. <!-- Begin Footer -->
  159. <HR>
  160. <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>
  161. <P CLASS="copyright">
  162. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
  163. </P>
  164. <!-- End Footer -->
  165. </BODY>