This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: basic_stringbuf<>::sync
- To: Benjamin Kosnik <bkoz@cygnus.com>
- Subject: Re: basic_stringbuf<>::sync
- From: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
- Date: Mon, 31 May 1999 11:36:35 +0200 (CEST)
- cc: libstdc++@sourceware.cygnus.com
- Reply-To: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
Below a test for the problem I described in my previous message.
In the sample I only create a stringbuf object and I call the str
member function. As we know string is reference counted. In the constructor
and str member function of stringbuf we use the copy constructor of string
so we can expect, that s.data() == str.data() (the same string::_Rep*).
But the sync member function of stringbuf calls the non-const begin/end
member functions of string - it makes the string unsharable. So we cannot
profit by the storage between string::size and string::capacity. Any call
to a non-const string member function will cause a reallocation.
The solution I described in my previous message should be completed:
We should use the _M_str member as a kind of character array.
In the constructor, both str and setbuf member functions
we shoud not use the copy constructor (operator=).
Instead we should use the assign member function.
Perhaps there is a better solution, but the current implementation
is inefficient.
#include <sstream>
#ifdef DEBUG_ASSERT
#include <cassert>
#endif
void
test01()
{
// basic_stringbuf<>::basic_stringbuf(const __string_type&,
// ios_base::openmode)
string s1 = "Barbapapa";
stringbuf sb(s1);
string str1 = sb.str();
#ifdef DEBUG_ASSERT
assert(s1.data() == str1.data()); // True if str1 is a reference copy of s1.
#endif
// basic_stringbuf<>::str(const __string_type&)
string s2 = "Barbamama";
sb.str(s2);
string str2 = sb.str();
#ifdef DEBUG_ASSERT
assert(s2.data() == str2.data()); // True if str2 is a reference copy of s2.
#endif
}
int main()
{
test01();
}
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