TITLE: a stream for custom string types (Newsgroups: comp.lang.c++.moderated, 6 Oct 99) ALEXANDRESCU: Andrei Alexandrescu > However, we found that std::basic_stringstream is tied to > std::basic_string, and there's no way to easily build a stringstream- > like class that's based upon our String implementation. KUEHL: Dietmar Kuehl If your 'String' class has a similar interface to 'wstring', it is fairly easy to create a 'stringstream'-like class: Just create a stream buffer which writes to your 'String' class and use this in corresponding stream classes. The interesting question which comes up for me is whether you really want to use 'stringstream' as opposed to 'wstringstream' if your stream class is similar to 'wstring'... ALEXANDRESCU: > I guess copy > and paste from the standard headers would work, but that's clumsy. KUEHL: I would expect this to be illegal in the first place and whether it is clumsy is a completely different issue. Creating a new stream buffer is not that hard and is what is in order to create a stream accessing a new "external representation". Depending on your needs, it can be much simpler than the stream buffer for the standard strings: For example, unless you need it, you can just drop positioning in the stream (I found this to be the only complex task in stream buffers). In this case, you would just implement the members - 'underflow()' to set up an input buffer (ie. the current string) - 'overflow()' to add a character to the stream - 'xsputn()' to add a bunch of characters The last function is not strictly necessary but improves the performance of adding strings to the stream: Without this function, the characters would be added one at a time using 'overflow()'. The other virtual functions or not necessary for I/O with strings: - 'uflow()' is necessary only for unbuffered streams (or streams which become unbuffered in certain states). - 'xsgetn()' can be used for improved reads of strings but improves the performance only if the strings read do not fit into the buffer (well, it might also help with bad implementations...). - 'sync()' is used to synchronize the external and the internal representation but for strings these are in synchronization anyway. - 'showmanyc()' ("es-how-many-c") is unnecessary because the strings is identical to the input buffer and thus this is computed already correctly by the default implementations. In any case, writing a simple stream buffer for something like a string is fairly easy...