comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen.leake@gsfc.nasa.gov>
Subject: Re: How to Use Abstract Data Types
Date: 1998/05/05
Date: 1998-05-05T00:00:00+00:00	[thread overview]
Message-ID: <354F5C4D.DFF8286E@gsfc.nasa.gov> (raw)
In-Reply-To: matthew_heaney-ya023680003004981709380001@news.ni.net


Matthew Heaney wrote:

> <snip>
> What's wrong with the iterator-as-type technique?
>
> package Libraries is
>
>    type Root_Library is abstract tagged null record;
> ...
>    type Library_Iterator (Library : access Root_Library'Class) is limited
> private;
>
>    function Is_Done (Iterator : Library_Iterator) return Boolean;
>
>    function Get_Book (Iterator : Library_Iterator) return Book'Class;
>
>    procedure Advance (Iterator : in out Library_Iterator);
> ...
> end;
>
> declare
>    Library : aliased My_Library;
>    Iterator : Library_Iterator (Library'Access);
> begin
> ...
>    while not Is_Done (Iterator) loop
>       ... Get_Book (Iterator) ...
>       Advance (Iterator);
>    end loop;
> ...
> end;
>
> This is the technique I use throughout the ACL.  In particular, it's the
> technique I used for the B-tree packages a few cla readers have asked for.

 I like this technique, too, but one problem I have with it is it assumes
read-write access to the Library. Suppose I want to implement Find:

function Find (Title : in String; Library : in My_Library) return Book'class
is
    Iterator : Library_Iterator (Library'access); -- illegal
begin
    ...
end Find;

Since the Library parameter is "in", you cannot use it as the access
discriminant. The only way around this (that I have found) is to restructure
Find to be a procedure, with Library "in out" and the Book result as an "out"
parameter.

With the generic package iterator:
   generic
      type Some_Library is new Library with private;
      This_Library: in  Some_Library;
   package Iterator is
      function More return Boolean;
      function Next return Book'Class;
   end Iterator;

"This_Library" is now read-only.

Is there a good way to get read-only access with Iterator_Type?

-- Stephe





  parent reply	other threads:[~1998-05-05  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-04-22  0:00 How to Use Abstract Data Types adam
1998-04-23  0:00 ` Jacob Sparre Andersen
1998-04-30  0:00 ` Robert I. Eachus
     [not found]   ` <matthew_heaney-ya023680003004981709380001@news.ni.net>
1998-05-05  0:00     ` Stephen Leake [this message]
1998-05-05  0:00       ` Matthew Heaney
1998-05-06  0:00         ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
1998-04-30  0:00 adam
1998-05-06  0:00 ` Robert I. Eachus
1998-05-04  0:00 adam
1998-05-06  0:00 ` Robert I. Eachus
replies disabled

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