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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,555115ba813101ad X-Google-Attributes: gid103376,public From: Jeff Carter Subject: Re: Finalization of package instance Date: 2000/01/18 Message-ID: <388496B3.7E36F781@earthlink.net>#1/1 X-Deja-AN: 574327431 Content-Transfer-Encoding: 7bit References: X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-ELN-Date: Tue Jan 18 10:37:42 2000 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 948220662 38.29.67.193 (Tue, 18 Jan 2000 10:37:42 PST) Organization: EarthLink Network, Inc. MIME-Version: 1.0 Reply-To: jrcarter@acm.org NNTP-Posting-Date: Tue, 18 Jan 2000 10:37:42 PST Newsgroups: comp.lang.ada Date: 2000-01-18T00:00:00+00:00 List-Id: Mario Amado Alves wrote: > > How do I define the finalization of an instantiation of a generic package? Note that the instantiation of a generic package IS a package. The fact that you have a generic is not important here. Instantiation takes place at compile time. > > For example, generic package Datalink initializes a connection with a > database server; when the package instance dies, the connection must be > closed (cleanly) via the proper procedure call. How does one automatize > this? E.g. > > generic > Database_Server_Address: String; > package Datalink is > ... > end Datalink; > > The body: > > package body Datalink is > > ... > > begin > > Connect(Database_Server_Address); > > finalize -- HERE, I WHICH THIS WAS POSSIBLE!!!!!!!!!!!!!!!!!!!!!!!!!!!!! > > Disconnect(Database_Server_Address); > > end Datalink; > > Then, > > procedure Main is > package Main_Data_Server is > new Datalink("some.address.net"); > begin > ... > end Main > > Now, when Main has complete running the disconnection should have been done > automatically. Is there an idiom to accomplish this in Ada? Ada provides finalization for types derived from Ada.Finalization.Controlled and Ada.Finalization.Limited_Controlled, but not for packages. You have a couple of workarounds: 1. Provide a Disconnect operation and require the client to call it. I find this unacceptable, but many systems have been successfully implemented with this kind of finalization. 2. I assume that package Datalink provides some visible operations, represented by "..." in your example. Provide a limited controlled type that is a parameter to these operations: with Ada.Finalization; package Datalink is type Handle (Server_Address : access String) is limited private; procedure Op (Server : in Handle; ...); ... private -- Datalink type Handle (Server_Address : access String) is new Ada.Finalization.Limited_Controlled with null record; procedure Finalize (Object : in out Handle); -- Disconnect here end Datalink; with Datalink; procedure Something_That_Uses_Data_Link is Server_Address : aliased String := "some.address.net"; Server : Datalink.Handle (Server_Address => Server_Address'access); begin Datalink.Op (Server => Server, ...); end; I think this may be the best option. 3. In the body of Datalink, declare a controlled type and an object of that type; the Finalize for the type does the disconnect. Datalink would be generic, so each instance would have its own controlled object. This can get you into trouble because a compiler may optimize away an unused variable. -- Jeff Carter "Hello! Smelly English K...niggets." Monty Python & the Holy Grail