Re: I/O streaming with custom data transport



Maciej Sobczak wrote:

Hi,

In C++ the IOStreams library allows to vary the behaviour of the stream
by decoupling formatting from data buffering and transport to the
physical device, which are in turn strategies for the stream object.
This means that having millions of functions like this:

void foo(ostream &s)
{
s << "Hello";
s << someObject;
s << someOtherObject;
// ...
}

(and *there are* millions of functions like this)

it is possible to reuse these functions with different stream objects,
from standard console output:

foo(cout);

...to file output:

foo(myFile);

...to network socket output:

foo(someSocketStreamConnectedSomewhere);

...to whatever else comes up to the programmer's mind. It's just the
matter of providing the implementation for the stream buffer.
The key point here is that the mass of code out there need not be aware
of it and can be reused with new stream objects without any
modifications (that's OO in action).

I'm looking for something like this in Ada.

The basic I/O facilities in the standard library don't seem to provide
anything like this.
I hoped that Ada.Streams allows this by subclassing Root_Stream_Type and
providing some overriding operations, but unfortunately I cannot even
find the specification of Root_Stream_Type (looks like there isn't any
and this type is just a name placeholder in ARM).

I think you haven't looked right. That's precisely how it's done. I have
chainable streams for compression, buffering, file I/O, memory streaming,
etc. And you do this just as you say: you extend
Ada.Streams.Root_Stream_Type and provide the read/write subprograms. Then,
you can dispatch on these or use the 'Input/'Output attributes of any type.
Conversely, you can override these to provide your own stream
representation of any type.

(I mention calling the subprograms directly instead of the 'attributes
because for some array types it's much more efficient -- at least it was
with GNAT 3.15p. But, usually, you just want to use the attributes).

Maybe is a compiler matter. In GNAT you can inspect the specification, but
if this is done by compiler magic in your case, still you should be able to
override the Read/Write subprograms in your derived stream type. The
package spec is in the RM05 13.13.1.

If the standard library does not provide the functionality that I'm
looking for (and I hope that I'm still misunderstanding something here),
did anybody tackle the problem by implementing some replacement IO
library with the functionality similar to that of IOStreams in C++?

If you're using GPS, go to Help -> GNAT runtime -> Ada -> Streams. You have
there the spec of Root_Stream_Type.

Ada 2005 adds new features for some kind of magic needed when inputting
types from an stream. I have had no need for this, but you may be as well
interested. It's all in the Barnes' rationale articles.
.



Relevant Pages

  • Re: Segmentation fault
    ... Here I have made room for 256 characters. ... You see, printfis outputting its string to the stdout stream, ... buffering for its streams: no buffering, line buffering, and full ... not be opened explicitly -- standard input (for reading ...
    (comp.lang.c)
  • Re: I/O buffering
    ... have buffering capabilities, why does Net also have special buffering ... sometimes it's useful to add buffering to a non-buffering Stream class. ... One would not normally use BufferedStream with a FileStream, ... Your own code would have to explicitly dispose your custom Stream class, when you were sure you were done with it. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: m3u Buffering non-stop
    ... at home I have a local LAN setup that uses DHCP to automatically ... at your home to see what your actual up and down stream rates are. ... every time I try to connect, WMP just keeps Buffering over and over and I ...
    (microsoft.public.windowsmedia.player)
  • Re: Are system calls sometimes costlier than library calls?
    ... I suspect, at least in the case of printf, that this buffering is usually ... printing a newline to a line-buffered stream; ... the host environment" is done with something that has some extra ... some implement "fast" file systems that can lose ...
    (comp.lang.c)
  • RE: m3u Buffering non-stop
    ... Ran the speed test and clocked in at 936 Kbps. ... KLAS stream with no problem. ... In WMP, open Tools -> Options, select the Performance tab. ... every time I try to connect, WMP just keeps Buffering over and over and I ...
    (microsoft.public.windowsmedia.player)