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 very 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 thebasic_stringstreamsource, the only purpose of that class is tostrfunction (it just calls the underlying buffer’sstr)rdbuf‘s return value tobasic_stringbuf*(but that’s unnecessary because an accessor forstrwas 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: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:Therefore, if you want iostream behavior, you need to implement a subclass of
basic_streambufand attach it to abasic_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 usingstream_iterator, not to make the sequence look like a stream.