comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Ada.Storage_IO: applied example?
Date: Wed, 24 Oct 2012 16:03:51 +0200
Date: 2012-10-24T16:03:48+02:00	[thread overview]
Message-ID: <5087f544$0$6572$9b4e6d93@newsspool3.arcor-online.net> (raw)
In-Reply-To: <op.wmolvvofule2fv@cardamome>

On 24.10.12 13:51, Yannick Duchêne (Hibou57) wrote:
> Le Wed, 24 Oct 2012 12:28:00 +0200, Dmitry A. Kazakov
> <mailbox@dmitry-kazakov.de> a écrit:
> 
>> On Wed, 24 Oct 2012 02:49:02 -0700 (PDT), AdaMagica wrote:
>>
>>> See AARM A.9(1.a):
>>> Reason: This package exists to allow the portable construction of
>>> user-defined direct-access-oriented input-output packages.
>>
>>    portable construction of X /= construction of portable X
>>
>> The package cannot be used to write files in a way that an application
>> compiled by one compiler X1 under OS Y1 could write a file readable for
>> compiler X2 under OS Y2.
>>
>> In order to be portable they should have defined the exact representation
>> of the object in the file. Yes, nobody would do that, but then they should
>> not include useless packages into the standard either.
> 
> What about private persistent storage?
> 

If traditional Ada I/O is good for your Ada program, you
can use Storage_IO in conventional algorithms this way. The
setup can employ static polymorphism that allows selecting
instances of Storage_IO or Direct_IO as needed.
Either at startup or later.

<rant>
The solution, using the egregious generic mechanisms of Ada,
will suffer from all the advantages of lack of an unused
tagged parameter.

The whole approach of stating requirements as generic formals
suffers from good opportunities for compilers to perform
optimization.

It suffers from the fact that stored internal program
data is not being stored in an ISO-approved form ready for
inspection by non-generic programs of an unrelated make
and purpose.

It also suffers from the problem that it still cannot make
the external world an Ada object, which could mollify the
previously argued flaw.

It suffers from lack of opportunities to control I/O of
octets algorithmically, thus preventing emulation of nice
per-program Storage_IO objects in DIY fashion.

It also suffers from not being a silver bullet.

But other than that ...</rant>

with Ada.Direct_IO;
with Ada.Storage_IO;
with Ada.Command_Line;  use Ada.Command_Line;

procedure Lookahead is
   use Ada;

   type Sentence (Length : Positive := 100) is record
      Text : Wide_String (1 .. 100);
   end record;

   subtype Name is Wide_String (1 .. 20);

   type Big is record   -- objects that will be processed
      First, Last : Name;
      Age : Natural;
      Motto : Sentence;
   end record;

   generic
      with procedure Store_Single_Item (Item : in Big);
      with procedure Fetch_Single_Item (Item : out Big);
   procedure Process_Records;

   procedure Process_Records is separate;
begin
   if Natural'Value (Argument(1)) = 0 then
      declare
         package Volatile is
            procedure Store (Item : in Big);
            procedure Fetch (Item : out Big);
         end Volatile;
         package body Volatile is
            package Undo_Buffer is new Storage_IO (Big);
            Storage : Undo_Buffer.Buffer_Type;
            procedure Fetch (Item : out Big) is
            begin
               Undo_Buffer.Read (Storage, Item);
            end Fetch;
            procedure Store (Item : in Big) is
            begin
               Undo_Buffer.Write (Storage, Item);
            end Store;
         end Volatile;
         procedure Run is new Process_Records
           (Store_Single_Item => Volatile.Store,
            Fetch_Single_Item => Volatile.Fetch);
      begin
         Run;
      end;
   else
      declare
         package Permanent is
            procedure Store (Item : in Big);
            procedure Fetch (Item : out Big);
         end Permanent;
         package body Permanent is
            package Undo_Buffer is new Direct_IO (Big);
            Storage : Undo_Buffer.File_Type;
            procedure Fetch (Item : out Big) is
            begin
               Undo_Buffer.Read (Storage, Item);
            end Fetch;
            procedure Store (Item : in Big) is
            begin
               Undo_Buffer.Write (Storage, Item);
            end Store;
            use Undo_Buffer;
         begin
            open (storage, Inout_File, "item.sto");
         end Permanent;
         procedure Run is new Process_Records
           (Store_Single_Item => Permanent.Store,
            Fetch_Single_Item => Permanent.Fetch);
      begin
         Run;
      end;
   end if;

end Lookahead;

separate (Lookahead)
procedure Process_Records is
   package Rec_IO is new Direct_IO (Big); use Rec_IO;
   Current: Big := (Age => Natural'Last, others => <>);
   input : File_Type;
   output : File_Type;
begin
   open (input, In_File, "records.all");
   open (output, Out_File, "records.flt");
   loop
      Store_Single_Item (Current);
      Read (input, current);
      -- ... filter based on pairs ...
         write (output, current);
   end loop;
end Process_Records;




      parent reply	other threads:[~2012-10-29  2:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-23 19:42 Ada.Storage_IO: applied example? Yannick Duchêne (Hibou57)
2012-10-23 20:28 ` Dmitry A. Kazakov
2012-10-23 20:56   ` Yannick Duchêne (Hibou57)
2012-10-23 21:21     ` Adam Beneschan
2012-10-23 21:02   ` Jeffrey Carter
2012-10-24  7:20     ` Dmitry A. Kazakov
2012-10-24  5:03   ` J-P. Rosen
2012-10-24  7:34     ` Dmitry A. Kazakov
2012-10-24  9:49       ` AdaMagica
2012-10-24 10:28         ` Dmitry A. Kazakov
2012-10-24 11:51           ` Yannick Duchêne (Hibou57)
2012-10-24 12:27             ` Dmitry A. Kazakov
2012-10-24 14:03             ` Georg Bauhaus [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox