[phobos] Improvement of stream

Shin Fujishiro rsinfu at gmail.com
Tue Jul 6 03:35:43 PDT 2010


Andrei Alexandrescu <andrei at erdani.com> wrote:
> On 07/05/2010 09:59 AM, Shin Fujishiro wrote:
> > Is the handle a similar concept as the container?
> 
> Well not quite because a container does not need put an emphasis on 
> "opening" and "closing".
> 
> > That is, I reckon a handle is a reference type object that has its own
> > resource and provides some ranges on top of it for accessing and
> > manipulating the resource:
> >
> > class ConceptualHandle
> > {
> >      void open();    // ?
> >      void close();
> >      ByChunk byChunk(size_t n);
> >      ByChar byChar();
> >      Writer blockWriter();
> > }
> 
> Yes, that seems plausible.
> 
> Andrei

Can handles (optionally or necessarily) have low-level I/O primitives
like rawRead() and rawWrite()?

class SomeHandle
{
     void open();
     void close();
    ByChunk byChunk(size_t n);
    ByChar byChar();

    size_t rawRead(ubyte[] buffer);    // low-level input primitive
}


byChunk etc. would suffice for usual purposes.  But sometimes we want
to read some user-defined structure from a handle.  With the rawRead
primitive, I can create my own input range for reading a sequence of
variable-length packets from a handle:

ByPacket byPacket(SomeHandle handle);

// This input range uses rawRead() for reading Packets.
struct ByPacket
{
    @property bool empty();
    @property Packet front();
    void popFront();
}
struct Packet
{
    uint signature;
    ubyte[] content;
    uint checksum;
}


Shin


More information about the phobos mailing list