From 9a7bf6c9c4ffc9bc8e711a1f034e170dced45823 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 9 May 2002 01:10:05 +0000 Subject: [PATCH] Initial commit [SVN r13766] --- .gitattributes | 96 +++++++++ doc/index.html | 62 ++++++ doc/ios_state.html | 341 +++++++++++++++++++++++++++++++ include/boost/io/ios_state.hpp | 361 +++++++++++++++++++++++++++++++++ include/boost/io_fwd.hpp | 68 +++++++ test/ios_state_test.cpp | 253 +++++++++++++++++++++++ 6 files changed, 1181 insertions(+) create mode 100644 .gitattributes create mode 100644 doc/index.html create mode 100644 doc/ios_state.html create mode 100644 include/boost/io/ios_state.hpp create mode 100644 include/boost/io_fwd.hpp create mode 100644 test/ios_state_test.cpp diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..3e84d7c --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/doc/index.html b/doc/index.html new file mode 100644 index 0000000..a67b629 --- /dev/null +++ b/doc/index.html @@ -0,0 +1,62 @@ + + + +Boost I/O Library + + + + + + + + + + + + +
c++boost.gif (8819 bytes)HomeLibrariesPeopleFAQMore
+ +

Boost Input/Output Library

+ + + + + + + + + + + + + + +
Header / DocsContents
+ <boost/io_fwd.hpp> + + Forward declaration header. +
+ <boost/io/ios_state.hpp>
+
documentation +
+ State-saving classes for various IOStream attributes. +
+ +

Rationale

+ +

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.

+ +
+ +

Revised: 26 Feb 2002

+ + \ No newline at end of file diff --git a/doc/ios_state.html b/doc/ios_state.html new file mode 100644 index 0000000..a09bea2 --- /dev/null +++ b/doc/ios_state.html @@ -0,0 +1,341 @@ + + + +I/O Stream-State Saver Library + + +

c++boost.gif (8819 bytes)Header <boost/io/ios_state.hpp>

+ +

The header boost/io/ios_state.hpp covers saving the stream state of objects in the C++ IOStreams system.

+ +

Contents

+ +
    +
  1. Contents +
  2. Rationale +
  3. Header Synopsis +
  4. Savers for Basic Standard Attributes
  5. +
  6. Savers for Advanced Standard Attributes
  7. +
  8. Savers for User-Defined Attributes
  9. +
  10. Savers for Combined Attributes
  11. +
  12. Example
  13. +
  14. References +
  15. Credits + +
+ +

Rationale

+ +

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.

+ +

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.

+ +
#include <ostream>
+#include <ios>
+
+void  hex_my_byte( std::ostream &os, char byte )
+{
+    os << std::hex << static_cast<unsigned>(byte);
+}
+
+ +

The os stream will retain its new hexadecimal printing mode after the call to hex_my_byte. 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.

+ +

See the example below for better code, using a saver +class.

+ +

Header Synopsis

+ +
#include <iosfwd>  // for std::char_traits (declaration)
+
+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;
+
+}
+}
+
+ +

Savers for Basic Standard Attributes

+ +

The basic saver classes have this format:

+ +
class saver_class
+{
+    typedef std::ios_base           state_type;
+    typedef implementation_defined  aspect_type;
+
+    explicit  saver_class( state_type &s );
+              saver_class( state_type &s, aspect_type const &new_value );
+             ~saver_class();
+};
+
+ +

The state_type is the IOStreams base class std::ios_base. 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 aspect_type value given. The destructor changes the stream's attribute back to the saved value.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Basic IOStreams State Saver Classes
ClassSaved AttributeAttribute TypeReading MethodWriting Method
boost::io::ios_flags_saverFormat control flagsstd::ios_base::fmtflagsflagsflags
boost::io::ios_precision_saverNumber of digits to print after decimal pointstd::streamsizeprecisionprecision
boost::io::ios_width_saverMinimum field width for printing objectsstd::streamsizewidthwidth
+ +

Savers for Advanced Standard Attributes

+ +

The saver class templates have this format:

+ +
template < typename Ch, class Tr >
+class saver_class
+{
+    typedef std::basic_ios<Ch, Tr>  state_type;
+    typedef implementation_defined  aspect_type;
+
+    explicit  saver_class( state_type &s );
+              saver_class( state_type &s, aspect_type const &new_value );
+             ~saver_class();
+};
+
+ +

The state_type is a version of the IOStreams base class template std::basic_ios<Ch, Tr>, where Ch is a character type and Tr 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 aspect_type value given. The destructor changes the stream's attribute back to the saved value.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Advanced IOStreams State Saver Class Templates
Class TemplateSaved AttributeAttribute TypeReading MethodWriting Method
boost::io::basic_ios_iostate_saver<Ch, Tr>Success state of the streamstd::ios_base::iostaterdstateclear
boost::io::basic_ios_exception_saver<Ch, Tr>Which success states trigger an exception [1]std::ios_base::iostateexceptionsexceptions
boost::io::basic_ios_tie_saver<Ch, Tr>Output stream synchronized with the streamstd::basic_ostream<Ch, Tr> *tietie
boost::io::basic_ios_rdbuf_saver<Ch, Tr>Stream buffer associated with the streamstd::basic_streambuf<Ch, Tr> *rdbufrdbuf
boost::io::basic_ios_fill_saver<Ch, Tr>Character used to pad oversized field widthsChfillfill
boost::io::basic_ios_locale_saver<Ch, Tr>Locale information associated with the stream [2]std::localegetloc (from std::ios_base)imbue (from std::basic_ios<Ch, Tr>)
+ +

Notes

+ +
    +
  1. 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 destructor of this class template may throw.
  2. +
  3. The saver for the locale uses the std::basic_ios<Ch, Tr> class to extract their information, although it could have used the functionality in std::ios_base. The problem is that the versions of the needed member functions in ios_base are not + polymorphicly related to the ones in basic_ios. 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 basic_ios.
  4. +
+ +

Savers for User-Defined Attributes

+ +

The saver classes for user-defined formatting information have this format:

+ +
#include <iosfwd>  // for std::ios_base (declaration)
+
+class saver_class
+{
+    typedef std::ios_base           state_type;
+    typedef int                     index_type;
+    typedef implementation_defined  aspect_type;
+
+    explicit  saver_class( state_type &s, index_type i );
+              saver_class( state_type &s, index_type i, aspect_type const &new_value );
+             ~saver_class();
+};
+
+ +

The index i differentiates between specific user-defined formatting attributes. The index can only be determined at run-time (most likely with the class-static std::ios_base::xalloc member function).

+ +

The state_type is the base class of the IOStreams system, std::ios_base. 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 aspect_type value given. The destructor changes the stream's attribute back to the saved value.

+ + + + + + + + + + + + + + + + + + + + + +
IOStream User-Defined State Saver Classes
ClassSaved AttributeAttribute TypeReference Method
boost::io::ios_iword_saverNumeric user-defined format flaglongiword
boost::io::ios_pword_saverPointer user-defined format flagvoid *pword
+ +

Savers for Combined Attributes

+ +

There are three class (templates) for combined attribute savers. The boost:io::ios_base_all_saver 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 boost::io::basic_ios_all_saver 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 boost::io::ios_all_word_saver 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.

+ +

Example

+ +

The code used in the rationale 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.

+ +
#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' );
+    }
+
+    //...
+}
+
+ +

References

+ + + +

Credits

+ +

Contributors

+ +
+
Daryle Walker +
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 ios_state_test.cpp. +
+ +

History

+ +
+
13 Mar 2002, Daryle Walker +
Initial version +
+ +
+ +

Revised: 13 March 2002

+ +

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.

+ + \ No newline at end of file diff --git a/include/boost/io/ios_state.hpp b/include/boost/io/ios_state.hpp new file mode 100644 index 0000000..c39c7a1 --- /dev/null +++ b/include/boost/io/ios_state.hpp @@ -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 // self include + +#include // for std::ios_base, std::basic_ios, etc. +#include // for std::locale +#include // for std::basic_ostream +#include // for std::basic_streambuf +#include // 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 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 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 state_type; + typedef ::std::basic_ostream * 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 state_type; + typedef ::std::basic_streambuf * 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 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 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 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 * const a6_save_; + ::std::basic_streambuf * 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 diff --git a/include/boost/io_fwd.hpp b/include/boost/io_fwd.hpp new file mode 100644 index 0000000..8a0eded --- /dev/null +++ b/include/boost/io_fwd.hpp @@ -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 // for std::char_traits (declaration) + + +namespace boost +{ +namespace io +{ + + +// From -------------------------------------------// + +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 > + class basic_ios_iostate_saver; +template < typename Ch, class Tr = ::std::char_traits > + class basic_ios_exception_saver; +template < typename Ch, class Tr = ::std::char_traits > + class basic_ios_tie_saver; +template < typename Ch, class Tr = ::std::char_traits > + class basic_ios_rdbuf_saver; +template < typename Ch, class Tr = ::std::char_traits > + class basic_ios_fill_saver; +template < typename Ch, class Tr = ::std::char_traits > + class basic_ios_locale_saver; +template < typename Ch, class Tr = ::std::char_traits > + class basic_ios_all_saver; + +typedef basic_ios_iostate_saver ios_iostate_saver; +typedef basic_ios_iostate_saver wios_iostate_saver; +typedef basic_ios_exception_saver ios_exception_saver; +typedef basic_ios_exception_saver wios_exception_saver; +typedef basic_ios_tie_saver ios_tie_saver; +typedef basic_ios_tie_saver wios_tie_saver; +typedef basic_ios_rdbuf_saver ios_rdbuf_saver; +typedef basic_ios_rdbuf_saver wios_rdbuf_saver; +typedef basic_ios_fill_saver ios_fill_saver; +typedef basic_ios_fill_saver wios_fill_saver; +typedef basic_ios_locale_saver ios_locale_saver; +typedef basic_ios_locale_saver wios_locale_saver; +typedef basic_ios_all_saver ios_all_saver; +typedef basic_ios_all_saver 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 diff --git a/test/ios_state_test.cpp b/test/ios_state_test.cpp new file mode 100644 index 0000000..b23697b --- /dev/null +++ b/test/ios_state_test.cpp @@ -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 // main, BOOST_TEST, etc. + +#include // for boost::exit_success +#include // for boost::io::ios_flags_saver, etc. + +#include // for std::setw +#include // for std::ios_base, std::streamsize, etc. +#include // for std::cout, etc. +#include // for std::istream +#include // for std::numpunct_byname, std::locale +#include // for std::endl, std::ostream +#include // for std::streambuf +#include // for std::string + + +// Facet with the bool names spelled backwards +class backward_bool_names + : public std::numpunct_byname +{ + typedef std::numpunct_byname 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; + } +}