comp.lang.ada
 help / color / mirror / Atom feed
From: woodruff@tanana.llnl.gov (John Woodruff)
Subject: Protected types made of ADT's, and Passive Iterators
Date: 1998/09/04
Date: 1998-09-04T00:00:00+00:00	[thread overview]
Message-ID: <udpvdbl8uf.fsf@tanana.llnl.gov> (raw)

I like to ask advice about an Ada programming problem I've been working.

By good fortune, I possess an inventory of well-made abstract data
types. And I want to use the objects provided by these ADT's in a
tasking-safe environment. So I determined that I might wrap the ADT into
a protected type.

Here's what I started with:

generic
    type Item is private;
package List is
    type Instance is private;
    procedure Insert (Into_List : in out Instance; The_Item : in Item);
    -- and lots more
private
    type Instance is new Integer;  -- of course not (but it compiles!)
end List;

I can make a generic package that encapsulates a protected instance of
List in a fairly straightforward way:

with List;
generic
    with package Inner_List is new List (<>);
package Prot_List is
    protected type Safe_List is
        entry Insert (The_Item : in Inner_List.Item);
    private
        In_Use : Boolean := False;
        Store : Inner_List.Instance;
    end Safe_List;
end Prot_List;

package body Prot_List is
    protected body Safe_List is
        entry Insert (The_Item : in Inner_List.Item) when not In_Use is
        begin
            In_Use := True;
            Inner_List.Insert (Into_List => Store, The_Item => The_Item);
            In_Use := False;
        exception
            when others =>
                In_Use := False;
        end Insert;
    end Safe_List;
end Prot_List;


So far, so good!

BUT the base list ADT provided a passive iterator, and I'd like to offer
that capability to the clients of my protected ADT as well.

Here is the way the base ADT specifies this ability:

generic
    type Item is private;
package List is
    type Instance is private;
    procedure Insert (Into_List : in out Instance; The_Item : in Item);

    generic
        with procedure Process
                          (The_Item : in Item; Continue : in out Boolean);
    procedure Iterate (Over_List : in Instance);
private
  -- as before
end List;

So my question is "How shall I enhance my Prot_List so it can define a
passive iterator?"

The only way I have found to now is to specify an additional parameter
when instantiating Prot_List - and let this parameter be a procedure to
be the iterating action:

with List;
generic
    with package Inner_List is new List (<>);
    with procedure Action (The_Item : in Inner_List.Item;
                           Continue : in out Boolean) is <>;
package Prot_List is
    protected type Safe_List is
    --  only change from before is to provide:
        procedure Iterate;
    end Safe_List;

end Prot_List;
 
This might be good for "partial credit" but I'm not satisfied: Iterate
in the protected list is more tightly bound to the abstract data type
than was the case in List.

Only *exactly one* iterator Action can ever be specified for an instance
of the protected list. Unlike the base ADT, it is not possible to use an
instance of the protected list object *without* specifying an Action
(that might never be used!).

And the "procedure iterate" is a part of the package Prot_List, unlike
in the base ADT where I am able to specify an Action is some far-away
scope.

Any advice that will make the full facility of passive iterator
available in the task-safe environment?

--
John Woodruff                                             N I F   \ ^ /
Lawrence Livermore National Lab                         =====---- < 0 >
925 422 4661                                                      / v \




             reply	other threads:[~1998-09-04  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-04  0:00 John Woodruff [this message]
1998-09-05  0:00 ` Protected types made of ADT's, and Passive Iterators Matthew Heaney
1998-09-05  0:00   ` Tom Moran
1998-09-07  0:00   ` Mats Weber
1998-09-17  0:00     ` Matthew Heaney
1998-09-05  0:00 ` Tom Moran
1998-09-07  0:00 ` Mats Weber
replies disabled

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