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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,37d1760a19f7ae57,start X-Google-Attributes: gid103376,public From: dgibson@snoopy.cis.ohio-state.edu (david scott gibson) Subject: Q. on Automatic Package Finalization Date: 1996/09/22 Message-ID: <523tt4INNlu7@snoopy.cis.ohio-state.edu>#1/1 X-Deja-AN: 184610073 organization: The Ohio State University, Department of Computer and Information Science newsgroups: comp.lang.ada Date: 1996-09-22T00:00:00+00:00 List-Id: Hi. Does anyone know of a good way to achieve automatic package finalization in Ada95? I'd like to create a generic package which automatically cleans up its package instance state (possibly including allocated memory, temporary files, etc.) when the package scope ends. The code below (which compiles under GNAT 3.05) is one attempt to achieve this using controlled types. It requires that the representation of state for package P instances be incorporated into an instance (pkg_var) of a controlled type (PC.T) declared in a library-level package (PC). This is not a very satisfactory solution even if it would work. Does anyone have some better ideas for handling automatic package finalization? Dave -- dgibson@cis.ohio-state.edu ---------------------------------------- -- pc.ads with Ada.Finalization; package PC is -- library level since controlled type T is new Ada.Finalization.Controlled with record rep: Integer; -- representation for pkg P's variables end record; -- non-privtate for P direct access procedure Finalize( x: in out T ); end PC; ---------------------------------------- -- pc.adb with Ada.Text_IO; package body PC is procedure Finalize_Package (x: in out T) is begin Ada.Text_IO.Put_Line("Finalizing package P"); x.rep := 0; end Finalize_Package; procedure Finalize( x: in out T ) is begin Finalize_Package(x); end Finalize; end PC; ---------------------------------------- -- p.ads with PC; generic package P is type T is private; procedure Initialize_Package (x: in out PC.T); private type T is record rep: PC.T; end record; end P; ---------------------------------------- -- p.adb with Ada.Text_IO; package body P is pkg_var: PC.T; procedure Initialize_Package (x: in out PC.T) is begin Ada.Text_IO.Put_Line("Initializing package P"); x.rep := 1; end; begin Initialize_Package(pkg_var); end P; ---------------------------------------- -- ptest.adb with P; procedure PTest is -- output: package PI1 is new P; -- Initializing package P begin declare package PI2 is new P; -- Initializing package P begin null; end; -- Finalizing package P end PTest; -- Finalizing package P