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,2ce943dcc1eb3f9c X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!bcklog1.nntp.dca.giganews.com!nntp.scarlet.biz!news.scarlet.biz.POSTED!not-for-mail NNTP-Posting-Date: Sun, 09 Jul 2006 09:33:27 -0500 From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: How to properly clean up an extended, generic structure? References: <44b107b7$0$3629$4d3efbfe@news.sover.net> Date: Sun, 09 Jul 2006 16:33:43 +0200 Message-ID: <87wtamalvc.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:1uaCY4fndETpMXO1jZwE99GYnhM= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 62.235.196.205 X-Trace: sv3-x7a0Ur5+RZ/tbpZTijKyNkeP/l1Hy9ryFprcW9TZS9Lk64DUZNxi1Ttv9pawBvs6AqXPiIbu1xOFf9c!OZgnCqbHut8p5TnYfBNeRiqS0+z5+lYRvawSEq66qxDI6iHdbWeZhhL/DyicFurblzocODqcP3I= X-Complaints-To: abuse@scarlet.be X-DMCA-Complaints-To: abuse@scarlet.biz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:5576 Date: 2006-07-09T16:33:43+02:00 List-Id: Peter C. Chapin writes: > Right now I'm working on a generic package that implements splay > trees. My generic parameters look like: > > generic > type Item_Type is private; > with function "<"(L : Item_Type; R : Item_Type) return Boolean; > package Splay_Tree is ... > > > I've created a procedure Destroy_Tree that uses an instance of > Ada.Unchecked_Deallocation to remove all the nodes in a given > tree. Destroy_Tree is meant to be used when a tree is no longer needed > and it serves the role of a destructor (using C++ terminology). It > occurs to me, though, that Item_Type might have its own clean up > needs. I assume that Unchecked_Deallocation knows nothing about > that. Thus my code currently might be improperly cleaning up the > Item_Types in each tree node. In C++ this is not a problem because > deleting a node invokes the node's destructor (if there is one) so the > issue is handled automatically. My question is how do I best deal with > this in Ada? > > I could pass a clean up procedure for Item_Types as another generic > parameter. However, in cases where no such procedure is necessary (for > example, when Item_Type is just Integer), such a solution seems > awkward. Is there a nicer solution? You do not need to change anything to your Splay_Tree; instead, just pass a controlled type as the Item_Type. A controlled type is a type derived from Ada.Finalization.Controlled(*). You override its Finalize procedure to reclaim the storage for the item; that's the equivalent of a destructor in C++. The language guarantees (ARM 7.6(1)) that the instance of Unchecked_Deallocation will call your Finalize, much like delete in C++ will call your destructor. For more details, see ARM 7.6. (*) or Limited_Controlled, but your generic does not accept limited types, which is probably OK for a container. HTH -- Ludovic Brenta.