comp.lang.ada
 help / color / mirror / Atom feed
From: Gene <gene.ressler@gmail.com>
Subject: Re: Access idiom
Date: Mon, 21 Jan 2008 21:10:26 -0800 (PST)
Date: 2008-01-21T21:10:26-08:00	[thread overview]
Message-ID: <f8287f4c-3b91-4bee-b23b-eabbcc5c5c0f@p69g2000hsa.googlegroups.com> (raw)
In-Reply-To: 26cde0d5-2c5e-4316-aad7-95fd7269646a@c23g2000hsa.googlegroups.com

On Jan 21, 10:56 pm, Gene <gene.ress...@gmail.com> wrote:
> 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;

I guess this classifies as a "duh..."  Declaring graph edges as
Node_Ptr_Type instead of access Node_Type'Class eliminates need for
type conversion at call site...  So now the 2nd last line above is
just

Xform(P.Left, P.Left);

Entlich ist es ganz sauber!  Fascinating that changing all the
declaration _and_ implementation code from

Xform(P : in Node_type; Rtn : out Node_Ptr_type);

to

Xform(P : access Node_Type; Rtn : out Node_Ptr_type);

was possible with only find-and-replace edits (including deletions of
many .all's) ... good evidence of the orthogonality and consistency of
Ada syntax.

Thanks for remarks that access params incur a performance hit.  In
this app, clean, simple syntax is far more important than performance.

All the best,
Gene



  reply	other threads:[~2008-01-22  5:10 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
2008-01-22  5:10     ` Gene [this message]
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