开发者

How can I extend std::basic_streambuf to treat any iterable sequence as a stream?

Note: Edited based on responses to receive more appropriate answers.

I have a collection of C++ templates that I've made over the years, which I call Joop. It comprises mainly libraries that don't quite fall into the "general-purpose" category but are just useful enough that I keep slapping them into different projects, so most of them don't have equivalents in other libraries such as Boost.

One of these classes is seqstream. The idea is that it allows you to treat any iterable sequence as an ordinary STL-like stream, whose "character type" is the value type of the sequence.

The rationale for this class is twofold. First, it should present an interface that makes any potentially nonlinear, noncontiguous sequence look linear and contiguous; and second, it should treat any object in the stream as though it were a single, complex, large character. There is a standard means of treating a stream as a sequence, so why not the other way around?

At present, seqstream wraps three iterators for the first, last, and current element. I want to replace seqstream with a basic_seqbuf that can be plugged into a standard stream. Can anyone provide resources to get me started on extending std::basic_streambuf to provide this kind of behaviour?

Additionally, if a writable seqbuf is allowed, it is ve开发者_Go百科ry that writing an object to the seqbuf does not serialise the object, but makes the appropriate call to an insert() method or uses a user-specified insert iterator, such as a std::back_insert_iterator.

Edit:

Here is an example of how seqstream is currently used:

// Create a sequence of objects.
std::vector<std::string> sequence;
for (int i = 0; i < 10; ++i) {
    std::ostringstream stream;
    stream << "Element " << i << ".";
    sequence.push_back(stream.str());
}

// Create a seqstream wrapping that sequence.
joop::seqstream< std::vector<std::string> > seqstream(sequence.begin(), sequence.end());

// Read the sequence like a stream.
std::string element;
while (seqstream >> element) // OR seqstream.get(element)
    std::cout << element << '\n';


It can be confusing to look at the examples in sstream, but you probably don't want a new stream class at all. Looking now for an example at the basic_stringstream source, the only purpose of that class is to

  • provide str function (it just calls the underlying buffer's str)
  • avoid the underlying buffer's vtable when calling its methods
  • change rdbuf's return value to basic_stringbuf* (but that's unnecessary because an accessor for str was provided)

The stream classes do very little, and really aren't supposed to have any functionality besides calling an underlying buffer of type basic_streambuf. For example, I can do this:

string str( "Hello, world!" );
stringbuf buf( str ); // subclass of basic_streambuf
iostream pseudo_stringstream( &buf );
    // pseudo_stringstream can do anything a stringstream can do.
    // (not necessarily with the same syntax)

Moreover, all streams are supposed to inherit from either basic_istream, basic_ostream, or both. Inserter/extractor functions may not work if your stream doesn't inherit correctly. These inserter declarations are perfectly fine:

operator<<( ostream os, MyData d ); // not a template at all
       // templated, but requires correct inheritance:
template< class C > operator<<( basic_ostream<C> os, MyData d );

Therefore, if you want iostream behavior, you need to implement a subclass of basic_streambuf and attach it to a basic_iostream.


But, what is your actual goal? What is the advantage of a memory-backed stream over the usual iterators and maybe some back_insert_iterators? Do you want to use the same code for serialization as for iteration? You probably want to make the stream look like a sequence using stream_iterator, not to make the sequence look like a stream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜