Stream Proposal

dsimcha dsimcha at yahoo.com
Fri Mar 11 18:29:42 PST 2011


The discussion we've had here lately about reading gzipped files has proved
rather enlightening.  I therefore propose the following high-level design for
streams, with the details to be filled in later:

1.  Streams should be built on top of input and output ranges.  A stream is
just an input or output range that's geared towards performing I/O rather than
computation.  The border between what belongs in std.algorithm vs. std.stream
may be a bit hazy.

2.  Streams should be template based/structs, rather than virtual function
based/classes.  This will allow reference counting for expensive resources,
and allow decorators to be used with zero overhead.  If you need runtime
polymorphism or a well-defined ABI, you can wrap your stream using
std.range.inputRangeObject and std.range.outputRangeObject.

3.  std.stdio.File should be moved to the new stream module but publicly
imported by std.stdio.  It should also grow some primitives that make it into
an input range of characters.  These can be implemented with buffering under
the hood for efficiency.

4.  std.stdio.byLine and byChunk and whatever functions support them should be
generalized to work with any input range of characters and any input range of
bytes, respectively.  The (horribly ugly) readlnImpl function that supports
byLine should be templated and decoupled from C's file I/O functions.  It
should simply read one byte at a time from any range of bytes, decode UTF as
necessary and build a line as a string/wstring/dstring.  Any buffering should
be handled by the range it's reading from.


More information about the Digitalmars-d mailing list