mirror of
https://github.com/boostorg/io.git
synced 2025-07-29 20:07:13 +02:00
Initial commit
[SVN r13766]
This commit is contained in:
96
.gitattributes
vendored
Normal file
96
.gitattributes
vendored
Normal file
@ -0,0 +1,96 @@
|
||||
* text=auto !eol svneol=native#text/plain
|
||||
*.gitattributes text svneol=native#text/plain
|
||||
|
||||
# Scriptish formats
|
||||
*.bat text svneol=native#text/plain
|
||||
*.bsh text svneol=native#text/x-beanshell
|
||||
*.cgi text svneol=native#text/plain
|
||||
*.cmd text svneol=native#text/plain
|
||||
*.js text svneol=native#text/javascript
|
||||
*.php text svneol=native#text/x-php
|
||||
*.pl text svneol=native#text/x-perl
|
||||
*.pm text svneol=native#text/x-perl
|
||||
*.py text svneol=native#text/x-python
|
||||
*.sh eol=lf svneol=LF#text/x-sh
|
||||
configure eol=lf svneol=LF#text/x-sh
|
||||
|
||||
# Image formats
|
||||
*.bmp binary svneol=unset#image/bmp
|
||||
*.gif binary svneol=unset#image/gif
|
||||
*.ico binary svneol=unset#image/ico
|
||||
*.jpeg binary svneol=unset#image/jpeg
|
||||
*.jpg binary svneol=unset#image/jpeg
|
||||
*.png binary svneol=unset#image/png
|
||||
*.tif binary svneol=unset#image/tiff
|
||||
*.tiff binary svneol=unset#image/tiff
|
||||
*.svg text svneol=native#image/svg%2Bxml
|
||||
|
||||
# Data formats
|
||||
*.pdf binary svneol=unset#application/pdf
|
||||
*.avi binary svneol=unset#video/avi
|
||||
*.doc binary svneol=unset#application/msword
|
||||
*.dsp text svneol=crlf#text/plain
|
||||
*.dsw text svneol=crlf#text/plain
|
||||
*.eps binary svneol=unset#application/postscript
|
||||
*.gz binary svneol=unset#application/gzip
|
||||
*.mov binary svneol=unset#video/quicktime
|
||||
*.mp3 binary svneol=unset#audio/mpeg
|
||||
*.ppt binary svneol=unset#application/vnd.ms-powerpoint
|
||||
*.ps binary svneol=unset#application/postscript
|
||||
*.psd binary svneol=unset#application/photoshop
|
||||
*.rdf binary svneol=unset#text/rdf
|
||||
*.rss text svneol=unset#text/xml
|
||||
*.rtf binary svneol=unset#text/rtf
|
||||
*.sln text svneol=native#text/plain
|
||||
*.swf binary svneol=unset#application/x-shockwave-flash
|
||||
*.tgz binary svneol=unset#application/gzip
|
||||
*.vcproj text svneol=native#text/xml
|
||||
*.vcxproj text svneol=native#text/xml
|
||||
*.vsprops text svneol=native#text/xml
|
||||
*.wav binary svneol=unset#audio/wav
|
||||
*.xls binary svneol=unset#application/vnd.ms-excel
|
||||
*.zip binary svneol=unset#application/zip
|
||||
|
||||
# Text formats
|
||||
.htaccess text svneol=native#text/plain
|
||||
*.bbk text svneol=native#text/xml
|
||||
*.cmake text svneol=native#text/plain
|
||||
*.css text svneol=native#text/css
|
||||
*.dtd text svneol=native#text/xml
|
||||
*.htm text svneol=native#text/html
|
||||
*.html text svneol=native#text/html
|
||||
*.ini text svneol=native#text/plain
|
||||
*.log text svneol=native#text/plain
|
||||
*.mak text svneol=native#text/plain
|
||||
*.qbk text svneol=native#text/plain
|
||||
*.rst text svneol=native#text/plain
|
||||
*.sql text svneol=native#text/x-sql
|
||||
*.txt text svneol=native#text/plain
|
||||
*.xhtml text svneol=native#text/xhtml%2Bxml
|
||||
*.xml text svneol=native#text/xml
|
||||
*.xsd text svneol=native#text/xml
|
||||
*.xsl text svneol=native#text/xml
|
||||
*.xslt text svneol=native#text/xml
|
||||
*.xul text svneol=native#text/xul
|
||||
*.yml text svneol=native#text/plain
|
||||
boost-no-inspect text svneol=native#text/plain
|
||||
CHANGES text svneol=native#text/plain
|
||||
COPYING text svneol=native#text/plain
|
||||
INSTALL text svneol=native#text/plain
|
||||
Jamfile text svneol=native#text/plain
|
||||
Jamroot text svneol=native#text/plain
|
||||
Jamfile.v2 text svneol=native#text/plain
|
||||
Jamrules text svneol=native#text/plain
|
||||
Makefile* text svneol=native#text/plain
|
||||
README text svneol=native#text/plain
|
||||
TODO text svneol=native#text/plain
|
||||
|
||||
# Code formats
|
||||
*.c text svneol=native#text/plain
|
||||
*.cpp text svneol=native#text/plain
|
||||
*.h text svneol=native#text/plain
|
||||
*.hpp text svneol=native#text/plain
|
||||
*.ipp text svneol=native#text/plain
|
||||
*.tpp text svneol=native#text/plain
|
||||
*.jam text svneol=native#text/plain
|
||||
*.java text svneol=native#text/plain
|
62
doc/index.html
Normal file
62
doc/index.html
Normal file
@ -0,0 +1,62 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Boost I/O Library</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="white" text="black">
|
||||
<table border="1" bgcolor="teal" cellpadding="2">
|
||||
<tr>
|
||||
<td bgcolor="white"><img src="../../../c++boost.gif"
|
||||
alt="c++boost.gif (8819 bytes)" width="277"
|
||||
height="86"></td>
|
||||
<td><a href="../../../index.htm"><font face="Arial"
|
||||
color="white"><big>Home</big></font></a></td>
|
||||
<td><a href="../../libraries.htm"><font face="Arial"
|
||||
color="white"><big>Libraries</big></font></a></td>
|
||||
<td><a href="../../../people/people.htm"><font face="Arial"
|
||||
color="white"><big>People</big></font></a></td>
|
||||
<td><a href="../../../more/faq.htm"><font face="Arial"
|
||||
color="white"><big>FAQ</big></font></a></td>
|
||||
<td><a href="../../../more/index.htm"><font face="Arial"
|
||||
color="white"><big>More</big></font></a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h1>Boost Input/Output Library</h1>
|
||||
|
||||
<table border="1" cellpadding="5" align="center">
|
||||
<tr>
|
||||
<th>Header / Docs</th>
|
||||
<th>Contents</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center">
|
||||
<code><a href="../../../boost/io_fwd.hpp"><boost/io_fwd.hpp></a></code>
|
||||
</td>
|
||||
<td valign="top">
|
||||
Forward declaration header.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center">
|
||||
<code><a href="../../../boost/io/ios_state.hpp"><boost/io/ios_state.hpp></a></code><br>
|
||||
<br><a href="ios_state.html">documentation</a>
|
||||
</td>
|
||||
<td valign="top">
|
||||
State-saving classes for various IOStream attributes.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Rationale</h2>
|
||||
|
||||
<p>The I/O sub-library of Boost helps segregate the large number of
|
||||
Boost headers. This sub-library should contain various items to use
|
||||
with/for the standard I/O library.</p>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>Revised: 26 Feb 2002</p>
|
||||
</body>
|
||||
</html>
|
341
doc/ios_state.html
Normal file
341
doc/ios_state.html
Normal file
@ -0,0 +1,341 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>I/O Stream-State Saver Library</title>
|
||||
</head>
|
||||
<body text="black" bgcolor="white" link="blue" vlink="purple" alink="red">
|
||||
<h1><img src="../../../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="middle" width="277" height="86">Header <<cite><a href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite>></h1>
|
||||
|
||||
<p>The header <cite><a href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite> covers saving the stream state of objects in the C++ IOStreams system.</p>
|
||||
|
||||
<h2><a name="contents">Contents</a></h2>
|
||||
|
||||
<ol>
|
||||
<li><a href="#contents">Contents</a>
|
||||
<li><a href="#rationale">Rationale</a>
|
||||
<li><a href="#header">Header Synopsis</a>
|
||||
<li><a href="#base_savers">Savers for Basic Standard Attributes</a></li>
|
||||
<li><a href="#adv_savers">Savers for Advanced Standard Attributes</a></li>
|
||||
<li><a href="#user_savers">Savers for User-Defined Attributes</a></li>
|
||||
<li><a href="#combo_savers">Savers for Combined Attributes</a></li>
|
||||
<li><a href="#example">Example</a></li>
|
||||
<li><a href="#refer">References</a>
|
||||
<li><a href="#credits">Credits</a>
|
||||
<ul>
|
||||
<li><a href="#contributors">Contributors</a>
|
||||
<li><a href="#history">History</a>
|
||||
</ul>
|
||||
</ol>
|
||||
|
||||
<h2><a name="rationale">Rationale</a></h2>
|
||||
|
||||
<p>Sometimes a certain value has to change only for a limited scope. Saver classes save a copy of the current state of some object (or an aspect of an object), and reset the object's state at destruction time, undoing any change the object may have gone through.</p>
|
||||
|
||||
<p>The saver class strategy is helpful when using I/O stream objects. Manipulator objects can change some aspect of a stream during input or output. The state changed by the manipulator usually sticks to its new value after the I/O transaction. This can be a problem if manipulators are used in a function that is not supposed to externally change a stream's state.</p>
|
||||
|
||||
<blockquote><pre>#include <ostream>
|
||||
#include <ios>
|
||||
|
||||
void hex_my_byte( std::ostream &os, char byte )
|
||||
{
|
||||
os << std::hex << static_cast<unsigned>(byte);
|
||||
}
|
||||
</pre></blockquote>
|
||||
|
||||
<p>The <var>os</var> stream will retain its new hexadecimal printing mode after the call to <code>hex_my_byte</code>. The stream's printing mode can be saved and restored with manual calls to the stream's state inspecting and mutating member functions. The manual method becomes unwieldy if the main functionality is complex and/or needs to be exception safe. A saver class can implement the better "resource
|
||||
acquisition is initialization" strategy.</p>
|
||||
|
||||
<p>See the <a href="#example">example</a> below for better code, using a saver
|
||||
class.</p>
|
||||
|
||||
<h2><a name="header">Header Synopsis</a></h2>
|
||||
|
||||
<blockquote><pre>#include <iosfwd> <i>// for std::char_traits (declaration)</i>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
|
||||
class ios_flags_saver;
|
||||
class ios_precision_saver;
|
||||
class ios_width_saver;
|
||||
class ios_base_all_saver;
|
||||
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_iostate_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_exception_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_tie_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_rdbuf_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_fill_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_locale_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_all_saver;
|
||||
|
||||
typedef basic_ios_iostate_saver<char> ios_iostate_saver;
|
||||
typedef basic_ios_iostate_saver<wchar_t> wios_iostate_saver;
|
||||
typedef basic_ios_exception_saver<char> ios_exception_saver;
|
||||
typedef basic_ios_exception_saver<wchar_t> wios_exception_saver;
|
||||
typedef basic_ios_tie_saver<char> ios_tie_saver;
|
||||
typedef basic_ios_tie_saver<wchar_t> wios_tie_saver;
|
||||
typedef basic_ios_rdbuf_saver<char> ios_rdbuf_saver;
|
||||
typedef basic_ios_rdbuf_saver<wchar_t> wios_rdbuf_saver;
|
||||
typedef basic_ios_fill_saver<char> ios_fill_saver;
|
||||
typedef basic_ios_fill_saver<wchar_t> wios_fill_saver;
|
||||
typedef basic_ios_locale_saver<char> ios_locale_saver;
|
||||
typedef basic_ios_locale_saver<wchar_t> wios_locale_saver;
|
||||
typedef basic_ios_all_saver<char> ios_all_saver;
|
||||
typedef basic_ios_all_saver<wchar_t> wios_all_saver;
|
||||
|
||||
class ios_iword_saver;
|
||||
class ios_pword_saver;
|
||||
class ios_all_word_saver;
|
||||
|
||||
}
|
||||
}
|
||||
</pre></blockquote>
|
||||
|
||||
<h2><a name="base_savers">Savers for Basic Standard Attributes</a></h2>
|
||||
|
||||
<p>The basic saver classes have this format:</p>
|
||||
|
||||
<blockquote><pre>class <var>saver_class</var>
|
||||
{
|
||||
typedef std::ios_base state_type;
|
||||
typedef <i>implementation_defined</i> aspect_type;
|
||||
|
||||
explicit saver_class( state_type &s );
|
||||
saver_class( state_type &s, <var>aspect_type</var> const &new_value );
|
||||
~saver_class();
|
||||
};
|
||||
</pre></blockquote>
|
||||
|
||||
<p>The <var>state_type</var> is the IOStreams base class <code>std::ios_base</code>. The user would usually place an actual input, output, or combined stream object for the state-type parameter, and not a base class object. The first constructor takes a stream object and saves a reference to the stream and the current value of a particular stream attribute. The second constructor works like the first, and uses its second argument to change the stream's attribute to the new <var>aspect_type</var> value given. The destructor changes the stream's attribute back to the saved value.</p>
|
||||
|
||||
<table border="1" align="center">
|
||||
<caption>Basic IOStreams State Saver Classes</caption>
|
||||
<tr>
|
||||
<th>Class</th>
|
||||
<th>Saved Attribute</th>
|
||||
<th>Attribute Type</th>
|
||||
<th>Reading Method</th>
|
||||
<th>Writing Method</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::ios_flags_saver</code></td>
|
||||
<td>Format control flags</td>
|
||||
<td><code>std::ios_base::fmtflags</code></td>
|
||||
<td><code>flags</code></td>
|
||||
<td><code>flags</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::ios_precision_saver</code></td>
|
||||
<td>Number of digits to print after decimal point</td>
|
||||
<td><code>std::streamsize</code></td>
|
||||
<td><code>precision</code></td>
|
||||
<td><code>precision</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::ios_width_saver</code></td>
|
||||
<td>Minimum field width for printing objects</td>
|
||||
<td><code>std::streamsize</code></td>
|
||||
<td><code>width</code></td>
|
||||
<td><code>width</code></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2><a name="adv_savers">Savers for Advanced Standard Attributes</a></h2>
|
||||
|
||||
<p>The saver class templates have this format:</p>
|
||||
|
||||
<blockquote><pre>template < typename Ch, class Tr >
|
||||
class <var>saver_class</var>
|
||||
{
|
||||
typedef std::basic_ios<Ch, Tr> state_type;
|
||||
typedef <i>implementation_defined</i> aspect_type;
|
||||
|
||||
explicit saver_class( state_type &s );
|
||||
saver_class( state_type &s, <var>aspect_type</var> const &new_value );
|
||||
~saver_class();
|
||||
};
|
||||
</pre></blockquote>
|
||||
|
||||
<p>The <var>state_type</var> is a version of the IOStreams base class template <code>std::basic_ios<Ch, Tr></code>, where <code>Ch</code> is a character type and <code>Tr</code> is a character traits class. The user would usually place an actual input, output, or combined stream object for the state-type parameter, and not a base class object. The first constructor takes a stream object and saves a reference to the stream and the current value of a particular stream attribute. The second constructor works like the first, and uses its second argument to change the stream's attribute to the new <var>aspect_type</var> value given. The destructor changes the stream's attribute back to the saved value.</p>
|
||||
|
||||
<table border="1" align="center">
|
||||
<caption>Advanced IOStreams State Saver Class Templates</caption>
|
||||
<tr>
|
||||
<th>Class Template</th>
|
||||
<th>Saved Attribute</th>
|
||||
<th>Attribute Type</th>
|
||||
<th>Reading Method</th>
|
||||
<th>Writing Method</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::basic_ios_iostate_saver<Ch, Tr></code></td>
|
||||
<td>Success state of the stream</td>
|
||||
<td><code>std::ios_base::iostate</code></td>
|
||||
<td><code>rdstate</code></td>
|
||||
<td><code>clear</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::basic_ios_exception_saver<Ch, Tr></code></td>
|
||||
<td>Which success states trigger an exception <a href="#Note1">[1]</a></td>
|
||||
<td><code>std::ios_base::iostate</code></td>
|
||||
<td><code>exceptions</code></td>
|
||||
<td><code>exceptions</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::basic_ios_tie_saver<Ch, Tr></code></td>
|
||||
<td>Output stream synchronized with the stream</td>
|
||||
<td><code>std::basic_ostream<Ch, Tr> *</code></td>
|
||||
<td><code>tie</code></td>
|
||||
<td><code>tie</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::basic_ios_rdbuf_saver<Ch, Tr></code></td>
|
||||
<td>Stream buffer associated with the stream</td>
|
||||
<td><code>std::basic_streambuf<Ch, Tr> *</code></td>
|
||||
<td><code>rdbuf</code></td>
|
||||
<td><code>rdbuf</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::basic_ios_fill_saver<Ch, Tr></code></td>
|
||||
<td>Character used to pad oversized field widths</td>
|
||||
<td><code>Ch</code></td>
|
||||
<td><code>fill</code></td>
|
||||
<td><code>fill</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::basic_ios_locale_saver<Ch, Tr></code></td>
|
||||
<td>Locale information associated with the stream <a href="#Note2">[2]</a></td>
|
||||
<td><code>std::locale</code></td>
|
||||
<td><code>getloc</code> (from <code>std::ios_base</code>)</td>
|
||||
<td><code>imbue</code> (from <code>std::basic_ios<Ch, Tr></code>)</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3>Notes</h3>
|
||||
|
||||
<ol>
|
||||
<li>When a new success state is activated for exception watching, an exception is thrown if that state is already active. This could mean that the <a name="Note1">destructor of this class template may throw</a>.</li>
|
||||
<li>The <a name="Note2">saver for the locale uses the <code>std::basic_ios<Ch, Tr></code> class to extract their information</a>, although it could have used the functionality in <code>std::ios_base</code>. The problem is that the versions of the needed member functions in <code>ios_base</code> are not
|
||||
polymorphicly related to the ones in <code>basic_ios</code>. The stream classes that will be used with the saver classes should use the versions of the member functions closest to them by
|
||||
inheritance, which means the ones in <code>basic_ios</code>.</li>
|
||||
</ol>
|
||||
|
||||
<h2><a name="user_savers">Savers for User-Defined Attributes</a></h2>
|
||||
|
||||
<p>The saver classes for user-defined formatting information have this format:</p>
|
||||
|
||||
<blockquote><pre>#include <iosfwd> <i>// for std::ios_base (declaration)</i>
|
||||
|
||||
class <var>saver_class</var>
|
||||
{
|
||||
typedef std::ios_base state_type;
|
||||
typedef int index_type;
|
||||
typedef <i>implementation_defined</i> aspect_type;
|
||||
|
||||
explicit saver_class( state_type &s, index_type i );
|
||||
saver_class( state_type &s, index_type i, <var>aspect_type</var> const &new_value );
|
||||
~saver_class();
|
||||
};
|
||||
</pre></blockquote>
|
||||
|
||||
<p>The index <var>i</var> differentiates between specific user-defined formatting attributes. The index can only be determined at run-time (most likely with the class-static <code>std::ios_base::xalloc</code> member function).</p>
|
||||
|
||||
<p>The <var>state_type</var> is the base class of the IOStreams system, <code>std::ios_base</code>. The user would usually place an actual input, output, or combined stream object for the state-type parameter, and not a base class object. The first constructor takes a stream object and index and saves a reference to the stream and the current value of a particular stream attribute. The second constructor works like the first, and uses its third argument to change the stream's attribute to the new <var>aspect_type</var> value given. The destructor changes the stream's attribute back to the saved value.</p>
|
||||
|
||||
<table border="1" align="center">
|
||||
<caption>IOStream User-Defined State Saver Classes</caption>
|
||||
<tr>
|
||||
<th>Class</th>
|
||||
<th>Saved Attribute</th>
|
||||
<th>Attribute Type</th>
|
||||
<th>Reference Method</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::ios_iword_saver</code></td>
|
||||
<td>Numeric user-defined format flag</td>
|
||||
<td><code>long</code></td>
|
||||
<td><code>iword</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>boost::io::ios_pword_saver</code></td>
|
||||
<td>Pointer user-defined format flag</td>
|
||||
<td><code>void *</code></td>
|
||||
<td><code>pword</code></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2><a name="combo_savers">Savers for Combined Attributes</a></h2>
|
||||
|
||||
<p>There are three class (templates) for combined attribute savers. The <code>boost:io::ios_base_all_saver</code> saver class combines the functionality of all the basic attribute saver classes. It has a constructor that takes the stream to have its state preserved. The <code>boost::io::basic_ios_all_saver</code> combines the functionality of all the advanced attribute saver class templates and the combined basic attribute saver class. It has a constructor that takes the stream to have its state preserved. The <code>boost::io::ios_all_word_saver</code> saver class combines the saver classes that preserve user-defined formatting information. Its constructor takes the stream to have its attributes saved and the index of the user-defined attributes.</p>
|
||||
|
||||
<h2><a name="example">Example</a></h2>
|
||||
|
||||
<p>The code used in the <a href="#rationale">rationale</a> can be improved at two places. The printing function could use a saver around the code that changes the formatting state. Or the calling function can surround the call with a saver. Or both can be done for paranoia's sake.</p>
|
||||
|
||||
<blockquote><pre>#include <boost/io/ios_state.hpp>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
void new_hex_my_byte( std::ostream &os, char byte )
|
||||
{
|
||||
boost::io::ios_flags_saver ifs( os );
|
||||
|
||||
os << std::hex << static_cast<unsigned>(byte);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
using std::cout;
|
||||
|
||||
//...
|
||||
|
||||
{
|
||||
boost::io::ios_all_saver ias( cout );
|
||||
|
||||
new_hex_my_byte( cout, 'A' );
|
||||
}
|
||||
|
||||
//...
|
||||
}
|
||||
</pre></blockquote>
|
||||
|
||||
<h2><a name="refer">References</a></h2>
|
||||
|
||||
<ul>
|
||||
<li>The I/O state saver library header itself: <cite><a href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite>
|
||||
<li>Some test/example code: <cite><a href="../test/ios_state_test.cpp">ios_state_test.cpp</a></cite>
|
||||
</ul>
|
||||
|
||||
<h2><a name="credits">Credits</a></h2>
|
||||
|
||||
<h3><a name="contributors">Contributors</a></h3>
|
||||
|
||||
<dl>
|
||||
<dt><a href="../../../people/daryle_walker.html">Daryle Walker</a>
|
||||
<dd>Started the library. Contributed the initial versions of the format flags, precision, width, and user-defined format flags saver classes. Contributed the initial versions of the success state, success state exception flags, output stream tie, stream buffer, character fill, and locale saver class templates. Contributed the combined attribute classes and class template. Contributed the test file <cite><a href="../test/ios_state_test.cpp">ios_state_test.cpp</a></cite>.
|
||||
</dl>
|
||||
|
||||
<h3><a name="history">History</a></h3>
|
||||
|
||||
<dl>
|
||||
<dt>13 Mar 2002, Daryle Walker
|
||||
<dd>Initial version
|
||||
</dl>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>Revised: 13 March 2002</p>
|
||||
|
||||
<p>Copyright © Daryle Walker 2002. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.</p>
|
||||
</body>
|
||||
</html>
|
361
include/boost/io/ios_state.hpp
Normal file
361
include/boost/io/ios_state.hpp
Normal file
@ -0,0 +1,361 @@
|
||||
// Boost io/ios_state.hpp header file --------------------------------------//
|
||||
|
||||
// (C) Copyright Daryle Walker 2002. Permission to copy, use, modify, sell and
|
||||
// distribute this software is granted provided this copyright notice appears
|
||||
// in all copies. This software is provided "as is" without express or implied
|
||||
// warranty, and with no claim as to its suitability for any purpose.
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#ifndef BOOST_IO_IOS_STATE_HPP
|
||||
#define BOOST_IO_IOS_STATE_HPP
|
||||
|
||||
#include <boost/io_fwd.hpp> // self include
|
||||
|
||||
#include <ios> // for std::ios_base, std::basic_ios, etc.
|
||||
#include <locale> // for std::locale
|
||||
#include <ostream> // for std::basic_ostream
|
||||
#include <streambuf> // for std::basic_streambuf
|
||||
#include <string> // for std::char_traits
|
||||
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
|
||||
|
||||
// Basic stream state saver class declarations -----------------------------//
|
||||
|
||||
class ios_flags_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
typedef ::std::ios_base::fmtflags aspect_type;
|
||||
|
||||
explicit ios_flags_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.flags() )
|
||||
{}
|
||||
ios_flags_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.flags(a) )
|
||||
{}
|
||||
~ios_flags_saver()
|
||||
{ s_save_.flags( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
class ios_precision_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
typedef ::std::streamsize aspect_type;
|
||||
|
||||
explicit ios_precision_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.precision() )
|
||||
{}
|
||||
ios_precision_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.precision(a) )
|
||||
{}
|
||||
~ios_precision_saver()
|
||||
{ s_save_.precision( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
class ios_width_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
typedef ::std::streamsize aspect_type;
|
||||
|
||||
explicit ios_width_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.width() )
|
||||
{}
|
||||
ios_width_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.width(a) )
|
||||
{}
|
||||
~ios_width_saver()
|
||||
{ s_save_.width( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
|
||||
// Advanced stream state saver class template declarations -----------------//
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_iostate_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
typedef ::std::ios_base::iostate aspect_type;
|
||||
|
||||
explicit basic_ios_iostate_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.rdstate() )
|
||||
{}
|
||||
basic_ios_iostate_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.rdstate() )
|
||||
{ s.clear(a); }
|
||||
~basic_ios_iostate_saver()
|
||||
{ s_save_.clear( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_exception_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
typedef ::std::ios_base::iostate aspect_type;
|
||||
|
||||
explicit basic_ios_exception_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.exceptions() )
|
||||
{}
|
||||
basic_ios_exception_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.exceptions() )
|
||||
{ s.exceptions(a); }
|
||||
~basic_ios_exception_saver()
|
||||
{ s_save_.exceptions( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_tie_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
typedef ::std::basic_ostream<Ch, Tr> * aspect_type;
|
||||
|
||||
explicit basic_ios_tie_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.tie() )
|
||||
{}
|
||||
basic_ios_tie_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.tie(a) )
|
||||
{}
|
||||
~basic_ios_tie_saver()
|
||||
{ s_save_.tie( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_rdbuf_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
typedef ::std::basic_streambuf<Ch, Tr> * aspect_type;
|
||||
|
||||
explicit basic_ios_rdbuf_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.rdbuf() )
|
||||
{}
|
||||
basic_ios_rdbuf_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.rdbuf(a) )
|
||||
{}
|
||||
~basic_ios_rdbuf_saver()
|
||||
{ s_save_.rdbuf( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_fill_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
typedef typename state_type::char_type aspect_type;
|
||||
|
||||
explicit basic_ios_fill_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.fill() )
|
||||
{}
|
||||
basic_ios_fill_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.fill(a) )
|
||||
{}
|
||||
~basic_ios_fill_saver()
|
||||
{ s_save_.fill( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_locale_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
typedef ::std::locale aspect_type;
|
||||
|
||||
explicit basic_ios_locale_saver( state_type &s )
|
||||
: s_save_( s ), a_save_( s.getloc() )
|
||||
{}
|
||||
basic_ios_locale_saver( state_type &s, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.imbue(a) )
|
||||
{}
|
||||
~basic_ios_locale_saver()
|
||||
{ s_save_.imbue( a_save_ ); }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
};
|
||||
|
||||
|
||||
// User-defined stream state saver class declarations ----------------------//
|
||||
|
||||
class ios_iword_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
typedef int index_type;
|
||||
typedef long aspect_type;
|
||||
|
||||
explicit ios_iword_saver( state_type &s, index_type i )
|
||||
: s_save_( s ), a_save_( s.iword(i) ), i_save_( i )
|
||||
{}
|
||||
ios_iword_saver( state_type &s, index_type i, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.iword(i) ), i_save_( i )
|
||||
{ s.iword(i) = a; }
|
||||
~ios_iword_saver()
|
||||
{ s_save_.iword( i_save_ ) = a_save_; }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
index_type const i_save_;
|
||||
};
|
||||
|
||||
class ios_pword_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
typedef int index_type;
|
||||
typedef void * aspect_type;
|
||||
|
||||
explicit ios_pword_saver( state_type &s, index_type i )
|
||||
: s_save_( s ), a_save_( s.pword(i) ), i_save_( i )
|
||||
{}
|
||||
ios_pword_saver( state_type &s, index_type i, aspect_type const &a )
|
||||
: s_save_( s ), a_save_( s.pword(i) ), i_save_( i )
|
||||
{ s.pword(i) = a; }
|
||||
~ios_pword_saver()
|
||||
{ s_save_.pword( i_save_ ) = a_save_; }
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
aspect_type const a_save_;
|
||||
index_type const i_save_;
|
||||
};
|
||||
|
||||
|
||||
// Combined stream state saver class (template) declarations ---------------//
|
||||
|
||||
class ios_base_all_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
|
||||
explicit ios_base_all_saver( state_type &s )
|
||||
: s_save_( s ), a1_save_( s.flags() ), a2_save_( s.precision() )
|
||||
, a3_save_( s.width() )
|
||||
{}
|
||||
|
||||
~ios_base_all_saver()
|
||||
{
|
||||
s_save_.width( a3_save_ );
|
||||
s_save_.precision( a2_save_ );
|
||||
s_save_.flags( a1_save_ );
|
||||
}
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
state_type::fmtflags const a1_save_;
|
||||
::std::streamsize const a2_save_;
|
||||
::std::streamsize const a3_save_;
|
||||
};
|
||||
|
||||
template < typename Ch, class Tr >
|
||||
class basic_ios_all_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::basic_ios<Ch, Tr> state_type;
|
||||
|
||||
explicit basic_ios_all_saver( state_type &s )
|
||||
: s_save_( s ), a1_save_( s.flags() ), a2_save_( s.precision() )
|
||||
, a3_save_( s.width() ), a4_save_( s.rdstate() )
|
||||
, a5_save_( s.exceptions() ), a6_save_( s.tie() )
|
||||
, a7_save_( s.rdbuf() ), a8_save_( s.fill() ), a9_save_( s.getloc() )
|
||||
{}
|
||||
|
||||
~basic_ios_all_saver()
|
||||
{
|
||||
s_save_.imbue( a9_save_ );
|
||||
s_save_.fill( a8_save_ );
|
||||
s_save_.rdbuf( a7_save_ );
|
||||
s_save_.tie( a6_save_ );
|
||||
s_save_.exceptions( a5_save_ );
|
||||
s_save_.clear( a4_save_ );
|
||||
s_save_.width( a3_save_ );
|
||||
s_save_.precision( a2_save_ );
|
||||
s_save_.flags( a1_save_ );
|
||||
}
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
typename state_type::fmtflags const a1_save_;
|
||||
::std::streamsize const a2_save_;
|
||||
::std::streamsize const a3_save_;
|
||||
typename state_type::iostate const a4_save_;
|
||||
typename state_type::iostate const a5_save_;
|
||||
::std::basic_ostream<Ch, Tr> * const a6_save_;
|
||||
::std::basic_streambuf<Ch, Tr> * const a7_save_;
|
||||
typename state_type::char_type const a8_save_;
|
||||
::std::locale const a9_save_;
|
||||
};
|
||||
|
||||
class ios_all_word_saver
|
||||
{
|
||||
public:
|
||||
typedef ::std::ios_base state_type;
|
||||
typedef int index_type;
|
||||
|
||||
ios_all_word_saver( state_type &s, index_type i )
|
||||
: s_save_( s ), i_save_( i ), a1_save_( s.iword(i) )
|
||||
, a2_save_( s.pword(i) )
|
||||
{}
|
||||
|
||||
~ios_all_word_saver()
|
||||
{
|
||||
s_save_.pword( i_save_ ) = a2_save_;
|
||||
s_save_.iword( i_save_ ) = a1_save_;
|
||||
}
|
||||
|
||||
private:
|
||||
state_type & s_save_;
|
||||
index_type const i_save_;
|
||||
long const a1_save_;
|
||||
void * const a2_save_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace io
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // BOOST_IO_IOS_STATE_HPP
|
68
include/boost/io_fwd.hpp
Normal file
68
include/boost/io_fwd.hpp
Normal file
@ -0,0 +1,68 @@
|
||||
// Boost io_fwd.hpp header file --------------------------------------------//
|
||||
|
||||
// (C) Copyright boost.org 2002. Permission to copy, use, modify, sell and
|
||||
// distribute this software is granted provided this copyright notice appears
|
||||
// in all copies. This software is provided "as is" without express or
|
||||
// implied warranty, and with no claim as to its suitability for any purpose.
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
#ifndef BOOST_IO_FWD_HPP
|
||||
#define BOOST_IO_FWD_HPP
|
||||
|
||||
#include <iosfwd> // for std::char_traits (declaration)
|
||||
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
|
||||
|
||||
// From <boost/io/ios_state.hpp> -------------------------------------------//
|
||||
|
||||
class ios_flags_saver;
|
||||
class ios_precision_saver;
|
||||
class ios_width_saver;
|
||||
class ios_base_all_saver;
|
||||
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_iostate_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_exception_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_tie_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_rdbuf_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_fill_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_locale_saver;
|
||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
||||
class basic_ios_all_saver;
|
||||
|
||||
typedef basic_ios_iostate_saver<char> ios_iostate_saver;
|
||||
typedef basic_ios_iostate_saver<wchar_t> wios_iostate_saver;
|
||||
typedef basic_ios_exception_saver<char> ios_exception_saver;
|
||||
typedef basic_ios_exception_saver<wchar_t> wios_exception_saver;
|
||||
typedef basic_ios_tie_saver<char> ios_tie_saver;
|
||||
typedef basic_ios_tie_saver<wchar_t> wios_tie_saver;
|
||||
typedef basic_ios_rdbuf_saver<char> ios_rdbuf_saver;
|
||||
typedef basic_ios_rdbuf_saver<wchar_t> wios_rdbuf_saver;
|
||||
typedef basic_ios_fill_saver<char> ios_fill_saver;
|
||||
typedef basic_ios_fill_saver<wchar_t> wios_fill_saver;
|
||||
typedef basic_ios_locale_saver<char> ios_locale_saver;
|
||||
typedef basic_ios_locale_saver<wchar_t> wios_locale_saver;
|
||||
typedef basic_ios_all_saver<char> ios_all_saver;
|
||||
typedef basic_ios_all_saver<wchar_t> wios_all_saver;
|
||||
|
||||
class ios_iword_saver;
|
||||
class ios_pword_saver;
|
||||
class ios_all_word_saver;
|
||||
|
||||
|
||||
} // namespace io
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // BOOST_IO_FWD_HPP
|
253
test/ios_state_test.cpp
Normal file
253
test/ios_state_test.cpp
Normal file
@ -0,0 +1,253 @@
|
||||
// Boost ios_state_test.cpp test file --------------------------------------//
|
||||
|
||||
// (C) Copyright Daryle Walker 2002. Permission to copy, use, modify, sell and
|
||||
// distribute this software is granted provided this copyright notice appears
|
||||
// in all copies. This software is provided "as is" without express or implied
|
||||
// warranty, and with no claim as to its suitability for any purpose.
|
||||
|
||||
// Revision History
|
||||
// 26 Feb 2002 Initial version (Daryle Walker)
|
||||
|
||||
#define BOOST_INCLUDE_MAIN
|
||||
#include <boost/test/test_tools.hpp> // main, BOOST_TEST, etc.
|
||||
|
||||
#include <boost/cstdlib.hpp> // for boost::exit_success
|
||||
#include <boost/io/ios_state.hpp> // for boost::io::ios_flags_saver, etc.
|
||||
|
||||
#include <iomanip> // for std::setw
|
||||
#include <ios> // for std::ios_base, std::streamsize, etc.
|
||||
#include <iostream> // for std::cout, etc.
|
||||
#include <istream> // for std::istream
|
||||
#include <locale> // for std::numpunct_byname, std::locale
|
||||
#include <ostream> // for std::endl, std::ostream
|
||||
#include <streambuf> // for std::streambuf
|
||||
#include <string> // for std::string
|
||||
|
||||
|
||||
// Facet with the bool names spelled backwards
|
||||
class backward_bool_names
|
||||
: public std::numpunct_byname<char>
|
||||
{
|
||||
typedef std::numpunct_byname<char> base_type;
|
||||
|
||||
public:
|
||||
explicit backward_bool_names( char const *name )
|
||||
: base_type( name )
|
||||
{}
|
||||
|
||||
protected:
|
||||
virtual string_type do_truename() const
|
||||
{ return "eurt"; }
|
||||
virtual string_type do_falsename() const
|
||||
{ return "eslaf"; }
|
||||
};
|
||||
|
||||
|
||||
// Index to test custom storage
|
||||
int const my_index = std::ios_base::xalloc();
|
||||
|
||||
// Test data
|
||||
char const test_string[] = "Hello world";
|
||||
int const test_num1 = -16;
|
||||
double const test_num2 = 34.5678901234;
|
||||
bool const test_bool = true;
|
||||
|
||||
|
||||
// Function prototypes
|
||||
void saver_tests_1( std::istream &input, std::ostream &output,
|
||||
std::ostream &err );
|
||||
void saver_tests_2( std::istream &input, std::ostream &output,
|
||||
std::ostream &err );
|
||||
|
||||
|
||||
// Test program
|
||||
int
|
||||
test_main
|
||||
(
|
||||
int , // "argc" is unused
|
||||
char * [] // "argv" is unused
|
||||
)
|
||||
{
|
||||
using std::cout;
|
||||
using std::ios_base;
|
||||
using std::streamsize;
|
||||
using std::cin;
|
||||
|
||||
cout << "The original data is:\n";
|
||||
cout << '\t' << test_string << '\n';
|
||||
cout << '\t' << test_num1 << '\n';
|
||||
cout << '\t' << test_num2 << '\n';
|
||||
cout << '\t' << std::boolalpha << test_bool << std::endl;
|
||||
|
||||
// Save states for comparison later
|
||||
ios_base::fmtflags const cout_flags = cout.flags();
|
||||
streamsize const cout_precision = cout.precision();
|
||||
streamsize const cout_width = cout.width();
|
||||
ios_base::iostate const cout_iostate = cout.rdstate();
|
||||
ios_base::iostate const cout_exceptions = cout.exceptions();
|
||||
std::ostream * const cin_tie = cin.tie();
|
||||
std::streambuf * const cout_sb = cout.rdbuf();
|
||||
char const cout_fill = cout.fill();
|
||||
std::locale const cout_locale = cout.getloc();
|
||||
|
||||
cout.iword( my_index ) = 42L;
|
||||
cout.pword( my_index ) = &cin;
|
||||
|
||||
// Run saver tests with changing separate from saving
|
||||
saver_tests_1( cin, cout, std::cerr );
|
||||
|
||||
// Check if states are back to normal
|
||||
BOOST_TEST( &cin == cout.pword(my_index) );
|
||||
BOOST_TEST( 42L == cout.iword(my_index) );
|
||||
BOOST_TEST( cout_locale == cout.getloc() );
|
||||
BOOST_TEST( cout_fill == cout.fill() );
|
||||
BOOST_TEST( cout_sb == cout.rdbuf() );
|
||||
BOOST_TEST( cin_tie == cin.tie() );
|
||||
BOOST_TEST( cout_exceptions == cout.exceptions() );
|
||||
BOOST_TEST( cout_iostate == cout.rdstate() );
|
||||
BOOST_TEST( cout_width == cout.width() );
|
||||
BOOST_TEST( cout_precision == cout.precision() );
|
||||
BOOST_TEST( cout_flags == cout.flags() );
|
||||
|
||||
// Run saver tests with combined saving and changing
|
||||
saver_tests_2( cin, cout, std::cerr );
|
||||
|
||||
// Check if states are back to normal
|
||||
BOOST_TEST( &cin == cout.pword(my_index) );
|
||||
BOOST_TEST( 42L == cout.iword(my_index) );
|
||||
BOOST_TEST( cout_locale == cout.getloc() );
|
||||
BOOST_TEST( cout_fill == cout.fill() );
|
||||
BOOST_TEST( cout_sb == cout.rdbuf() );
|
||||
BOOST_TEST( cin_tie == cin.tie() );
|
||||
BOOST_TEST( cout_exceptions == cout.exceptions() );
|
||||
BOOST_TEST( cout_iostate == cout.rdstate() );
|
||||
BOOST_TEST( cout_width == cout.width() );
|
||||
BOOST_TEST( cout_precision == cout.precision() );
|
||||
BOOST_TEST( cout_flags == cout.flags() );
|
||||
|
||||
return boost::exit_success;
|
||||
}
|
||||
|
||||
// Save, change, and restore stream properties
|
||||
void
|
||||
saver_tests_1
|
||||
(
|
||||
std::istream & input,
|
||||
std::ostream & output,
|
||||
std::ostream & err
|
||||
)
|
||||
{
|
||||
using std::locale;
|
||||
using std::ios_base;
|
||||
using std::setw;
|
||||
|
||||
boost::io::ios_flags_saver const ifls( output );
|
||||
boost::io::ios_precision_saver const iprs( output );
|
||||
boost::io::ios_width_saver const iws( output );
|
||||
boost::io::ios_tie_saver const its( input );
|
||||
boost::io::ios_rdbuf_saver const irs( output );
|
||||
boost::io::ios_fill_saver const ifis( output );
|
||||
boost::io::ios_locale_saver const ils( output );
|
||||
boost::io::ios_iword_saver const iis( output, my_index );
|
||||
boost::io::ios_pword_saver const ipws( output, my_index );
|
||||
|
||||
locale loc( locale(""), new backward_bool_names("") );
|
||||
|
||||
input.tie( &err );
|
||||
output.rdbuf( err.rdbuf() );
|
||||
output.iword( my_index ) = 69L;
|
||||
output.pword( my_index ) = &err;
|
||||
|
||||
output << "The data is (again):\n";
|
||||
output.setf( ios_base::showpos | ios_base::boolalpha );
|
||||
output.setf( ios_base::internal, ios_base::adjustfield );
|
||||
output.fill( '@' );
|
||||
output.precision( 9 );
|
||||
output << '\t' << test_string << '\n';
|
||||
output << '\t' << setw( 10 ) << test_num1 << '\n';
|
||||
output << '\t' << setw( 15 ) << test_num2 << '\n';
|
||||
output.imbue( loc );
|
||||
output << '\t' << test_bool << '\n';
|
||||
|
||||
BOOST_TEST( &err == output.pword(my_index) );
|
||||
BOOST_TEST( 69L == output.iword(my_index) );
|
||||
|
||||
try
|
||||
{
|
||||
boost::io::ios_exception_saver const ies( output );
|
||||
boost::io::ios_iostate_saver const iis( output );
|
||||
|
||||
output.exceptions( ios_base::eofbit );
|
||||
output.setstate( ios_base::eofbit );
|
||||
BOOST_ERROR( "previous line should have thrown" );
|
||||
}
|
||||
catch ( ios_base::failure &f )
|
||||
{
|
||||
err << "Got the expected I/O failure: \"" << f.what() << "\".\n";
|
||||
BOOST_TEST( output.exceptions() == ios_base::goodbit );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
err << "Got an unknown error when doing exception test!\n";
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
// Save & change and restore stream properties
|
||||
void
|
||||
saver_tests_2
|
||||
(
|
||||
std::istream & input,
|
||||
std::ostream & output,
|
||||
std::ostream & err
|
||||
)
|
||||
{
|
||||
using std::locale;
|
||||
using std::ios_base;
|
||||
|
||||
boost::io::ios_tie_saver const its( input, &err );
|
||||
boost::io::ios_rdbuf_saver const irs( output, err.rdbuf() );
|
||||
boost::io::ios_iword_saver const iis( output, my_index, 69L );
|
||||
boost::io::ios_pword_saver const ipws( output, my_index, &err );
|
||||
output << "The data is (a third time; adding the numbers):\n";
|
||||
|
||||
boost::io::ios_flags_saver const ifls( output, (output.flags()
|
||||
& ~ios_base::adjustfield) | ios_base::showpos | ios_base::boolalpha
|
||||
| (ios_base::internal & ios_base::adjustfield) );
|
||||
boost::io::ios_precision_saver const iprs( output, 9 );
|
||||
boost::io::ios_fill_saver const ifis( output, '@' );
|
||||
output << '\t' << test_string << '\n';
|
||||
|
||||
boost::io::ios_width_saver const iws( output, 12 );
|
||||
output.put( '\t' );
|
||||
output << test_num1 + test_num2;
|
||||
output.put( '\n' );
|
||||
|
||||
locale loc( locale(""),
|
||||
new backward_bool_names("") );
|
||||
|
||||
boost::io::ios_locale_saver const ils( output, loc );
|
||||
output << '\t' << test_bool << '\n';
|
||||
|
||||
BOOST_TEST( &err == output.pword(my_index) );
|
||||
BOOST_TEST( 69L == output.iword(my_index) );
|
||||
|
||||
try
|
||||
{
|
||||
boost::io::ios_exception_saver const ies( output, ios_base::eofbit );
|
||||
boost::io::ios_iostate_saver const iis( output, output.rdstate()
|
||||
| ios_base::eofbit );
|
||||
|
||||
BOOST_ERROR( "previous line should have thrown" );
|
||||
}
|
||||
catch ( ios_base::failure &f )
|
||||
{
|
||||
err << "Got the expected I/O failure: \"" << f.what() << "\".\n";
|
||||
BOOST_TEST( output.exceptions() == ios_base::goodbit );
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
err << "Got an unknown error when doing exception test!\n";
|
||||
throw;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user