comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Access idiom
Date: Tue, 22 Jan 2008 10:01:41 +0100
Date: 2008-01-22T09:52:29+01:00	[thread overview]
Message-ID: <7z96v1lwnp09.advcjt2uyly.dlg@40tude.net> (raw)
In-Reply-To: 26cde0d5-2c5e-4316-aad7-95fd7269646a@c23g2000hsa.googlegroups.com

On Mon, 21 Jan 2008 19:56:54 -0800 (PST), Gene wrote:

> 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_Type (<>) is abstract limited tagged private;

1. I would add limited to indicate that are nodes never copied.

2. Maybe Ada.Finalization.Limited_Controlled should be used as the base.

3. "null record" looks much like an interface. You are in Ada 2005, so
Node_Type could become an interface.

>    type Node_Ptr_Type is access all Node_Type'Class;

   type Node_Ptr_Type is access Node_Type'Class;

I don't like general access types. What if somebody allocated a node on the
stack linked it with another in the pool and then left the scope? Also,
trees can often profit from arena and mark-and-sweep allocators, so you
could do:

   for Node_Ptr_Type'Storage_Pool use My_Nice_Arena_Pool;

>    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;

   Left, Right : Node_Ptr_Type;

We have an access type for that, and we don't want nodes having children
allocated on the stack. (Not-null constraint can be added in Ada2005 if
required.)

>    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);

OK, that will be an unchecked conversion with pool-specific access types.
This is a weaknesses of my approach, but I usually buy that. When
conversions become too frequent I use the Rosen trick.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2008-01-22  9:01 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
2008-01-22  9:01     ` Dmitry A. Kazakov [this message]
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