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 10 <!-- Begin Banner --> 11 12 <H1 CLASS="title">Function Template <CODE>write</CODE></H1> 13 <HR CLASS="banner"> 14 15 <!-- End Banner --> 16 17 <DL class="page-index"> 18 <DT><A href="#overview">Overview</A></DT> 19 <DT><A href="#example">Example</A></DT> 20 <DT><A href="#headers">Headers</A></DT> 21 <DT><A href="#reference">Reference</A></DT> 22 </DL> 23 24 <A NAME="overview"></A> 25 <H2>Overview</H2> 26 27 <P> 28 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>. 29 <UL> 30 <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. 31 <LI>The second overload is primarily for internal use by the library. 32 </UL> 33 </P> 34 35 <A NAME="example"></A> 36 <H2>Example</H2> 37 38 <P> 39 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. 40 </P> 41 42 <PRE CLASS="broken_ie"> <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal"><algorithm></SPAN> <SPAN CLASS="comment">// reverse</SPAN> 43 <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal"><vector></SPAN> 44 <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal"><boost/iostreams/concepts.hpp></SPAN></A> <SPAN CLASS="comment">// output_filter</SPAN> 45 <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal"><boost/iostreams/operations.hpp></SPAN></A> <SPAN CLASS="comment">// write</SPAN> 46 47 <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std; 48 <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> boost::io; 49 50 <SPAN CLASS="keyword">struct</SPAN> reversing_filter : <SPAN CLASS="keyword">public</SPAN> multichar_output_filter { 51 <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> Sink> 52 std::streamsize write(Sink& snk, <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">char</SPAN>* s, streamsize n) 53 { 54 data.insert(data.end(), s, s + n); 55 <SPAN CLASS="keyword">return</SPAN> n; 56 } 57 <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> Sink> 58 <SPAN CLASS="keyword">void</SPAN> close(Sink& snk) 59 { 60 std::reverse(data.begin(), data.end()); 61 boost::iostreams::write(&data[0], (streamsize) data.size()); 62 data.clear(); 63 } 64 std::vector<<SPAN CLASS="keyword">char</SPAN>> data; 65 };</PRE> 66 67 <A NAME="headers"></A> 68 <H2>Headers</H2> 69 70 <DL> 71 <DT><A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><CODE><boost/iostreams/operations.hpp></CODE></A></DT> 72 <DT><A CLASS="header" HREF="../../../../boost/iostreams/write.hpp"><CODE><boost/iostreams/write.hpp></CODE></A></DT> 73 </DL> 74 75 <A NAME="reference"></A> 76 <H2>Reference</H2> 77 78 <A NAME="description"></A> 79 <H4>Description</H4> 80 81 <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> 82 83 <A NAME="synopsis"></A> 84 <H4>Synopsis</H4> 85 86 <PRE CLASS="broken_ie"><SPAN CLASS="keyword">namespace</SPAN> boost { <SPAN CLASS="keyword">namespace</SPAN> iostreams { 87 88 <SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> <A CLASS="documented" HREF="#template_params">T</A>> 89 std::streamsize 90 <A CLASS="documented" HREF="#write_device">write</A>( T& <A CLASS="documented" HREF="#function_params">t</A>, 91 <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><T>::type* <A CLASS="documented" HREF="#function_params">s</A>, 92 std::streamsize <A CLASS="documented" HREF="#function_params">n</A> ); 93 94 <SPAN CLASS="keyword">template</SPAN><<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>> 95 std::streamsize 96 <A CLASS="documented" HREF="#write_filter">write</A>( T& <A CLASS="documented" HREF="#function_params">t</A>, 97 Sink& <A CLASS="documented" HREF="#function_params">snk</A>, 98 <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><T>::type* <A CLASS="documented" HREF="#function_params">s</A>, 99 std::streamsize <A CLASS="documented" HREF="#function_params">n</A> ); 100 101 } } <SPAN CLASS="comment">// End namespace boost::io</SPAN></PRE> 102 103 <A NAME="template_params"></A> 104 <H4>Template Parameters</H4> 105 106 <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2> 107 <TR> 108 <TR> 109 <TD VALIGN="top"><I>T</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 110 <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>. 111 </TR> 112 <TR> 113 <TD VALIGN="top"><I>Sink</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 114 <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>. 115 </TR> 116 </TABLE> 117 118 <A NAME="function_params"></A> 119 <H4>Function Parameters</H4> 120 121 <TABLE STYLE="margin-left:2em" BORDER=0 CELLPADDING=2> 122 <TR> 123 <TR> 124 <TD VALIGN="top"><I>t</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 125 <TD>An instance of T</CODE></TD> 126 </TR> 127 <TR> 128 <TD VALIGN="top"><I>s</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 129 <TD>A buffer containing characters to write</TD> 130 </TR> 131 <TR> 132 <TD VALIGN="top"><I>n</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 133 <TD>The number of characters to write</TD> 134 </TR> 135 <TR> 136 <TD VALIGN="top"><I>snk</I></TD><TD WIDTH="2em" VALIGN="top">-</TD> 137 <TD>An instance of <CODE>Sink</CODE>. 138 </TR> 139 </TABLE> 140 141 <A NAME="write_device"></A> 142 <H4>Semantics — Device Types</H4> 143 144 <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> T> 145 std::streamsize 146 write( T& t, 147 <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">typename</SPAN> char_type_of<T>::type* s, 148 std::streamsize n );</PRE> 149 150 <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> 151 152 <TABLE STYLE="margin-left:2em" BORDER=1 CELLPADDING=4> 153 <TR><TH><CODE>category_of<T>::type</CODE></TH><TH>semantics</TH></TR> 154 <TR> 155 <TD VALIGN="top">convertible to <CODE>ostream_tag</CODE></TD> 156 <TD>returns <CODE>t.rdbuf()->sputn(s, n)</CODE></TD> 157 </TR> 158 <TR> 159 <TD VALIGN="top">convertible to <CODE>streambuf_tag</CODE> but not to <CODE>ostream_tag</CODE></TD> 160 <TD>returns <CODE>t.sputn(s, n)</CODE></TD> 161 </TR> 162 <TR> 163 <TD VALIGN="top">otherwise</TD> 164 <TD>returns <CODE>t.write(s, n)</CODE></TD> 165 </TR> 166 </TABLE> 167 168 <A NAME="write_filter"></A> 169 <H4>Semantics — Filter Types</H4> 170 171 <PRE CLASS="broken_ie"><SPAN CLASS="keyword">template</SPAN><<SPAN CLASS="keyword">typename</SPAN> T, <SPAN CLASS="keyword">typename</SPAN> Sink> 172 std::streamsize 173 write( T& t, 174 Sink& snk, 175 <SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">typename</SPAN> char_type_of<T>::type* s, 176 std::streamsize n );</PRE> 177 178 <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> 179 180 <TABLE STYLE="margin:0,0,2em,2em" BORDER=1 CELLPADDING=4> 181 <TR><TH><CODE>category_of<T>::type</CODE></TH><TH>semantics</TH></TR> 182 <TR> 183 <TD VALIGN="top">convertible to <CODE>multichar_tag</CODE></TD> 184 <TD>returns <CODE>t.write(snk, s, n)</CODE></TD> 185 </TR> 186 <TR> 187 <TD VALIGN="top"><I>otherwise</I></TD> 188 <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> 189 </TR> 190 </TABLE> 191 192 <!-- Begin Footer --> 193 194 <HR> 195 196 <P CLASS="copyright">© Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>© Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P> 197 <P CLASS="copyright"> 198 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>) 199 </P> 200 201 <!-- End Footer --> 202 203 </BODY>