Merge branch 'develop'

This commit is contained in:
Glen Fernandes
2019-12-17 20:36:21 -05:00
4 changed files with 529 additions and 843 deletions

View File

@ -109,13 +109,14 @@ The basic saver classes have this format:
[subs=+quotes] [subs=+quotes]
``` ```
class saver_class { class saver {
public:
typedef std::ios_base state_type; typedef std::ios_base state_type;
typedef `implementation_defined` aspect_type; typedef `implementation_defined` aspect_type;
explicit saver_class(state_type& s); explicit saver(state_type& s);
saver_class(state_type& s, const aspect_type& new_value); saver(state_type& s, const aspect_type& new_value);
~saver_class(); ~saver();
void restore(); void restore();
}; };
@ -159,13 +160,14 @@ The saver class templates have this format:
[subs=+quotes] [subs=+quotes]
``` ```
template<class Ch, class Tr> template<class Ch, class Tr>
class saver_class { class saver {
public:
typedef std::basic_ios<Ch, Tr> state_type; typedef std::basic_ios<Ch, Tr> state_type;
typedef `implementation-defined` aspect_type; typedef `implementation-defined` aspect_type;
explicit saver_class(state_type& s); explicit saver(state_type& s);
saver_class(state_type& s, const aspect_type& new_value); saver(state_type& s, const aspect_type& new_value);
~saver_class(); ~saver();
void restore(); void restore();
}; };
@ -259,7 +261,7 @@ 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. 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 Or the calling function can surround the call with a saver. Or both can be
done, especially if the user does not know if the printing function uses a done, especially if the user does not know if the printing function uses a
state saver. If the user wants a series of changes back & forth, without state saver. If the user wants a series of changes back and forth, without
surrounding each change within a separate block, the restore member function surrounding each change within a separate block, the restore member function
can be called between each trial. can be called between each trial.

View File

@ -1,67 +1,63 @@
// Boost io_fwd.hpp header file --------------------------------------------// /*
Copyright 2002 Daryle Walker
// Copyright 2002 Daryle Walker. Use, modification, and distribution are subject
// to the Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// See <http://www.boost.org/libs/io/> for the library's home page.
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_IO_FWD_HPP #ifndef BOOST_IO_FWD_HPP
#define BOOST_IO_FWD_HPP #define BOOST_IO_FWD_HPP
#include <iosfwd> // for std::char_traits (declaration) #include <iosfwd>
namespace boost {
namespace boost namespace io {
{
namespace io
{
// From <boost/io/ios_state.hpp> -------------------------------------------//
class ios_flags_saver; class ios_flags_saver;
class ios_precision_saver; class ios_precision_saver;
class ios_width_saver; class ios_width_saver;
class ios_base_all_saver; class ios_base_all_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> > template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_iostate_saver; 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; template<class Ch, class Tr = std::char_traits<Ch> >
typedef basic_ios_iostate_saver<wchar_t> wios_iostate_saver; class basic_ios_exception_saver;
typedef basic_ios_exception_saver<char> ios_exception_saver;
typedef basic_ios_exception_saver<wchar_t> wios_exception_saver; template<class Ch, class Tr = std::char_traits<Ch> >
typedef basic_ios_tie_saver<char> ios_tie_saver; class basic_ios_tie_saver;
typedef basic_ios_tie_saver<wchar_t> wios_tie_saver;
typedef basic_ios_rdbuf_saver<char> ios_rdbuf_saver; template<class Ch, class Tr = std::char_traits<Ch> >
typedef basic_ios_rdbuf_saver<wchar_t> wios_rdbuf_saver; class basic_ios_rdbuf_saver;
typedef basic_ios_fill_saver<char> ios_fill_saver;
typedef basic_ios_fill_saver<wchar_t> wios_fill_saver; template<class Ch, class Tr = std::char_traits<Ch> >
typedef basic_ios_locale_saver<char> ios_locale_saver; class basic_ios_fill_saver;
typedef basic_ios_locale_saver<wchar_t> wios_locale_saver;
typedef basic_ios_all_saver<char> ios_all_saver; template<class Ch, class Tr = std::char_traits<Ch> >
typedef basic_ios_all_saver<wchar_t> wios_all_saver; class basic_ios_locale_saver;
template<class 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_iword_saver;
class ios_pword_saver; class ios_pword_saver;
class ios_all_word_saver; class ios_all_word_saver;
} /* io */
} /* boost */
} // namespace io #endif
} // namespace boost
#endif // BOOST_IO_FWD_HPP

View File

@ -1,264 +1,171 @@
// Boost ios_state_test.cpp test file --------------------------------------// /*
Copyright 2002, 2003 Daryle Walker
// Copyright 2002, 2003 Daryle Walker. Use, modification, and distribution are Copyright 2019 Glen Joseph Fernandes
// subject to the Boost Software License, Version 1.0. (See accompanying file (glenjofe@gmail.com)
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// Copyright 2019 Glen Joseph Fernandes Distributed under the Boost Software License, Version 1.0.
// (glenjofe@gmail.com) (http://www.boost.org/LICENSE_1_0.txt)
*/
// See <http://www.boost.org/libs/io/> for the library's home page. #include <boost/io/ios_state.hpp>
// Revision History
// 15 Jun 2003 Adjust to changes in Boost.Test (Daryle Walker)
// 26 Feb 2002 Initial version (Daryle Walker)
#include <boost/config.hpp>
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/io/ios_state.hpp> // for boost::io::ios_flags_saver, etc. #include <iomanip>
#include <ios>
#include <cstddef> // for std::size_t #include <istream>
#include <iomanip> // for std::setw #include <locale>
#include <ios> // for std::ios_base, std::streamsize, etc. #include <ostream>
#include <iostream> // for std::cout, etc. #include <sstream>
#include <istream> // for std::istream
#include <locale> // for std::numpunct, std::locale
#include <ostream> // for std::endl, std::ostream
#include <streambuf> // for std::streambuf
#include <string> // for std::string
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
#include <stdexcept> #include <stdexcept>
#endif #endif
#include <streambuf>
#include <string>
#include <cstddef>
// Facet with the bool names spelled backwards
class backward_bool_names class backward_bool_names
: public std::numpunct<char> : public std::numpunct<char> {
{ typedef std::numpunct<char> base_type;
typedef std::numpunct<char> base_type;
public: public:
explicit backward_bool_names( std::size_t refs = 0 ) explicit backward_bool_names(std::size_t refs = 0)
: base_type( refs ) : base_type( refs ) { }
{}
protected: protected:
virtual ~backward_bool_names() {} ~backward_bool_names() { }
virtual base_type::string_type do_truename() const base_type::string_type do_truename() const {
{ return "eurt"; } return "eurt";
virtual base_type::string_type do_falsename() const }
{ return "eslaf"; }
base_type::string_type do_falsename() const {
return "eslaf";
}
}; };
void saver_tests_1(int index,
std::istream& input,
std::ostream& output,
std::ostream& err)
{
boost::io::ios_flags_saver ifls(output);
boost::io::ios_precision_saver iprs(output);
boost::io::ios_width_saver iws(output);
boost::io::ios_tie_saver its(input);
boost::io::ios_rdbuf_saver irs(output);
boost::io::ios_fill_saver ifis(output);
boost::io::ios_locale_saver ils(output);
boost::io::ios_iword_saver iis(output, index);
boost::io::ios_pword_saver ipws(output, index);
std::locale loc(std::locale::classic(), new backward_bool_names);
input.tie(&err);
output.rdbuf(err.rdbuf());
output.iword(index) = 69L;
output.pword(index) = &err;
output.setf(std::ios_base::showpos | std::ios_base::boolalpha);
output.setf(std::ios_base::internal, std::ios_base::adjustfield);
output.fill('@');
output.precision( 9 );
output << "Hello world";
output << std::setw(10) << -16;
output << std::setw(15) << 34.5678901234;
output.imbue(loc);
output << true;
BOOST_TEST(&err == output.pword(index));
BOOST_TEST(69L == output.iword(index));
try {
boost::io::ios_exception_saver ies(output);
boost::io::ios_iostate_saver iis(output);
output.exceptions(std::ios_base::eofbit | std::ios_base::badbit);
output.setstate(std::ios_base::eofbit);
BOOST_ERROR("previous line should have thrown");
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
} catch (std::exception&) {
#else
} catch (std::ios_base::failure&) {
#endif
BOOST_TEST(output.exceptions() == std::ios_base::goodbit );
}
}
// Index to test custom storage void saver_tests_2(int index,
int const my_index = std::ios_base::xalloc(); std::istream& input,
std::ostream& output,
std::ostream& err)
{
boost::io::ios_tie_saver its(input, &err);
boost::io::ios_rdbuf_saver irs(output, err.rdbuf());
boost::io::ios_iword_saver iis(output, index, 69L);
boost::io::ios_pword_saver ipws(output, index, &err);
boost::io::ios_flags_saver ifls(output,
(output.flags() & ~std::ios_base::adjustfield) |
std::ios_base::showpos |
std::ios_base::boolalpha |
(std::ios_base::internal & std::ios_base::adjustfield));
boost::io::ios_precision_saver iprs(output, 9);
boost::io::ios_fill_saver ifis(output, '@');
output << "Hello world";
boost::io::ios_width_saver iws(output, 12);
output << -16 + 34.5678901234;
std::locale loc(std::locale::classic(), new backward_bool_names);
boost::io::ios_locale_saver ils(output, loc);
output << true;
BOOST_TEST(&err == output.pword(index));
BOOST_TEST(69L == output.iword(index));
try {
boost::io::ios_exception_saver ies(output, std::ios_base::eofbit);
boost::io::ios_iostate_saver iis(output,
output.rdstate() | std::ios_base::eofbit );
BOOST_ERROR("previous line should have thrown");
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
} catch (std::exception&) {
#else
} catch (std::ios_base::failure&) {
#endif
BOOST_TEST(output.exceptions() == std::ios_base::goodbit);
}
}
// 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 main() int main()
{ {
using std::cout; int index = std::ios_base::xalloc();
using std::ios_base; std::ostringstream out;
using std::streamsize; std::ostringstream err;
using std::cin; std::istringstream in;
std::ios_base::fmtflags out_flags = out.flags();
cout << "The original data is:\n"; std::streamsize out_precision = out.precision();
cout << '\t' << test_string << '\n'; std::streamsize out_width = out.width();
cout << '\t' << test_num1 << '\n'; std::ios_base::iostate out_iostate = out.rdstate();
cout << '\t' << test_num2 << '\n'; std::ios_base::iostate out_exceptions = out.exceptions();
cout << '\t' << std::boolalpha << test_bool << std::endl; std::ostream* in_tie = in.tie();
std::streambuf* out_sb = out.rdbuf();
// Save states for comparison later char out_fill = out.fill();
ios_base::fmtflags const cout_flags = cout.flags(); std::locale out_locale = out.getloc();
streamsize const cout_precision = cout.precision(); out.iword(index) = 42L;
streamsize const cout_width = cout.width(); out.pword(index) = &in;
ios_base::iostate const cout_iostate = cout.rdstate(); saver_tests_1(index, in, out, err);
ios_base::iostate const cout_exceptions = cout.exceptions(); BOOST_TEST(&in == out.pword(index));
std::ostream * const cin_tie = cin.tie(); BOOST_TEST(42L == out.iword(index));
std::streambuf * const cout_sb = cout.rdbuf(); BOOST_TEST(out_locale == out.getloc());
char const cout_fill = cout.fill(); BOOST_TEST(out_fill == out.fill());
std::locale const cout_locale = cout.getloc(); BOOST_TEST(out_sb == out.rdbuf());
BOOST_TEST(in_tie == in.tie());
cout.iword( my_index ) = 42L; BOOST_TEST(out_exceptions == out.exceptions());
cout.pword( my_index ) = &cin; BOOST_TEST(out_iostate == out.rdstate());
BOOST_TEST(out_width == out.width());
// Run saver tests with changing separate from saving BOOST_TEST(out_precision == out.precision());
saver_tests_1( cin, cout, std::cerr ); BOOST_TEST(out_flags == out.flags());
saver_tests_2(index, in, out, err);
// Check if states are back to normal BOOST_TEST(&in == out.pword(index));
BOOST_TEST( &cin == cout.pword(my_index) ); BOOST_TEST(42L == out.iword(index));
BOOST_TEST( 42L == cout.iword(my_index) ); BOOST_TEST(out_locale == out.getloc());
BOOST_TEST( cout_locale == cout.getloc() ); BOOST_TEST(out_fill == out.fill());
BOOST_TEST( cout_fill == cout.fill() ); BOOST_TEST(out_sb == out.rdbuf());
BOOST_TEST( cout_sb == cout.rdbuf() ); BOOST_TEST(in_tie == in.tie());
BOOST_TEST( cin_tie == cin.tie() ); BOOST_TEST(out_exceptions == out.exceptions());
BOOST_TEST( cout_exceptions == cout.exceptions() ); BOOST_TEST(out_iostate == out.rdstate());
BOOST_TEST( cout_iostate == cout.rdstate() ); BOOST_TEST(out_width == out.width());
BOOST_TEST( cout_width == cout.width() ); BOOST_TEST(out_precision == out.precision());
BOOST_TEST( cout_precision == cout.precision() ); BOOST_TEST(out_flags == out.flags());
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::report_errors(); return boost::report_errors();
} }
// 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::classic(), 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 | ios_base::badbit );
output.setstate( ios_base::eofbit );
BOOST_ERROR( "previous line should have thrown" );
}
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
catch ( std::exception &f )
#else
catch ( ios_base::failure &f )
#endif
{
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::classic(),
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" );
}
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
catch ( std::exception &f )
#else
catch ( ios_base::failure &f )
#endif
{
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;
}
}

File diff suppressed because it is too large Load Diff