comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: access to controlled types
Date: 1999/02/20
Date: 1999-02-20T00:00:00+00:00	[thread overview]
Message-ID: <m3emnlfg44.fsf@mheaney.ni.net> (raw)
In-Reply-To: m3n229fokq.fsf@mheaney.ni.net

Matthew Heaney <matthew_heaney@acm.org> writes:

> "Terry J. Westley" <twestley@buffalo.veridian.com> writes:
> 
> > You have a good point.  I'll think about it a bit.  The specific
> > situation is that TASH supports Tcl lists which are heterogenous.
> > 
> > So, when you fetch an element out of the list, it could be an
> > integer, a float, a string, or another list.
> 
> 
> Can your list return a discriminated object? Something like the code
> below.

Actually, I thought of another way to do this, that doesn't require a
List_Item type in the spec.  Function Get_Head can just return the
different types (float, integer, string, list) directly, without the
intermediate type.  

If you know you have just floats, say, then you don't even have to
bother calling the Get_Item_Kind query function.

I don't really know if this is what you had in mind.  But if you want a
reference-counted list, then a private non-limited list type,
implemented as deriving from Controlled, is the way to go.  

This is one example of the fact that you do not have to expose access
types in the spec.  Access types are only for implementing abstractions,
and can thus be hidden from clients.  (There is one exception to this
rule.  See my post about the Interpreter pattern in the patterns archive
at the ACM, which uses an access type as a "handle," to point to a
limited and indefinite type.)



with Ada.Finalization;

package Lists2 is

   type List_Type is private;

   type List_Item_Kind is
      (Float_List_Item,
       Integer_List_Item,
       String_List_Item,
       List_List_Item);

   function Get_Item_Kind
     (List : List_Type) return List_Item_Kind;

   function Get_Head
     (List : List_Type) return Float;

   function Get_Head
     (List : List_Type) return Integer;

   function Get_Head
     (List : List_Type) return String;

   function Get_Head
     (List : List_Type) return List_Type;

private

   type Node_Type;
   type Node_Access is access Node_Type;

   type List_Type is
     new Ada.Finalization.Controlled with record
        Head : Node_Access;
     end record;

   procedure Adjust (List : in out List_Type);

   procedure Finalize (List : in out List_Type);


end Lists2;



 
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

package body Lists2 is

   type List_Item
     (Kind : List_Item_Kind := List_Item_Kind'First) is
      record
         case Kind is
            when Float_List_Item =>
               F : Float;

            when Integer_List_Item =>
               I : Integer;

            when String_List_Item =>
               S : Unbounded_String;

            when List_List_Item =>
               L : List_Type;

         end case;
      end record;


   type Node_Type is
      record
         Item      : List_Item;
         Ref_Count : Natural := 0;
         Next      : List_Type; -- or Node_Access?
      end record;


   function Get_Item_Kind
     (List : List_Type) return List_Item_Kind is
   begin
      return List.Head.Item.Kind;
   end;


   function Get_Head
     (List : List_Type) return Float is
   begin
      return List.Head.Item.F;
   end;


   function Get_Head
     (List : List_Type) return Integer is
   begin
      return List.Head.Item.I;
   end;


   function Get_Head
     (List : List_Type) return String is
   begin
      return To_String (List.Head.Item.S);
   end;


   function Get_Head
     (List : List_Type) return List_Type is
   begin
      return List.Head.Item.L;
   end;



   procedure Adjust (List : in out List_Type) is
   begin
      null; -- adjust ref count
   end;


   procedure Finalize (List : in out List_Type) is
   begin
      null; -- adjust ref count
   end;


end Lists2;




  reply	other threads:[~1999-02-20  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-03  0:00 access to controlled types Terry J. Westley
1999-02-04  0:00 ` Stephen Leake
1999-02-05  0:00   ` Terry J. Westley
1999-02-20  0:00     ` Matthew Heaney
1999-02-20  0:00       ` Matthew Heaney [this message]
1999-02-04  0:00 ` news.oxy.com
1999-02-05  0:00   ` Garb Coll + Heap Cmpctn (was:access to controlled types) Nick Roberts
1999-03-01  0:00 ` access to controlled types Matthew Heaney
replies disabled

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