From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5c89acd494ea9116 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!eweka.nl!lightspeed.eweka.nl!62.179.104.142.MISMATCH!amsnews11.chello.com!nuzba.szn.dk!news.jacob-sparre.dk!pnx.dk!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Self pointer in limited record Date: Mon, 10 Sep 2007 22:12:47 -0500 Organization: Jacob's private Usenet server Message-ID: References: <1183577468.034566.57830@n60g2000hse.googlegroups.com> <1188578849.187422.280620@50g2000hsm.googlegroups.com> <9fy1xoukz1e3$.h574sqmiauri$.dlg@40tude.net> <1189441670.293887.176810@g4g2000hsf.googlegroups.com> NNTP-Posting-Host: static-69-95-181-76.mad.choiceone.net X-Trace: jacob-sparre.dk 1189480196 23653 69.95.181.76 (11 Sep 2007 03:09:56 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Tue, 11 Sep 2007 03:09:56 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1807 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1896 Xref: g2news2.google.com comp.lang.ada:1875 Date: 2007-09-10T22:12:47-05:00 List-Id: "Dmitry A. Kazakov" wrote in message news:h9v0j8x5uuf3.puwqdmssdfn6$.dlg@40tude.net... > On Mon, 10 Sep 2007 16:27:50 -0000, amado.alves@gmail.com wrote: > > > FWIW, the problem was the following. I am writing a library. I wanted > > to provide a function to the user to allow him to specify the parent > > of an object in a simple way e.g. > > Parent_Object : Object := Lookup (Name = "foo foo"); > > Object : Object := Create (Name => "bla bla", Parent => > > Parent_Object); > > What happens when the parent gets finalized before its child? The point is > that if you have a reference semantics, then probably Object must be a > reference. So access Obkect'Class were a better design. If you want to hide > references, then you should use smart pointers instead. I disagree. Claw uses a design rather like the one proposed here. All of the needed references are created inside of the Claw library, and managed there (with Finalization and Adjust). For instance, if the parent object is finalized, the child object is finalized first (that's necessary because destroying a parent window also destroys any children). If a child window is finalized, it is unlinked from any lists that it is in. Keep in mind that a tagged parameter is always considered aliased. So there is no problem creating any references you need inside the library (assuming that yyour basic types are tagged -- which they usually will be in order to have them Controlled). I strongly believe that having explicit access types in a specification is wrong. Because of limitations in Ada, they can't quite all be eliminated (no "in out" parameters for functions, for instance), but I think they should only be used if there is no viable alternative. (This opinion is not universal.) > > The idea was to have a clean profile like > > Create (Name : String; Parent : Object); > > instead of > > Create (Name : String; Parent : access Object); Yes, except the first probably ought to be Create (Name : String; Parent : Object'Class); (tagged subroutines either should be primitive operations or have class-wide parameters, otherwise there is something suspicious going on. And surely Create is not a primitive of the parent object's type!) > > Inside the function there is code like > > New_Object.Parent := Parent.Self; -- (clean profile) Don't even need that. Just use: New_Object.Parent := Parent'Unchecked_Access; since tagged parameters are always considered aliased. (You do need to keep a list of children in Parent as well so that it can be unlinked from the child if it is finalized first). I personally think self pointers are overrated in Ada; you only really need them to work around language bugs ("in out" parameters for functions again); for everything else, just use the object directly. Randy.