From: Benjamin Kosnik Date: Thu, 24 May 2001 23:09:53 +0000 (+0000) Subject: basic_ios.tcc: Small tweak. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=d422980b3f3d50a34f91fdf5c843b14d3f59fc4c;p=gcc.git basic_ios.tcc: Small tweak. 2001-05-24 Benjamin Kosnik libstdc++/2832 * include/bits/basic_ios.tcc: Small tweak. * include/bits/std_fstream.h (ifstream): Add buffer member. Adjust ctors and dtors, and rdbuf settings. (ofstream): Same. (fstream): Same. * include/bits/std_sstream.h: Same, but for stringstream classes. * testsuite/27_io/ostringstream_members.cc: New. * testsuite/27_io/stringstream_members.cc: New. * testsuite/27_io/fstream_members.cc: New. * testsuite/27_io/ifstream_members.cc: Add test. * testsuite/27_io/istringstream_members.cc: Add test. * testsuite/27_io/ofstream_members.cc: Add test. From-SVN: r42547 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c69a31add8b..afe93e7c823 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,19 @@ +2001-05-24 Benjamin Kosnik + + libstdc++/2832 + * include/bits/basic_ios.tcc: Small tweak. + * include/bits/std_fstream.h (ifstream): Add buffer member. Adjust + ctors and dtors, and rdbuf settings. + (ofstream): Same. + (fstream): Same. + * include/bits/std_sstream.h: Same, but for stringstream classes. + * testsuite/27_io/ostringstream_members.cc: New. + * testsuite/27_io/stringstream_members.cc: New. + * testsuite/27_io/fstream_members.cc: New. + * testsuite/27_io/ifstream_members.cc: Add test. + * testsuite/27_io/istringstream_members.cc: Add test. + * testsuite/27_io/ofstream_members.cc: Add test. + 2001-05-24 Gabriel Dos Reis * include/bits/c++config(__NO_MATH_INLINES): Move to... diff --git a/libstdc++-v3/include/bits/basic_ios.tcc b/libstdc++-v3/include/bits/basic_ios.tcc index d561b3bcbe0..f1b634744c8 100644 --- a/libstdc++-v3/include/bits/basic_ios.tcc +++ b/libstdc++-v3/include/bits/basic_ios.tcc @@ -132,6 +132,6 @@ namespace std { } // namespace std -#endif /* _CPP_BITS_BASICIOS_TCC */ +#endif // _CPP_BITS_BASICIOS_TCC diff --git a/libstdc++-v3/include/bits/std_fstream.h b/libstdc++-v3/include/bits/std_fstream.h index 83f31dc0c5a..a06721f6cf8 100644 --- a/libstdc++-v3/include/bits/std_fstream.h +++ b/libstdc++-v3/include/bits/std_fstream.h @@ -244,41 +244,45 @@ namespace std typedef basic_filebuf __filebuf_type; typedef basic_istream __istream_type; - // Constructors/Destructors: + private: + __filebuf_type _M_filebuf; + + public: + // Constructors/Destructors: basic_ifstream() - : __istream_type(new __filebuf_type()) - { } + : __istream_type(NULL), _M_filebuf() + { this->init(&_M_filebuf); } explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) - : __istream_type(new __filebuf_type()) - { this->open(__s, __mode); } - - ~basic_ifstream() + : __istream_type(NULL), _M_filebuf() { - delete _M_streambuf; - _M_streambuf = NULL; + this->init(&_M_filebuf); + this->open(__s, __mode); } + + ~basic_ifstream() + { } // Members: __filebuf_type* rdbuf() const - { return static_cast<__filebuf_type*>(_M_streambuf); } + { return const_cast<__filebuf_type*>(&_M_filebuf); } bool - is_open(void) { return rdbuf()->is_open(); } + is_open(void) { return _M_filebuf.is_open(); } void open(const char* __s, ios_base::openmode __mode = ios_base::in) { - if (rdbuf()->open(__s, __mode | ios_base::in) == NULL) + if (_M_filebuf.open(__s, __mode | ios_base::in) == NULL) this->setstate(ios_base::failbit); } void close(void) { - if (!rdbuf()->close()) + if (!_M_filebuf.close()) this->setstate(ios_base::failbit); } }; @@ -300,43 +304,47 @@ namespace std typedef basic_filebuf __filebuf_type; typedef basic_ostream __ostream_type; + private: + __filebuf_type _M_filebuf; + + public: // Constructors: basic_ofstream() - : __ostream_type(new __filebuf_type()) - { } + : __ostream_type(NULL), _M_filebuf() + { this->init(&_M_filebuf); } explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out|ios_base::trunc) - : __ostream_type(new __filebuf_type()) - { this->open(__s, __mode); } - - ~basic_ofstream() + : __ostream_type(NULL), _M_filebuf() { - delete _M_streambuf; - _M_streambuf = NULL; + this->init(&_M_filebuf); + this->open(__s, __mode); } + ~basic_ofstream() + { } + // Members: __filebuf_type* rdbuf(void) const - { return static_cast<__filebuf_type*>(_M_streambuf); } + { return const_cast<__filebuf_type*>(&_M_filebuf); } bool - is_open(void) { return rdbuf()->is_open(); } + is_open(void) { return _M_filebuf.is_open(); } void open(const char* __s, ios_base::openmode __mode = ios_base::out | ios_base::trunc) { - if (!rdbuf()->open(__s, __mode | ios_base::out)) + if (!_M_filebuf.open(__s, __mode | ios_base::out)) this->setstate(ios_base::failbit); } void close(void) { - if (!rdbuf()->close()) + if (!_M_filebuf.close()) setstate(ios_base::failbit); } }; @@ -359,44 +367,48 @@ namespace std typedef basic_ios __ios_type; typedef basic_iostream __iostream_type; + private: + __filebuf_type _M_filebuf; + + public: // Constructors/destructor: basic_fstream() - : __iostream_type(new __filebuf_type()) - { } + : __iostream_type(NULL), _M_filebuf() + { this->init(&_M_filebuf); } explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out) - : __iostream_type(new __filebuf_type()) - { this->open(__s, __mode); } - - ~basic_fstream() + : __iostream_type(NULL), _M_filebuf() { - delete _M_streambuf; - _M_streambuf = NULL; + this->init(&_M_filebuf); + this->open(__s, __mode); } + + ~basic_fstream() + { } // Members: __filebuf_type* rdbuf(void) const - { return static_cast<__filebuf_type*>(_M_streambuf); } + { return const_cast<__filebuf_type*>(&_M_filebuf); } bool - is_open(void) { return rdbuf()->is_open(); } + is_open(void) { return _M_filebuf.is_open(); } void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out) { - if (!rdbuf()->open(__s, __mode)) - setstate (ios_base::failbit); + if (!_M_filebuf.open(__s, __mode)) + setstate(ios_base::failbit); } void close(void) { - if (!rdbuf()->close()) - setstate (ios_base::failbit); + if (!_M_filebuf.close()) + setstate(ios_base::failbit); } }; } // namespace std diff --git a/libstdc++-v3/include/bits/std_sstream.h b/libstdc++-v3/include/bits/std_sstream.h index d03bdfa100d..ca347dc326c 100644 --- a/libstdc++-v3/include/bits/std_sstream.h +++ b/libstdc++-v3/include/bits/std_sstream.h @@ -203,37 +203,37 @@ namespace std typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; typedef basic_istream __istream_type; + private: + __stringbuf_type _M_stringbuf; + + public: // Constructors: explicit basic_istringstream(ios_base::openmode __mode = ios_base::in) - : __istream_type(new __stringbuf_type(__mode | ios_base::in)) - { } + : __istream_type(NULL), _M_stringbuf(__mode | ios_base::in) + { this->init(&_M_stringbuf); } explicit basic_istringstream(const __string_type& __str, ios_base::openmode __mode = ios_base::in) - : __istream_type(new __stringbuf_type(__str, __mode | ios_base::in)) - { } + : __istream_type(NULL), _M_stringbuf(__str, __mode | ios_base::in) + { this->init(&_M_stringbuf); } ~basic_istringstream() - { - delete _M_streambuf; - _M_streambuf = NULL; - } + { } // Members: __stringbuf_type* rdbuf() const - { return static_cast<__stringbuf_type*>(_M_streambuf); } + { return const_cast<__stringbuf_type*>(&_M_stringbuf); } __string_type str() const - { return this->rdbuf()->str(); } + { return _M_stringbuf.str(); } void str(const __string_type& __s) - { rdbuf()->str(__s); } - + { _M_stringbuf.str(__s); } }; @@ -254,37 +254,37 @@ namespace std typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; typedef basic_ostream __ostream_type; - // Constructors/destructor: + private: + __stringbuf_type _M_stringbuf; + + public: + // Constructors/destructor: explicit basic_ostringstream(ios_base::openmode __mode = ios_base::out) - : __ostream_type(new __stringbuf_type(__mode | ios_base::out)) - { } + : __ostream_type(NULL), _M_stringbuf(__mode | ios_base::out) + { this->init(&_M_stringbuf); } explicit basic_ostringstream(const __string_type __str, ios_base::openmode __mode = ios_base::out) - : __ostream_type(new __stringbuf_type(__str, __mode | ios_base::out)) - { } + : __ostream_type(NULL), _M_stringbuf(__str, __mode | ios_base::out) + { this->init(&_M_stringbuf); } ~basic_ostringstream() - { - delete _M_streambuf; - _M_streambuf = NULL; - } + { } // Members: __stringbuf_type* rdbuf() const - { return static_cast<__stringbuf_type*>(_M_streambuf); } + { return const_cast<__stringbuf_type*>(&_M_stringbuf); } __string_type str() const - { return this->rdbuf()->str(); } + { return _M_stringbuf.str(); } void str(const __string_type& __s) - { rdbuf()->str(__s); } - + { _M_stringbuf.str(__s); } }; @@ -304,41 +304,39 @@ namespace std typedef basic_string<_CharT, _Traits, _Alloc> __string_type; typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; typedef basic_iostream __iostream_type; - + + private: + __stringbuf_type _M_stringbuf; + + public: // Constructors/destructors explicit - basic_stringstream(ios_base::openmode __mode = - ios_base::out | ios_base::in) - : __iostream_type(new __stringbuf_type(__mode)) - { } + basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in) + : __iostream_type(NULL), _M_stringbuf(__m) + { this->init(&_M_stringbuf); } explicit basic_stringstream(const __string_type& __str, - ios_base::openmode __mode = - ios_base::out | ios_base::in) - : __iostream_type(new __stringbuf_type(__str, __mode)) - { } + ios_base::openmode __m = ios_base::out | ios_base::in) + : __iostream_type(NULL), _M_stringbuf(__str, __m) + { this->init(&_M_stringbuf); } ~basic_stringstream() - { - delete _M_streambuf; - _M_streambuf = NULL; - } + { } // Members: __stringbuf_type* rdbuf() const - { return static_cast<__stringbuf_type*>(_M_streambuf); } + { return const_cast<__stringbuf_type*>(&_M_stringbuf); } __string_type str() const - { return rdbuf()->str(); } + { return _M_stringbuf.str(); } void str(const __string_type& __s) - { rdbuf()->str(__s); } + { _M_stringbuf.str(__s); } }; - } // namespace std @@ -350,5 +348,5 @@ namespace std #endif #endif -#endif /* _CPP_SSTREAM */ +#endif // _CPP_SSTREAM diff --git a/libstdc++-v3/testsuite/27_io/fstream_members.cc b/libstdc++-v3/testsuite/27_io/fstream_members.cc new file mode 100644 index 00000000000..13520119f8d --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/fstream_members.cc @@ -0,0 +1,70 @@ +// 2001-05-24 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 27.8.1.13 member functions (fstream_members) + +#include +#include + +void +redirect_buffer(std::ios& stream, std::streambuf* new_buf) +{ stream.rdbuf(new_buf); } + +std::streambuf* +active_buffer(std::ios& stream) +{ return stream.rdbuf(); } + +// libstdc++/2832 +void test02() +{ + bool test = true; + const char* strlit01 = "fuck war"; + const char* strlit02 = "two less cars abstract riot crew, critical mass/SF"; + const std::string str00; + const std::string str01(strlit01); + std::string str02; + std::filebuf fbuf; + std::streambuf* pbasebuf0 = &fbuf; + + std::fstream sstrm1; + // derived rdbuf() always returns original streambuf, even though + // it's no longer associated with the stream. + std::filebuf* const buf1 = sstrm1.rdbuf(); + // base rdbuf() returns the currently associated streambuf + std::streambuf* pbasebuf1 = active_buffer(sstrm1); + redirect_buffer(sstrm1, &fbuf); + std::filebuf* const buf2 = sstrm1.rdbuf(); + std::streambuf* pbasebuf2 = active_buffer(sstrm1); + VERIFY( buf1 == buf2 ); + VERIFY( pbasebuf1 != pbasebuf2 ); + VERIFY( pbasebuf2 == pbasebuf0 ); + + // How confusing and non-intuitive is this? + // These semantics are a joke, a serious defect, and incredibly lame. +} + +int main() +{ + test02(); + return 0; +} + + + diff --git a/libstdc++-v3/testsuite/27_io/ifstream_members.cc b/libstdc++-v3/testsuite/27_io/ifstream_members.cc index 6a88ff48989..6a4961a82a1 100644 --- a/libstdc++-v3/testsuite/27_io/ifstream_members.cc +++ b/libstdc++-v3/testsuite/27_io/ifstream_members.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2000 Free Software Foundation, Inc. +// Copyright (C) 2000, 2001 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -73,10 +73,51 @@ bool test01() return test; } +void +redirect_buffer(std::ios& stream, std::streambuf* new_buf) +{ stream.rdbuf(new_buf); } + +std::streambuf* +active_buffer(std::ios& stream) +{ return stream.rdbuf(); } + +// libstdc++/2832 +void test02() +{ + bool test = true; + const char* strlit01 = "fuck war"; + const char* strlit02 = "two less cars abstract riot crew, critical mass/SF"; + const std::string str00; + const std::string str01(strlit01); + std::string str02; + std::filebuf fbuf; + std::streambuf* pbasebuf0 = &fbuf; + + std::ifstream sstrm1; + // derived rdbuf() always returns original streambuf, even though + // it's no longer associated with the stream. + std::filebuf* const buf1 = sstrm1.rdbuf(); + // base rdbuf() returns the currently associated streambuf + std::streambuf* pbasebuf1 = active_buffer(sstrm1); + redirect_buffer(sstrm1, &fbuf); + std::filebuf* const buf2 = sstrm1.rdbuf(); + std::streambuf* pbasebuf2 = active_buffer(sstrm1); + VERIFY( buf1 == buf2 ); + VERIFY( pbasebuf1 != pbasebuf2 ); + VERIFY( pbasebuf2 == pbasebuf0 ); + + // How confusing and non-intuitive is this? + // These semantics are a joke, a serious defect, and incredibly lame. +} int main() { test00(); test01(); + + test02(); return 0; } + + + diff --git a/libstdc++-v3/testsuite/27_io/istringstream_members.cc b/libstdc++-v3/testsuite/27_io/istringstream_members.cc index a803e3b0915..58885b934e7 100644 --- a/libstdc++-v3/testsuite/27_io/istringstream_members.cc +++ b/libstdc++-v3/testsuite/27_io/istringstream_members.cc @@ -1,6 +1,6 @@ // 2000-01-10 bkoz -// Copyright (C) 2000 Free Software Foundation, Inc. +// Copyright (C) 2000, 2001 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -18,19 +18,16 @@ // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, // USA. -// 27.7.2 template class basic_istringstream // 27.7.2.2 member functions (istringstream_members) -// stringbuf* rdbuf() const - #include #include - void test01() { bool test = true; std::istringstream is01; + const std::string str00; const std::string str01 = "123"; std::string str02; const int i01 = 123; @@ -39,9 +36,15 @@ void test01() std::ios_base::iostate state1, state2, statefail, stateeof; statefail = std::ios_base::failbit; stateeof = std::ios_base::eofbit; - + + // string str() const + str02 = is01.str(); + VERIFY( str00 == str02 ); + // void str(const basic_string&) is01.str(str01); + str02 = is01.str(); + VERIFY( str01 == str02 ); state1 = is01.rdstate(); is01 >> a; state2 = is01.rdstate(); @@ -66,17 +69,63 @@ void test01() VERIFY( state1 != state2 ); VERIFY( state2 == stateeof ); - // string str() const - str02 = is01.str(); - VERIFY( str01 == str02 ); - #ifdef DEBUG_ASSERT assert(test); #endif } +void +redirect_buffer(std::ios& stream, std::streambuf* new_buf) +{ stream.rdbuf(new_buf); } + +std::streambuf* +active_buffer(std::ios& stream) +{ return stream.rdbuf(); } + +// libstdc++/2832 +void test02() +{ + bool test = true; + const char* strlit01 = "fuck war"; + const char* strlit02 = "two less cars abstract riot crew, critical mass/SF"; + const std::string str00; + const std::string str01(strlit01); + std::string str02; + std::stringbuf sbuf(str01); + std::streambuf* pbasebuf0 = &sbuf; + + std::istringstream sstrm1; + VERIFY( sstrm1.str() == str00 ); + // derived rdbuf() always returns original streambuf, even though + // it's no longer associated with the stream. + std::stringbuf* const buf1 = sstrm1.rdbuf(); + // base rdbuf() returns the currently associated streambuf + std::streambuf* pbasebuf1 = active_buffer(sstrm1); + redirect_buffer(sstrm1, &sbuf); + std::stringbuf* const buf2 = sstrm1.rdbuf(); + std::streambuf* pbasebuf2 = active_buffer(sstrm1); + VERIFY( buf1 == buf2 ); + VERIFY( pbasebuf1 != pbasebuf2 ); + VERIFY( pbasebuf2 == pbasebuf0 ); + + // derived rdbuf() returns the original buf, so str() doesn't change. + VERIFY( sstrm1.str() != str01 ); + VERIFY( sstrm1.str() == str00 ); + // however, casting the active streambuf to a stringbuf shows what's up: + std::stringbuf* psbuf = dynamic_cast(pbasebuf2); + str02 = psbuf->str(); + VERIFY( str02 == str01 ); + + // How confusing and non-intuitive is this? + // These semantics are a joke, a serious defect, and incredibly lame. +} + int main() { test01(); + test02(); return 0; } + + + diff --git a/libstdc++-v3/testsuite/27_io/ofstream_members.cc b/libstdc++-v3/testsuite/27_io/ofstream_members.cc index 744036f0d7a..7a10072b94d 100644 --- a/libstdc++-v3/testsuite/27_io/ofstream_members.cc +++ b/libstdc++-v3/testsuite/27_io/ofstream_members.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2000 Free Software Foundation, Inc. +// Copyright (C) 2000, 2001 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -74,9 +74,51 @@ bool test01() return test; } +void +redirect_buffer(std::ios& stream, std::streambuf* new_buf) +{ stream.rdbuf(new_buf); } + +std::streambuf* +active_buffer(std::ios& stream) +{ return stream.rdbuf(); } + +// libstdc++/2832 +void test02() +{ + bool test = true; + const char* strlit01 = "fuck war"; + const char* strlit02 = "two less cars abstract riot crew, critical mass/SF"; + const std::string str00; + const std::string str01(strlit01); + std::string str02; + std::filebuf fbuf; + std::streambuf* pbasebuf0 = &fbuf; + + std::ofstream sstrm1; + // derived rdbuf() always returns original streambuf, even though + // it's no longer associated with the stream. + std::filebuf* const buf1 = sstrm1.rdbuf(); + // base rdbuf() returns the currently associated streambuf + std::streambuf* pbasebuf1 = active_buffer(sstrm1); + redirect_buffer(sstrm1, &fbuf); + std::filebuf* const buf2 = sstrm1.rdbuf(); + std::streambuf* pbasebuf2 = active_buffer(sstrm1); + VERIFY( buf1 == buf2 ); + VERIFY( pbasebuf1 != pbasebuf2 ); + VERIFY( pbasebuf2 == pbasebuf0 ); + + // How confusing and non-intuitive is this? + // These semantics are a joke, a serious defect, and incredibly lame. +} + int main() { test00(); test01(); + + test02(); return 0; } + + + diff --git a/libstdc++-v3/testsuite/27_io/ostringstream_members.cc b/libstdc++-v3/testsuite/27_io/ostringstream_members.cc new file mode 100644 index 00000000000..856544cb625 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/ostringstream_members.cc @@ -0,0 +1,105 @@ +// 2001-05-23 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 27.7.3.2 member functions (ostringstream_members) + +#include +#include + +void test01() +{ + bool test = true; + std::ostringstream os01; + const std::string str00; + const std::string str01 = "123"; + std::string str02; + const int i01 = 123; + int a,b; + + std::ios_base::iostate state1, state2, statefail, stateeof; + statefail = std::ios_base::failbit; + stateeof = std::ios_base::eofbit; + + // string str() const + str02 = os01.str(); + VERIFY( str00 == str02 ); + + // void str(const basic_string&) + os01.str(str01); + str02 = os01.str(); + VERIFY( str01 == str02 ); + + #ifdef DEBUG_ASSERT + assert(test); +#endif +} + +void +redirect_buffer(std::ios& stream, std::streambuf* new_buf) +{ stream.rdbuf(new_buf); } + +std::streambuf* +active_buffer(std::ios& stream) +{ return stream.rdbuf(); } + +// libstdc++/2832 +void test02() +{ + bool test = true; + const char* strlit01 = "fuck war"; + const char* strlit02 = "two less cars abstract riot crew, critical mass/SF"; + const std::string str00; + const std::string str01(strlit01); + std::string str02; + std::stringbuf sbuf(str01); + std::streambuf* pbasebuf0 = &sbuf; + + std::ostringstream sstrm1; + VERIFY( sstrm1.str() == str00 ); + // derived rdbuf() always returns original streambuf, even though + // it's no longer associated with the stream. + std::stringbuf* const buf1 = sstrm1.rdbuf(); + // base rdbuf() returns the currently associated streambuf + std::streambuf* pbasebuf1 = active_buffer(sstrm1); + redirect_buffer(sstrm1, &sbuf); + std::stringbuf* const buf2 = sstrm1.rdbuf(); + std::streambuf* pbasebuf2 = active_buffer(sstrm1); + VERIFY( buf1 == buf2 ); + VERIFY( pbasebuf1 != pbasebuf2 ); + VERIFY( pbasebuf2 == pbasebuf0 ); + + // derived rdbuf() returns the original buf, so str() doesn't change. + VERIFY( sstrm1.str() != str01 ); + VERIFY( sstrm1.str() == str00 ); + // however, casting the active streambuf to a stringbuf shows what's up: + std::stringbuf* psbuf = dynamic_cast(pbasebuf2); + str02 = psbuf->str(); + VERIFY( str02 == str01 ); + + // How confusing and non-intuitive is this? + // These semantics are a joke, a serious defect, and incredibly lame. +} + +int main() +{ + test01(); + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/27_io/stringstream_members.cc b/libstdc++-v3/testsuite/27_io/stringstream_members.cc new file mode 100644 index 00000000000..5b3ead1b2d0 --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/stringstream_members.cc @@ -0,0 +1,131 @@ +// 2001-05-24 Benjamin Kosnik + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 27.7.6 member functions (stringstream_members) + +#include +#include + +void test01() +{ + bool test = true; + std::stringstream is01; + const std::string str00; + const std::string str01 = "123"; + std::string str02; + const int i01 = 123; + int a,b; + + std::ios_base::iostate state1, state2, statefail, stateeof; + statefail = std::ios_base::failbit; + stateeof = std::ios_base::eofbit; + + // string str() const + str02 = is01.str(); + VERIFY( str00 == str02 ); + + // void str(const basic_string&) + is01.str(str01); + str02 = is01.str(); + VERIFY( str01 == str02 ); + state1 = is01.rdstate(); + is01 >> a; + state2 = is01.rdstate(); + VERIFY( a = i01 ); + // 22.2.2.1.2 num_get virtual functions + // p 13 + // in any case, if stage 2 processing was terminated by the test for + // in == end then err != ios_base::eofbit is performed. + VERIFY( state1 != state2 ); + VERIFY( state2 == stateeof ); + + is01.str(str01); + is01 >> b; + VERIFY( b != a ); + // as is01.good() is false, istream::sentry blocks extraction. + + is01.clear(); + state1 = is01.rdstate(); + is01 >> b; + state2 = is01.rdstate(); + VERIFY( b == a ); + VERIFY( state1 != state2 ); + VERIFY( state2 == stateeof ); + + #ifdef DEBUG_ASSERT + assert(test); +#endif +} + +void +redirect_buffer(std::ios& stream, std::streambuf* new_buf) +{ stream.rdbuf(new_buf); } + +std::streambuf* +active_buffer(std::ios& stream) +{ return stream.rdbuf(); } + +// libstdc++/2832 +void test02() +{ + bool test = true; + const char* strlit01 = "fuck war"; + const char* strlit02 = "two less cars abstract riot crew, critical mass/SF"; + const std::string str00; + const std::string str01(strlit01); + std::string str02; + std::stringbuf sbuf(str01); + std::streambuf* pbasebuf0 = &sbuf; + + std::stringstream sstrm1; + VERIFY( sstrm1.str() == str00 ); + // derived rdbuf() always returns original streambuf, even though + // it's no longer associated with the stream. + std::stringbuf* const buf1 = sstrm1.rdbuf(); + // base rdbuf() returns the currently associated streambuf + std::streambuf* pbasebuf1 = active_buffer(sstrm1); + redirect_buffer(sstrm1, &sbuf); + std::stringbuf* const buf2 = sstrm1.rdbuf(); + std::streambuf* pbasebuf2 = active_buffer(sstrm1); + VERIFY( buf1 == buf2 ); + VERIFY( pbasebuf1 != pbasebuf2 ); + VERIFY( pbasebuf2 == pbasebuf0 ); + + // derived rdbuf() returns the original buf, so str() doesn't change. + VERIFY( sstrm1.str() != str01 ); + VERIFY( sstrm1.str() == str00 ); + // however, casting the active streambuf to a stringbuf shows what's up: + std::stringbuf* psbuf = dynamic_cast(pbasebuf2); + str02 = psbuf->str(); + VERIFY( str02 == str01 ); + + // How confusing and non-intuitive is this? + // These semantics are a joke, a serious defect, and incredibly lame. +} + +int main() +{ + test01(); + test02(); + return 0; +} + + +