comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Extending a type and Finalization
Date: Fri, 5 Jun 2009 11:11:25 +0200
Date: 2009-06-05T11:11:25+02:00	[thread overview]
Message-ID: <cdg604olt56l$.gpep3ypgnv35.dlg@40tude.net> (raw)
In-Reply-To: e805e0d7-bc8a-4061-8692-400aa55e5bfa@t10g2000vbg.googlegroups.com

On Thu, 4 Jun 2009 14:18:41 -0700 (PDT), Adam Beneschan wrote:

> On Jun 4, 1:06�pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
>> On Thu, 4 Jun 2009 12:33:04 -0700 (PDT), Adam Beneschan wrote:
>>> In addition to making an individual component a Limited_Controlled
>>> type, you can use trickery using access discriminants to allow the
>>> Initialize and Finalize routines to access the entire record.
>>> Assuming P.T is limited, you can define something like:
>>
>>> � �type Control_Type (Ref : access P.T'Class) is new
>>> � � � �Ada.Finalization.Limited_Controlled with null record;
>>
>>> � �overriding procedure Initialize (Obj : in out Control_Type);
>>> � �overriding procedure Finalize � (Obj : in out Control_Type);
>>
>>> And then when defining TT:
>>
>>> � �type TT is new P.T with record
>>> � � � Control : Control_Type (TT'Access);
>>> � � � ...
>>> � �end record;
>>
>>> Then when you declare an object of type TT, Initialize will be called
>>> on the Control component, and the access discriminant Ref will give
>>> you access to the entire record inside the Initialize and Finalize
>>> routines.
>>
>>> (This idea isn't mine. �It might be Matthew Heaney's, but I'm not
>>> sure. �The Control_Type declaration was adapted from a "shapes"
>>> demonstration program, but I'm not sure where it came from.)
>>
>> I am not sure if this pattern is safe. What gives a guaranty that other
>> components of TT are initialized before the Initialize gets called on
>> Control and finalized after the Finalize gets called?
> 
> RM 7.6(12)

Then it should rather be:

 � �type TT is new P.T with record
 � � � ...
 � � � Control : Control_Type (TT'Access);  -- The last component
 � �end record;

BTW, it seems that this pattern has a more consistent behavior upon
extension than Ada.Finalization itself:

   type TTT;
 � type TTT_Hook (Ref : not null access TTT'Class) is new
 � � �Ada.Finalization.Limited_Controlled with null record;
 � overriding procedure Initialize (Obj : in out TTT_Hook);
 � overriding procedure Finalize � (Obj : in out TTT_Hook);

 � type TTT is new TT with record
 � � �...
 � � �Hook : TTT_Hook (TTT'Access);
 � end record;

Unlikely to Ada.Finalize from the Initialize of TTT_Hook we do not call to
the Initialize of Control_Type, because it has been called.

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



  reply	other threads:[~2009-06-05  9:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-04 17:45 Extending a type and Finalization pascal.malaise
2009-06-04 18:00 ` Hibou57 (Yannick Duchêne)
2009-06-04 19:33   ` Adam Beneschan
2009-06-04 20:06     ` Dmitry A. Kazakov
2009-06-04 21:18       ` Adam Beneschan
2009-06-05  9:11         ` Dmitry A. Kazakov [this message]
2009-06-05 14:48           ` Adam Beneschan
2009-06-05 17:15             ` Dmitry A. Kazakov
2009-06-05 11:29     ` malaise
2009-06-05 12:21       ` Ludovic Brenta
2009-06-05 13:02         ` malaise
2009-06-05 13:14           ` Ludovic Brenta
2009-06-05 13:32             ` malaise
2009-06-15  6:30 ` AdaMagica
replies disabled

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