Re: I/O streaming with custom data transport
- From: "Alex R. Mosteo" <devnull@xxxxxxxxxxxxxx>
- Date: Tue, 21 Nov 2006 18:51:12 +0100
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.
.
- Follow-Ups:
- Re: I/O streaming with custom data transport
- From: Maciej Sobczak
- Re: I/O streaming with custom data transport
- References:
- I/O streaming with custom data transport
- From: Maciej Sobczak
- I/O streaming with custom data transport
- Prev by Date: Re: End_Of_File underspecified?
- Next by Date: Re: generic question
- Previous by thread: Re: I/O streaming with custom data transport
- Next by thread: Re: I/O streaming with custom data transport
- Index(es):
Relevant Pages
|