comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Encapsulating Ada.Direct_IO
Date: Tue, 16 Nov 2010 21:20:05 -0800 (PST)
Date: 2010-11-16T21:20:05-08:00	[thread overview]
Message-ID: <82e9201f-9186-4450-bbf8-9433f6a19d71@v12g2000vbh.googlegroups.com> (raw)
In-Reply-To: 5ba4147a-6099-4a05-b548-09544f58247a@j18g2000yqd.googlegroups.com

On Nov 16, 8:44 pm, Bryan <brobinson....@gmail.com> wrote:
> I'm trying to port some code to Ada for dealing with Big5-encoded
> files. I realize that I might be able to use Ada.Wide_Text_IO, but I'm
> trying to learn Ada and understand the language better.  I'm still
> working on wrapping my head around types and packages.  My original
> code in C++ opens a file as binary and parses it byte by byte and
> breaking it into Big5 characters depending on the byte codes.  I
> thought I'd try to do something similar by encapsulating Ada.Direct_IO
> into a package.  I'm not having much luck, however.
>
> Spec file:
> ======================
> with Ada.Direct_IO;
> package Big5_Text_IO is
>   type File_Type is limited private;
>   procedure Close( File : in out File_Type );
> private
>   package Byte_IO is new Ada.Direct_IO(Character);
>   type File_Type is new Byte_IO.File_Type;
> end Big5_Text_IO;
> ======================
>
> Body file
> ======================
> package body Big5_Text_IO is
>   procedure Close( File : in out File_Type ) is
>   begin
>         Byte_IO.Close(File);
>   end Close;
> end Big5_Text_IO;
> ======================
>
> Test driver:
> ======================
> with Big5_Text_IO;
> with Ada.Text_IO;
> procedure Big5_Test is
>   Input_File : Big5_Text_IO.File_Type;
> begin
>   Ada.Text_IO.Put_Line("OK?");
> end Big5_Test;
> ======================
>
> If I leave out the Close method and remove the body file, I can build
> the test driver with no issues.  Otherwise, I get the following from
> GNAT:
>
> ======================
> gcc -c big5_text_io.adb
> big5_text_io.adb:6:23: expected private type "Ada.Direct_Io.File_Type"
> from instance at big5_text_io.ads:11
> big5_text_io.adb:6:23: found private type "Big5_Text_IO.File_Type"
> defined at big5_text_io.ads:12
> gnatmake: "big5_text_io.adb" compilation error
> ======================
>
> I would *greatly* appreciate any tips in how I can better design my
> package so that it can encapsulate Ada.Direct_IO or some other method
> of binary I/O.  I looked at the GNAT source and I'm hoping I won't
> have to emulate what they have done...its a bit over my head at this
> point.

You're close.  Try changing

   Byte_IO.Close (File);

to

   Byte_IO.Close (Byte_IO.File_Type (File));

When you declare a derived type "type T2 is new T1", then T2 and T1
are not the same type, so you can't use an object of type T2 where
something of type T1 is expected.  But you can use a type conversion.

Note: I'm at home so I can't try this easily.  I seem to recall that
there were some issues using this paradigm with limited types
(including an incompatibility with earlier versions of the language),
but I don't recall the details and it's hard for me to look them up
right now.  If it turns out the type conversion doesn't work, then you
might have to make File_Type a record in the private part:

   type File_Type is record
      F : Byte_IO.File_Type;
   end record;

and then use File.F whenever you want to use a Byte_IO operation,
e.g.:

   Byte_IO.Close (File.F);

Hope this helps,

                                  -- Adam



  reply	other threads:[~2010-11-17  5:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-17  4:44 Encapsulating Ada.Direct_IO Bryan
2010-11-17  5:20 ` Adam Beneschan [this message]
2010-11-26 15:31   ` Bryan
2010-11-17 12:25 ` Peter C. Chapin
2010-11-18  1:16   ` Randy Brukardt
2010-11-18  2:21     ` Peter C. Chapin
2010-11-18 16:36       ` Adam Beneschan
2010-11-18 18:21         ` Peter C. Chapin
2010-11-18 18:36           ` Randy Brukardt
2010-11-18 19:48           ` Adam Beneschan
2010-11-18 20:15             ` Dmitry A. Kazakov
2010-11-18  7:39     ` AdaMagica
2010-11-18 18:38       ` Randy Brukardt
2010-11-18  9:46     ` Maciej Sobczak
2010-11-18 16:31     ` Adam Beneschan
2010-11-18 17:05       ` Dmitry A. Kazakov
     [not found]         ` <ENidndoH8qoqjHvRnZ2dnUVZ_j-dnZ2d@earthlink.com>
2010-11-19  8:24           ` Dmitry A. Kazakov
2010-11-19 16:19             ` Adam Beneschan
2010-11-18 18:45       ` Randy Brukardt
2010-11-24 21:31     ` Warren
2010-11-17 22:32 ` Yannick Duchêne (Hibou57)
2010-11-17 23:03   ` Adam Beneschan
2010-11-17 23:11     ` Yannick Duchêne (Hibou57)
replies disabled

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