seekable_device.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>SeekableDevice</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">SeekableDevice</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <H2>Definition</H2>
  14. <P>
  15. A SeekableDevice is a <A HREF="device.html">Device</A> whose <A HREF="../guide/modes.html">mode</A> refines <A HREF="../guide/modes.html#seekable">seekable</A>.
  16. </P>
  17. <H2>Description</H2>
  18. <P>
  19. A SeekableDevice provides read- write- and random-access to a sequence of characters of a given type, with a single repositionable read/write head. A SeekableDevice may expose this sequence in two ways:
  20. <OL>
  21. <LI STYLE="list-style-type:lower-roman">
  22. by defining member functions <CODE>read</CODE>, <CODE>write</CODE> and <CODE>seek</CODE>; or
  23. </LI>
  24. <LI STYLE="list-style-type:lower-roman">
  25. by defining member functions <CODE>input_sequence</CODE> and <CODE>output_sequence</CODE>, returning pairs of pointers delimiting the sequences in its entirety. The return values of these two functions must be the same.<A CLASS="footnote_ref" NAME="note_1_ref" HREF="#note_1"><SUP>[1]</SUP></A>
  26. </LI>
  27. </OL>
  28. </P>
  29. <P>A SeekableDevice has mode <A HREF="../guide/modes.html#seekable"><CODE>seekable</CODE></A>.</P>
  30. <H2>Note</H2>
  31. <P>To be usable with streams and stream buffers, SeekableDevices must model <A HREF="blocking.html">Blocking</A>.
  32. <H2>Example</H2>
  33. <P>A model of SeekableDevice can be defined as follows:</P>
  34. <PRE CLASS="broken_ie"><SPAN CLASS="keyword">struct</SPAN> SeekableDevice {
  35. <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type;
  36. <SPAN CLASS="keyword">typedef</SPAN> seekable_device_tag category;
  37. std::streamsize read(<SPAN CLASS="keyword">char</SPAN>* s, std::streamsize n)
  38. {
  39. <SPAN CLASS="comment">// Read up to n characters from the input
  40. // sequence into the buffer s, returning
  41. // the number of characters read, or -1
  42. // to indicate end-of-sequence.</SPAN>
  43. }
  44. <SPAN CLASS="keyword">void</SPAN> write(<SPAN CLASS="keyword">const</SPAN> <SPAN CLASS="keyword">char</SPAN>* s, std::streamsize n)
  45. {
  46. <SPAN CLASS="comment">// Write up to n characters from the buffer
  47. // s to the output sequence, returning the
  48. // number of characters written</SPAN>
  49. }
  50. std::streampos seek(stream_offset off, std::ios_base::seekdir way)
  51. {
  52. <SPAN CLASS="comment">// Advances the read/write head by off characters,
  53. // returning the new position, where the offset is
  54. // calculated from:
  55. // - the start of the sequence if way == ios_base::beg
  56. // - the current position if way == ios_base::cur
  57. // - the end of the sequence if way == ios_base::end</SPAN>
  58. }
  59. };</PRE>
  60. <P>
  61. Here <CODE>seekable_device_tag</CODE> is a <A HREF="../guide/traits.html#category_tags">category tag</A> identifying the containing type as a model of SeekableDevice. When defining a new SeekableDevice, it suffices to use the tag <CODE>seekable_device_tag</CODE>. One can also derive from the helper classes <A HREF="../classes/device.html"><CODE>device&lt;seekable&gt;</CODE></A> or <A HREF="../classes/device.html#synopsis"><CODE>wdevice&lt;seekable&gt;</CODE></A>.
  62. </P>
  63. <H2>Refinement of</H2>
  64. <P><A HREF="source.html">Source</A>, <A HREF="sink.html">Sink</A>.</P>
  65. <H2>Associated Types</H2>
  66. <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P>
  67. <TABLE CELLPADDING="5" BORDER="1">
  68. <TR><TD>Category</TD><TD>A type convertible to <A HREF="../guide/traits.html#category_tags"><CODE>device_tag</CODE></A> and to <A HREF="../guide/modes.html#mode_tags"><CODE>seekable</CODE></A></TD></TR>
  69. </TABLE>
  70. <H2>Notation</H2>
  71. <TABLE CELLPADDING="2">
  72. <TR><TD><CODE>D</CODE></TD><TD>- A type which is a model of SeekableDevice</TD></TR>
  73. <TR><TD><CODE>Ch</CODE></TD><TD>- The character type of <CODE>D</CODE></TD></TR>
  74. <TR><TD><CODE>dev</CODE></TD><TD>- Object of type <CODE>D</CODE></TD></TR>
  75. <TR><TD><CODE>s1</CODE></TD><TD>- Object of type <CODE>Ch*</CODE></SPAN></TD></TR>
  76. <TR><TD><CODE>s2</CODE></TD><TD>- Object of type <CODE>const Ch*</CODE></SPAN></TD></TR>
  77. <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR>
  78. <TR><TD><CODE>off</CODE></TD><TD>- Object of type <A HREF="../functions/positioning.html#synopsis"><CODE>stream_offset</CODE></A></TD></TR>
  79. <TR><TD><CODE>way</CODE></TD><TD>- Object of type <CODE>std::ios_base::seekdir</CODE></TD></TR>
  80. <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR>
  81. </TABLE>
  82. <H2>Valid Expressions / Semantics</H2>
  83. <P>Same as <A HREF="device.html#types">Device</A>, with the following additional requirements:</P>
  84. <TABLE CELLPADDING="5" BORDER="1">
  85. <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR>
  86. <TR>
  87. <TD><PRE CLASS="plain_code"><CODE><A HREF="../functions/read.html">io::read</A>(dev, s1, n)</CODE></PRE></TD>
  88. <TD><CODE>std::streamsize</CODE></TD>
  89. <TD ROWSPAN="3">Not convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD>
  90. <TD>
  91. Reads up to <CODE>n</CODE> characters from the sequence controlled by <CODE>dev</CODE> into <CODE>s1</CODE>, returning the number of characters read, or <CODE>-1</CODE> to indicate end-of-sequence
  92. </TD>
  93. </TR>
  94. <TR>
  95. <TD><PRE CLASS="plain_code"><CODE><A HREF="../functions/write.html">io::write</A>(dev, s2, n)</CODE></PRE></TD>
  96. <TD><CODE>std::streamsize</CODE></TD>
  97. <TD>
  98. Writes up to <CODE>n</CODE> characters from the sequence beginning at <CODE>s2</CODE> to the sequence controlled by <CODE>dev</CODE>, returning the number of characters written
  99. </TD>
  100. </TR>
  101. <TR>
  102. <TD><PRE CLASS="plain_code"><CODE><A HREF="../functions/seek.html">io::seek</A>(dev, off, way)</CODE></PRE></TD>
  103. <TD><CODE>std::streampos</CODE></TD>
  104. <TD>Advances the read/write head by <CODE>off</CODE> characters, returning the new position, where the offset is calculated from:
  105. <UL STYLE="margin:0,0,0,auto">
  106. <LI STYLE="list-style-type:disc;list-style-image:none">the start of the sequence if <CODE>way</CODE> is <CODE>ios_base::beg</CODE>
  107. <LI STYLE="list-style-type:disc;list-style-image:none">the current position if <CODE>way</CODE> is <CODE>ios_base::cur</CODE>
  108. <LI STYLE="list-style-type:disc;list-style-image:none">the end of the sequence if <CODE>way</CODE> is <CODE>ios_base::end</CODE>
  109. </UL>
  110. </TD>
  111. </TR>
  112. <TR>
  113. <TD><PRE CLASS="plain_code"><CODE>dev.input_sequence()</CODE></PRE></TD>
  114. <TD><PRE CLASS="plain_code"><CODE>std::pair&lt;Ch*,Ch*&gt;</CODE></PRE></TD>
  115. <TD ROWSPAN="2">Convertible to <A HREF="direct.html"><CODE>direct_tag</CODE></A></TD>
  116. <TD>Returns a pair of pointers delimiting the sequence controlled by <CODE>dev</CODE></TD>
  117. </TR>
  118. <TR>
  119. <TD><PRE CLASS="plain_code"><CODE>dev.output_sequence()</CODE></PRE></TD>
  120. <TD><PRE CLASS="plain_code"><CODE>std::pair&lt;Ch*,Ch*&gt;</CODE></PRE></TD>
  121. <TD>Returns a pair of pointers delimiting the sequence controlled by <CODE>dev</CODE></TD>
  122. </TR>
  123. </TABLE>
  124. <H2>Exceptions</H2>
  125. <P>
  126. Errors which occur during the execution of member functions <CODE>read</CODE>, <CODE>write</CODE>, <CODE>seek</CODE>, <CODE>input_sequence</CODE> or <CODE>output_sequence</CODE> are indicated by throwing exceptions. Reaching the end of the sequence is not an error, but attempting to write past the end of the sequence is.
  127. </P>
  128. <P>
  129. After an exception is thrown, a SeekableDevice must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour.
  130. </P>
  131. <H2>Models</H2>
  132. <UL>
  133. <LI><A HREF="../classes/array.html#array"><CODE>array</CODE></A>, <A HREF="../classes/file.html#file"><CODE>file</CODE></A>, <A HREF="../classes/file_descriptor.html#file_descriptor"><CODE>file_descriptor</CODE></A>, <A HREF="../classes/mapped_file.html#mapped_file"><CODE>mapped_file</CODE></A>.
  134. </UL>
  135. <!-- Begin Footnotes -->
  136. <P>
  137. <A CLASS="footnote_ref" NAME="note_1" HREF="#note_1_ref"><SUP>[1]</SUP></A>A more elegant specification would require a single member function <CODE>sequence</CODE>; the present version was selected to simplify the implementation of <A HREF="../guide/generic_streams.html#stream_buffer"><CODE>stream_buffer</CODE></A>.
  138. </P>
  139. <!-- End Footnotes -->
  140. <!-- Begin Footer -->
  141. <HR>
  142. <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>
  143. <P CLASS="copyright">
  144. 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>)
  145. </P>
  146. <!-- End Footer -->
  147. </BODY>