Re: I/O streaming with custom data transport



Robert A Duff wrote:

procedure Write_Hello(Stream : in out Stream_Access) is
begin
String'Write(Stream, "Hello");
end Write_Hello;

I don't understand this. Ada streams are streams of Stream_Elements
(usually bytes), and the 'Read/'Write attributes encode things in
some way that is not necessarily human-readable.

That's right, but the above was written for experimental/presentation purposes and was based on the assumption that the implementation will not unnecessarily complicate things so that the output will anyway be easy to verify to show that the principle works.

For example,
I think the above outputs the bounds of the string in some
binary form.

T'Input/'Output do, but T'Write/'Read don't.

But the original question was about streams of characters,
intended to be human-readable. Right?

Not necessarily. The point was only to make sure that there is a possiblity to use streams in a polimorphic way. Or to put it more precisely, where the existing code can be reused with different data transport strategies and where the reuse method will be based on run-time polymorphism and not, for example, on generics.

So how do Ada's streams help, here?

Taking into account these objective, they do. :-)

Of course, the question about text streams is still valid - and was left open in one of my other posts.

For the moment I assume that in order to do this with text, I have to first format it in some string buffer and then do:

String'Write(S, Buf);

(just like with the "Hello" above)

Taking into account that T'Write does not output constraints, it might be a valid method to insert text into the stream. Is this correct?

It seems to me what you want is an abstract Character_Output_Stream
type, with a primitive (or class-wide?) operation that puts a Character
to the stream. Then you can make derived types for sending to a file,
to an in-memory buffer, etc.

That makes sense as well, but seems to be further off from what the standard library offers out of the box.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
.