This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: basic_stringbuf<>::sync


As I wrote in my message from 31 May 1999 the current implementation
of basic_stringbuf<> prevents the capabilities of basic_string<>. 
It calls the non const begin/end member functions of basic_string<> 
in basic_stringbuf<>::sync - it makes _M_str unsharable. 
For this reason ANY call of a function of _M_str causes a reallocation.

My patch avoids the call of non const begin/end. In sync I use
the basic_string<>::data member function and I remove the const attribute.

In the constructor of basic_stringbuf<>, in both str and in setbuf
member functions I use basic_string<>::basic_string(Iter, Iter)
or basic_string::assign(Iter, Iter).
So there are no reference copies of _M_str.  

Another problem is the copying of a basic_stringbuf<> object
as I wrote in "basic_streambuf<> and copying".


Ryszard Kabatek
Martin-Luther University Halle-Wittenberg, Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 (2466) Fax. +49 3461 46 2129
Index: bits/std_sstream.h
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/std_sstream.h,v
retrieving revision 1.34
diff -c -2 -p -r1.34 std_sstream.h
*** std_sstream.h	1999/05/27 08:48:19	1.34
--- std_sstream.h	1999/06/08 15:16:58
*************** namespace std {
*** 69,73 ****
        basic_stringbuf(const __string_type& __str,
  		      ios_base::openmode __mode = ios_base::in | ios_base::out)
!       : __streambuf_type(), _M_str(__str)
        { 
  	_M_mode = __mode;
--- 69,73 ----
        basic_stringbuf(const __string_type& __str,
  		      ios_base::openmode __mode = ios_base::in | ios_base::out)
!       : __streambuf_type(), _M_str(__str.begin(), __str.end())
        { 
  	_M_mode = __mode;
*************** namespace std {
*** 77,86 ****
        // Get and set:
        __string_type 
!       str() const { return _M_str; }
  
        void 
        str(const __string_type& __s)
        {
! 	_M_str = __s;
  	this->sync();
        }
--- 77,87 ----
        // Get and set:
        __string_type 
!       str() const 
!       { return __string_type(_M_str.data(), _M_str.data() + _M_str.size()); }
  
        void 
        str(const __string_type& __s)
        {
! 	_M_str.assign(__s.begin(), __s.end());
  	this->sync();
        }
*************** namespace std {
*** 109,113 ****
  	if (__n) 
  	  {
! 	    _M_str.assign(__s, __n);
  	    this->sync();
  	  }
--- 110,114 ----
  	if (__n) 
  	  {
! 	    _M_str.assign(__s, __s + __n);
  	    this->sync();
  	  }
*************** namespace std {
*** 126,136 ****
        sync()
        {
!          // Using details of iterator implementation to convert
!          // iterator to pointer
  	if (_M_mode & ios_base::in)
! 	  setg(_M_str.begin().base(), _M_str.begin().base(),
!                _M_str.end().base());
  	if (_M_mode & ios_base::out)
! 	  setp(_M_str.begin().base(), _M_str.end().base());
  	return 0;
        }
--- 127,136 ----
        sync()
        {
!         char_type* __first = const_cast<char_type*>(_M_str.data());
!         char_type* __last  = __first + _M_str.size();
  	if (_M_mode & ios_base::in)
! 	  setg(__first, __first, __last);
  	if (_M_mode & ios_base::out)
! 	  setp(__first, __last);
  	return 0;
        }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]