I have code which dumps a representation of complex objects to a stream. The representation uses open and close curly brackets to denote the start and end of nested objects. Rather than pretty print the representation format, I feel it's better to leave all whitespace out of the raw output and implement pretty printing, if desired, as a decorator pattern.
The pretty print algorithm is simply: examine next char to output, if it is a close brace, output newline + close brace + newline. Else just output the char.
I could implement this decorator as a class derived from ostream_iterator or as a class derived from ostream. Which is more commonly done? Are there any downsides to creating an ostream wrapper?
The only reason where it is advisable to derive from std::ostream
is when using the derived class to construct an std::streambuf
and initialize the the base std::ostream
with it. I don't think there is ever a reason to derive from std::ostream_iterator<...>
at all.
Instead you'd derive from std::streambuf
and override overflow()
to do whatever filter operation you want to do. For convenience you might then derive from std::ostream
to construct a convenience stream.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.