CVS difference for ais/ai-00260.txt

Differences between 1.2 and version 1.3
Log of other versions for file ais/ai-00260.txt

--- ais/ai-00260.txt	2001/03/22 22:24:53	1.2
+++ ais/ai-00260.txt	2001/05/11 03:46:21	1.3
@@ -275,3 +275,166 @@
 based on that idea??
 
 ****************************************************************
+
+From: Pascal Obry
+Sent: Wednesday, April 04, 2001 11:30 AM
+
+Hello,
+
+Here is a proposal for an Ada stream extenssion, the goal is to provide more
+control on the Tag for streamed object.
+
+<<
+!summary
+
+The stream input/output attributes are not flexible enough. The problem is for
+the tag handling of tagged object. With the current implementation it is not
+possible for example to redefine the 'Read and 'Write to have the object
+streamed as an XML object. We propose a solution to solve this problem.
+
+!problem
+
+The problem is for the tag handling of tagged object. With the current
+implementation it is not possible for example to redefine the 'Read and 'Write
+attribute to have the object streamed as an XML object. Indeed the tag is
+defined as a string. It is possible to change the tag string value by using
+Ada.Tags facilities. But the tag will always be a string : an unbounded
+object. Each time the tagged object will be streamed the tag string value will
+be written preceded by the string bounds which are binary data. There is no way
+to avoid these binary data to be sent. In other word, there is no way to
+stream an object with a plain text representation.
+
+!proposal
+
+Have the possibility to set the way the 'Class'Output and 'Class'Input handle
+the tag with the new attributes.
+
+Two new attributes form are proposed:
+
+   S'External_Tag'Read
+     This attribute can be used to set the right function to use for reading
+     the tag on a stream. This function will be called by S'Class'Input
+     instead of the default one. The function prototype must be:
+
+        function Read (S : access Streams.Root_Stream_Type'Class)
+          return Ada.Tags.Tag;
+
+   S'External_Tag'Write
+     This attribute can be used to set the right function to use for writing
+     the tag on a stream. This function will be called by S'Class'Output
+     instead of the default one. The procedure prototype must be:
+
+        procedure Write (S : access Streams.Root_Stream_Type'Class;
+                         Tag : Ada.Tags.Tag);
+
+The definition for S'Class'Input and S'Class'Output must be changed
+accordingly.
+
+!discussion
+
+This proposal does not really introduce new attributes. The S'External_Tag
+attribute can be composed with 'Read and 'Write. This change is upward
+compatible as it does not change the semantic for streamed object when it is
+not used.
+
+!example
+
+Here is an example that show the problem. Let's say that we want to stream an
+object with an XML encoding:
+
+with Ada.Streams;
+
+package Class is
+
+   use Ada;
+
+   type Object is tagged record
+      V : Integer := 2;
+   end record;
+
+   procedure Write (S : access Streams.Root_Stream_Type'Class; O : Object);
+   for Object'Write use Write;
+
+   for Object'External_Tag use "<object>";
+
+end Class;
+
+package body Class is
+
+   procedure Write (S : access Streams.Root_Stream_Type'Class; O : Object) is
+   begin
+      String'Write (S, "<v>");
+      String'Write (S, Integer'Image (O.V));
+      String'Write (S, "</v>");
+      String'Write (S, "</object>");
+   end Write;
+
+end Class;
+
+
+with Ada.Text_IO.Text_Streams;
+with Class;
+
+procedure Main is
+   use Ada;
+
+   O : Class.Object;
+begin
+   Class.Object'Class'Output
+     (Text_IO.Text_Streams.Stream (Text_IO.Current_Output), O);
+end Main;
+
+This program output will be something like:
+
+   ^A^@^@^@^H^@^@^@<object><v> 2</v></object>
+
+The 8 first characters are binary representation for the tag bound. This is no
+way to bypass that, so there is no way to stream an object with an XML
+representation.
+
+Using the new 'External_Tag'Read and 'External_Tag'Write attributes it is
+possible to control the tag input/output. By changing the Class spec to read:
+
+with Ada.Streams;
+
+package Class is
+
+   use Ada;
+
+   type Object is tagged record
+      V : Integer := 2;
+   end record;
+
+   for Object'External_Tag use "object";
+
+   procedure Write (S : access Streams.Root_Stream_Type'Class; O : Object);
+   for Object'Write use Write;
+
+   procedure Write (S : access Streams.Root_Stream_Type'Class;
+                    Tag : Ada.Tags.Tag);
+   --  Implementation not shown. This function:
+   --    1) output character '<'
+   --    2) output Ada.Tags.External_Tag (Tag)
+   --    3) output character '>'
+   for Object'External_Tag'Write use Write;
+
+   function Read (S : access Streams.Root_Stream_Type'Class)
+     return Ada.Tags.Tag;
+   --  Implementation not shown. This function:
+   --    1) read first character, raise Ada.Tags.Tag_Error if it is not '<'
+   --    2) read the tag (let's call it Tag_String)
+   --    3) check for end of tag which is '>' (raise Ada.Tags.Tag_Error if no
+   --       such character on the stream)
+   --    4) return Ada.Tags.Internal_Tag (Tag_String)
+   for Object'External_Tag'Read use Read;
+
+end Class;
+
+We now have the following output:
+
+   <object><v> 2</v></object>
+>>
+
+Let me know what you think about this proposal ? Do you need something else ?
+
+****************************************************************

Questions? Ask the ACAA Technical Agent