ios_state.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
  2. <html>
  3. <head>
  4. <title>I/O Stream-State Saver Library</title>
  5. </head>
  6. <body text="black" bgcolor="white" link="blue" vlink="purple" alink="red">
  7. <h1><img src="../../../boost.png" alt="boost.png (6897 bytes)"
  8. align="middle" width="277" height="86">Header &lt;<cite><a
  9. href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite>
  10. &gt;</h1>
  11. <p>The header <cite><a
  12. href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite>
  13. covers saving the stream state of objects in the C++ IOStreams
  14. system.</p>
  15. <h2><a name="contents">Contents</a></h2>
  16. <ol>
  17. <li><a href="#contents">Contents</a></li>
  18. <li><a href="#rationale">Rationale</a></li>
  19. <li><a href="#header">Header Synopsis</a></li>
  20. <li><a href="#base_savers">Savers for Basic Standard Attributes</a></li>
  21. <li><a href="#adv_savers">Savers for Advanced Standard Attributes</a></li>
  22. <li><a href="#user_savers">Savers for User-Defined Attributes</a></li>
  23. <li><a href="#combo_savers">Savers for Combined Attributes</a></li>
  24. <li><a href="#example">Example</a></li>
  25. <li><a href="#refer">References</a></li>
  26. <li><a href="#credits">Credits</a>
  27. <ul>
  28. <li><a href="#contributors">Contributors</a></li>
  29. <li><a href="#history">History</a></li>
  30. </ul></li>
  31. </ol>
  32. <h2><a name="rationale">Rationale</a></h2>
  33. <p>Sometimes a certain value has to change only for a limited scope.
  34. Saver classes save a copy of the current state of some object (or an
  35. aspect of an object), and reset the object's state at destruction time,
  36. undoing any change the object may have gone through.</p>
  37. <p>The saver class strategy is helpful when using I/O stream objects.
  38. Manipulator objects can change some aspect of a stream during input or
  39. output. The state changed by the manipulator usually sticks to its new
  40. value after the I/O transaction. This can be a problem if manipulators
  41. are used in a function that is not supposed to externally change a
  42. stream's state.</p>
  43. <blockquote><pre>#include &lt;ostream&gt;
  44. #include &lt;ios&gt;
  45. void hex_my_byte( std::ostream &amp;os, char byte )
  46. {
  47. os &lt;&lt; std::hex &lt;&lt; static_cast&lt;unsigned&gt;(byte);
  48. }
  49. </pre></blockquote>
  50. <p>The <var>os</var> stream will retain its new hexadecimal printing
  51. mode after the call to <code>hex_my_byte</code>. The stream's printing
  52. mode can be saved and restored with manual calls to the stream's state
  53. inspecting and mutating member functions. The manual method becomes
  54. unwieldy if the main functionality is complex and/or needs to be
  55. exception safe. A saver class can implement the better &quot;resource
  56. acquisition is initialization&quot; strategy.</p>
  57. <p>See the <a href="#example">example</a> below for better code, using
  58. saver classes.</p>
  59. <h2><a name="header">Header Synopsis</a></h2>
  60. <blockquote><pre>#include &lt;iosfwd&gt; <i>// for std::char_traits (declaration)</i>
  61. namespace boost
  62. {
  63. namespace io
  64. {
  65. class ios_flags_saver;
  66. class ios_precision_saver;
  67. class ios_width_saver;
  68. class ios_base_all_saver;
  69. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  70. class basic_ios_iostate_saver;
  71. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  72. class basic_ios_exception_saver;
  73. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  74. class basic_ios_tie_saver;
  75. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  76. class basic_ios_rdbuf_saver;
  77. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  78. class basic_ios_fill_saver;
  79. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  80. class basic_ios_locale_saver;
  81. template &lt; typename Ch, class Tr = ::std::char_traits&lt;Ch&gt; &gt;
  82. class basic_ios_all_saver;
  83. typedef basic_ios_iostate_saver&lt;char&gt; ios_iostate_saver;
  84. typedef basic_ios_iostate_saver&lt;wchar_t&gt; wios_iostate_saver;
  85. typedef basic_ios_exception_saver&lt;char&gt; ios_exception_saver;
  86. typedef basic_ios_exception_saver&lt;wchar_t&gt; wios_exception_saver;
  87. typedef basic_ios_tie_saver&lt;char&gt; ios_tie_saver;
  88. typedef basic_ios_tie_saver&lt;wchar_t&gt; wios_tie_saver;
  89. typedef basic_ios_rdbuf_saver&lt;char&gt; ios_rdbuf_saver;
  90. typedef basic_ios_rdbuf_saver&lt;wchar_t&gt; wios_rdbuf_saver;
  91. typedef basic_ios_fill_saver&lt;char&gt; ios_fill_saver;
  92. typedef basic_ios_fill_saver&lt;wchar_t&gt; wios_fill_saver;
  93. typedef basic_ios_locale_saver&lt;char&gt; ios_locale_saver;
  94. typedef basic_ios_locale_saver&lt;wchar_t&gt; wios_locale_saver;
  95. typedef basic_ios_all_saver&lt;char&gt; ios_all_saver;
  96. typedef basic_ios_all_saver&lt;wchar_t&gt; wios_all_saver;
  97. class ios_iword_saver;
  98. class ios_pword_saver;
  99. class ios_all_word_saver;
  100. }
  101. }
  102. </pre></blockquote>
  103. <h2><a name="base_savers">Savers for Basic Standard Attributes</a></h2>
  104. <p>The basic saver classes have this format:</p>
  105. <blockquote><pre>class <var>saver_class</var>
  106. {
  107. typedef std::ios_base state_type;
  108. typedef <i>implementation_defined</i> aspect_type;
  109. explicit saver_class( state_type &amp;s );
  110. saver_class( state_type &amp;s, <var>aspect_type</var> const &amp;new_value );
  111. ~saver_class();
  112. void restore();
  113. };
  114. </pre></blockquote>
  115. <p>The <var>state_type</var> is the IOStreams base class
  116. <code>std::ios_base</code>. The user would usually place an actual
  117. input, output, or combined stream object for the state-type parameter,
  118. and not a base class object. The first constructor takes a stream
  119. object and saves a reference to the stream and the current value of a
  120. particular stream attribute. The second constructor works like the
  121. first, and uses its second argument to change the stream's attribute to
  122. the new <var>aspect_type</var> value given. The destructor restores the
  123. stream's attribute to the saved value. The restoration can be activated
  124. early (and often) with the <code>restore</code> member function.</p>
  125. <table border="1" align="center">
  126. <caption>Basic IOStreams State Saver Classes</caption>
  127. <tr>
  128. <th>Class</th>
  129. <th>Saved Attribute</th>
  130. <th>Attribute Type</th>
  131. <th>Reading Method</th>
  132. <th>Writing Method</th>
  133. </tr>
  134. <tr>
  135. <td><code>boost::io::ios_flags_saver</code></td>
  136. <td>Format control flags</td>
  137. <td><code>std::ios_base::fmtflags</code></td>
  138. <td><code>flags</code></td>
  139. <td><code>flags</code></td>
  140. </tr>
  141. <tr>
  142. <td><code>boost::io::ios_precision_saver</code></td>
  143. <td>Number of digits to print after decimal point</td>
  144. <td><code>std::streamsize</code></td>
  145. <td><code>precision</code></td>
  146. <td><code>precision</code></td>
  147. </tr>
  148. <tr>
  149. <td><code>boost::io::ios_width_saver</code></td>
  150. <td>Minimum field width for printing objects</td>
  151. <td><code>std::streamsize</code></td>
  152. <td><code>width</code></td>
  153. <td><code>width</code></td>
  154. </tr>
  155. </table>
  156. <h2><a name="adv_savers">Savers for Advanced Standard Attributes</a></h2>
  157. <p>The saver class templates have this format:</p>
  158. <blockquote><pre>template &lt; typename Ch, class Tr &gt;
  159. class <var>saver_class</var>
  160. {
  161. typedef std::basic_ios&lt;Ch, Tr&gt; state_type;
  162. typedef <i>implementation_defined</i> aspect_type;
  163. explicit saver_class( state_type &amp;s );
  164. saver_class( state_type &amp;s, <var>aspect_type</var> const &amp;new_value );
  165. ~saver_class();
  166. void restore();
  167. };
  168. </pre></blockquote>
  169. <p>The <var>state_type</var> is a version of the IOStreams base class
  170. template <code>std::basic_ios&lt;Ch, Tr&gt;</code>, where
  171. <code>Ch</code> is a character type and <code>Tr</code> is a character
  172. traits class. The user would usually place an actual input, output, or
  173. combined stream object for the state-type parameter, and not a base
  174. class object. The first constructor takes a stream object and saves a
  175. reference to the stream and the current value of a particular stream
  176. attribute. The second constructor works like the first, and uses its
  177. second argument to change the stream's attribute to the new
  178. <var>aspect_type</var> value given. The destructor restores the stream's
  179. attribute to the saved value. The restoration can be activated
  180. early (and often) with the <code>restore</code> member function.</p>
  181. <table border="1" align="center">
  182. <caption>Advanced IOStreams State Saver Class Templates</caption>
  183. <tr>
  184. <th>Class Template</th>
  185. <th>Saved Attribute</th>
  186. <th>Attribute Type</th>
  187. <th>Reading Method</th>
  188. <th>Writing Method</th>
  189. </tr>
  190. <tr>
  191. <td><code>boost::io::basic_ios_iostate_saver&lt;Ch, Tr&gt;</code></td>
  192. <td>Failure state of the stream <a href="#Note1">[1]</a>, <a href="#Note2">[2]</a></td>
  193. <td><code>std::ios_base::iostate</code></td>
  194. <td><code>rdstate</code></td>
  195. <td><code>clear</code></td>
  196. </tr>
  197. <tr>
  198. <td><code>boost::io::basic_ios_exception_saver&lt;Ch, Tr&gt;</code></td>
  199. <td>Which failure states trigger an exception <a href="#Note1">[1]</a></td>
  200. <td><code>std::ios_base::iostate</code></td>
  201. <td><code>exceptions</code></td>
  202. <td><code>exceptions</code></td>
  203. </tr>
  204. <tr>
  205. <td><code>boost::io::basic_ios_tie_saver&lt;Ch, Tr&gt;</code></td>
  206. <td>Output stream synchronized with the stream</td>
  207. <td><code>std::basic_ostream&lt;Ch, Tr&gt; *</code></td>
  208. <td><code>tie</code></td>
  209. <td><code>tie</code></td>
  210. </tr>
  211. <tr>
  212. <td><code>boost::io::basic_ios_rdbuf_saver&lt;Ch, Tr&gt;</code></td>
  213. <td>Stream buffer associated with the stream <a href="#Note2">[2]</a></td>
  214. <td><code>std::basic_streambuf&lt;Ch, Tr&gt; *</code></td>
  215. <td><code>rdbuf</code></td>
  216. <td><code>rdbuf</code></td>
  217. </tr>
  218. <tr>
  219. <td><code>boost::io::basic_ios_fill_saver&lt;Ch, Tr&gt;</code></td>
  220. <td>Character used to pad oversized field widths</td>
  221. <td><code>Ch</code></td>
  222. <td><code>fill</code></td>
  223. <td><code>fill</code></td>
  224. </tr>
  225. <tr>
  226. <td><code>boost::io::basic_ios_locale_saver&lt;Ch, Tr&gt;</code></td>
  227. <td>Locale information associated with the stream <a href="#Note3">[3]</a></td>
  228. <td><code>std::locale</code></td>
  229. <td><code>getloc</code> (from <code>std::ios_base</code>)</td>
  230. <td><code>imbue</code> (from <code>std::basic_ios&lt;Ch, Tr&gt;</code>)</td>
  231. </tr>
  232. </table>
  233. <h3>Notes</h3>
  234. <ol>
  235. <li>When the failure state flags and/or the failure state exception
  236. watching flags are changed, an exception is thrown if a match
  237. occurs among the two sets of flags. This could mean that
  238. the <a name="Note1">constructor or destructor of these class
  239. templates may throw</a>.</li>
  240. <li>When the associated stream buffer is changed, the stream's
  241. failure state set is reset to &quot;good&quot; if the given stream
  242. buffer's address is non-NULL, but the &quot;bad&quot; failure
  243. state is set if that address is NULL. This means that a saved
  244. failure state of &quot;good&quot; may be restored as &quot;bad&quot;
  245. if the stream is stripped of an associated stream buffer. Worse,
  246. given a NULL stream buffer address, an exception is thrown if the
  247. &quot;bad&quot; failure state is being watched. This could mean
  248. that the <a name="Note2">constructor or destructor of these class
  249. templates may throw</a>.</li>
  250. <li>The <a name="Note3">saver for the locale uses the
  251. <code>std::basic_ios&lt;Ch, Tr&gt;</code> class to extract their
  252. information</a>, although it could have used the functionality
  253. in <code>std::ios_base</code>. The problem is that the versions
  254. of the needed member functions in <code>ios_base</code> are not
  255. polymorphically related to the ones in <code>basic_ios</code>.
  256. The stream classes that will be used with the saver classes
  257. should use the versions of the member functions closest to them
  258. by inheritance, which means the ones in
  259. <code>basic_ios</code>.</li>
  260. </ol>
  261. <h2><a name="user_savers">Savers for User-Defined Attributes</a></h2>
  262. <p>The saver classes for user-defined formatting information have this
  263. format:</p>
  264. <blockquote><pre>#include &lt;iosfwd&gt; <i>// for std::ios_base (declaration)</i>
  265. class <var>saver_class</var>
  266. {
  267. typedef std::ios_base state_type;
  268. typedef int index_type;
  269. typedef <i>implementation_defined</i> aspect_type;
  270. explicit saver_class( state_type &amp;s, index_type i );
  271. saver_class( state_type &amp;s, index_type i, <var>aspect_type</var> const &amp;new_value );
  272. ~saver_class();
  273. void restore();
  274. };
  275. </pre></blockquote>
  276. <p>The index <var>i</var> differentiates between specific user-defined
  277. formatting attributes. The index can only be determined at run-time
  278. (most likely with the class-static <code>std::ios_base::xalloc</code>
  279. member function).</p>
  280. <p>The <var>state_type</var> is the base class of the IOStreams system,
  281. <code>std::ios_base</code>. The user would usually place an actual
  282. input, output, or combined stream object for the state-type parameter,
  283. and not a base class object. The first constructor takes a stream
  284. object and index and saves a reference to the stream and the current
  285. value of a particular stream attribute. The second constructor works
  286. like the first, and uses its third argument to change the stream's
  287. attribute to the new <var>aspect_type</var> value given. The destructor
  288. restores the stream's attribute to the saved value. The restoration can
  289. be activated early (and often) with the <code>restore</code> member
  290. function.</p>
  291. <table border="1" align="center">
  292. <caption>IOStream User-Defined State Saver Classes</caption>
  293. <tr>
  294. <th>Class</th>
  295. <th>Saved Attribute</th>
  296. <th>Attribute Type</th>
  297. <th>Reference Method</th>
  298. </tr>
  299. <tr>
  300. <td><code>boost::io::ios_iword_saver</code></td>
  301. <td>Numeric user-defined format flag</td>
  302. <td><code>long</code></td>
  303. <td><code>iword</code></td>
  304. </tr>
  305. <tr>
  306. <td><code>boost::io::ios_pword_saver</code></td>
  307. <td>Pointer user-defined format flag</td>
  308. <td><code>void *</code></td>
  309. <td><code>pword</code></td>
  310. </tr>
  311. </table>
  312. <h2><a name="combo_savers">Savers for Combined Attributes</a></h2>
  313. <p>There are three class (templates) for combined attribute savers. The
  314. <code>boost:io::ios_base_all_saver</code> saver class combines the
  315. functionality of all the basic attribute saver classes. It has a
  316. constructor that takes the stream to have its state preserved. The
  317. <code>boost::io::basic_ios_all_saver</code> combines the functionality
  318. of all the advanced attribute saver class templates and the combined
  319. basic attribute saver class. It has a constructor that takes the stream
  320. to have its state preserved. The
  321. <code>boost::io::ios_all_word_saver</code> saver class combines the
  322. saver classes that preserve user-defined formatting information. Its
  323. constructor takes the stream to have its attributes saved and the index
  324. of the user-defined attributes. The destructor for each class restores
  325. the saved state. Restoration can be activated early (and often) for a
  326. class with the <code>restore</code> member function.</p>
  327. <h2><a name="example">Example</a></h2>
  328. <p>The code used in the <a href="#rationale">rationale</a> can be
  329. improved at two places. The printing function could use a saver around
  330. the code that changes the formatting state. Or the calling function can
  331. surround the call with a saver. Or both can be done, especially if the
  332. user does not know if the printing function uses a state saver. If the
  333. user wants a series of changes back &amp; forth, without surrounding each
  334. change within a separate block, the <code>restore</code> member function
  335. can be called between each trial.</p>
  336. <blockquote><pre>#include &lt;boost/io/ios_state.hpp&gt;
  337. #include &lt;ios&gt;
  338. #include &lt;iostream&gt;
  339. #include &lt;ostream&gt;
  340. void new_hex_my_byte( std::ostream &amp;os, char byte )
  341. {
  342. boost::io::ios_flags_saver ifs( os );
  343. os &lt;&lt; std::hex &lt;&lt; static_cast&lt;unsigned&gt;(byte);
  344. }
  345. int main()
  346. {
  347. using std::cout;
  348. using std::cerr;
  349. //...
  350. {
  351. boost::io::ios_all_saver ias( cout );
  352. new_hex_my_byte( cout, 'A' );
  353. }
  354. //...
  355. {
  356. boost::io::ios_all_saver ias( cerr );
  357. new_hex_my_byte( cerr, 'b' );
  358. ias.restore();
  359. new_hex_my_byte( cerr, 'C' );
  360. }
  361. //...
  362. }
  363. </pre></blockquote>
  364. <h2><a name="refer">References</a></h2>
  365. <ul>
  366. <li>The I/O state saver library header itself: <cite><a
  367. href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite></li>
  368. <li>Some test/example code: <cite><a
  369. href="../test/ios_state_test.cpp">ios_state_test.cpp</a></cite></li>
  370. </ul>
  371. <h2><a name="credits">Credits</a></h2>
  372. <h3><a name="contributors">Contributors</a></h3>
  373. <dl>
  374. <dt><a href="http://www.boost.org/people/daryle_walker.html">Daryle Walker</a>
  375. <dd>Started the library. Contributed the initial versions of the
  376. format flags, precision, width, and user-defined format flags
  377. saver classes. Contributed the initial versions of the success
  378. state, success state exception flags, output stream tie, stream
  379. buffer, character fill, and locale saver class templates.
  380. Contributed the combined attribute classes and class template.
  381. Contributed the test file <cite><a
  382. href="../test/ios_state_test.cpp">ios_state_test.cpp</a></cite>.
  383. </dl>
  384. <h3><a name="history">History</a></h3>
  385. <dl>
  386. <dt>28 Feb 2005, Daryle Walker
  387. <dd>Added the <code>restore</code> member functions, based on suggestions
  388. by Gennadiy Rozental and Rob Stewart
  389. <dt>13 Mar 2002, Daryle Walker
  390. <dd>Initial version
  391. </dl>
  392. <hr>
  393. <p>Revised: 28 February 2005</p>
  394. <p>Copyright 2002, 2005 Daryle Walker. Use, modification, and distribution
  395. are subject to the Boost Software License, Version 1.0. (See accompanying
  396. file <a href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at
  397. &lt;<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>&gt;.)</p>
  398. </body>
  399. </html>