comp.lang.ada
 help / color / mirror / Atom feed
From: Gene <gene.ressler@gmail.com>
Subject: Re: Access idiom
Date: Mon, 21 Jan 2008 19:56:54 -0800 (PST)
Date: 2008-01-21T19:56:54-08:00	[thread overview]
Message-ID: <26cde0d5-2c5e-4316-aad7-95fd7269646a@c23g2000hsa.googlegroups.com> (raw)
In-Reply-To: 6j5lj.309998$Fc.80333@attbi_s21

On Jan 21, 1:15 pm, "Jeffrey R. Carter"
<spam.jrcarter....@acm.nospam.org> wrote:
> Gene wrote:
>
> > I'm at an impasse developing a graph data structure.  There are many
> > node types derived from a "most general" one.  Most of the node types
> > contain fields that are classwide access to child nodes.  Various
> > primitive procedures operate on nodes, dispatching on unnamed node
> > access types.  In many cases, the operations return such an access
> > child value.  Other "identity" ops just return the dispatching
> > parameter as classwide access.
>
> There should be no need for public access types or values in such a data
> structure. Hiding them should make the package easier to use, easier to
> implement, and safer.

First, I think the collective wisdom has put me straight. Thanks.

Another, more succinct explanation...  The graph has many node types,
all with common ancestor.  Edges are pointers--named or anonymous
access Node_Type'Class; don't care which.  Required is a primitive
"transform" procedure that dispatches on node type and must return a
pointer to an _arbitrary node type_ (i.e. again of type access
Node_Type'Class).  Frequently the transform should just return its
parameter without modification.  Using primitive functions (which
would enable return of anonymous access Node_Type'Class) is not good
due to need for multiple return values.

Jeff, in view of all the above, a named "access all Node_Type'Class"
type seems necessary for the return values.

I failed earlier because the out parameter wouldn't convert
automatically from named to unnamed access, and I didn't realize type
conversion would work on L-values.

Further comments very welcome if there is a better way.  Again,
thanks.

This compiles and dispatches as expected:

---- hoo.ads
package Hoo is

   type Node_Type is abstract tagged null record;
   type Node_Ptr_Type is access all Node_Type'Class;
   procedure XForm(P : access Node_Type;
                   Rtn : out Node_Ptr_Type) is abstract;

   type Binary_Type is new Node_Type with record
      Left, Right : access Node_Type'Class;
   end record;

   overriding
   procedure XForm(P : access Binary_Type;
                   Rtn : out Node_Ptr_Type);
end Hoo;

---- hoo.adb
package body Hoo is

   overriding
   procedure XForm
     (P : access Binary_Type;
      Rtn : out Node_Ptr_Type)
   is
   begin
      Rtn := Node_Ptr_Type(P);
   end XForm;

end Hoo;

---- foo.adb
procedure Foo is
   P : access Binary_Type;
begin
   P := new Binary_Type;
   Xform(P.Left, Node_Ptr_Type(P.Left));
end Foo;



  reply	other threads:[~2008-01-22  3:56 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-20 19:57 Access idiom Gene
2008-01-21  1:01 ` Ludovic Brenta
2008-01-21  4:16   ` Gene
2008-01-21 15:37     ` Robert A Duff
2008-01-22  4:11     ` Randy Brukardt
2008-01-22  4:11     ` Randy Brukardt
2008-01-21  9:05 ` Dmitry A. Kazakov
2008-01-21 18:15 ` Jeffrey R. Carter
2008-01-22  3:56   ` Gene [this message]
2008-01-22  5:10     ` Gene
2008-01-22  9:01     ` Dmitry A. Kazakov
2008-01-22 18:47     ` Jeffrey R. Carter
2008-01-22  4:15   ` Randy Brukardt
2008-01-22  4:15   ` Randy Brukardt
  -- strict thread matches above, loose matches on Subject: below --
2008-01-21  7:12 Grein, Christoph (Fa. ESG)
replies disabled

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