comp.lang.ada
 help / color / mirror / Atom feed
From: Jeff Carter <jrcarter010@earthlink.net>
Subject: Re: Finalization of package instance
Date: 2000/01/18
Date: 2000-01-18T00:00:00+00:00	[thread overview]
Message-ID: <388496B3.7E36F781@earthlink.net> (raw)
In-Reply-To: Pine.LNX.4.10.10001181512310.27452-100000@lexis.di.fct.unl.pt

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




  reply	other threads:[~2000-01-18  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-18  0:00 Finalization of package instance Mario Amado Alves
2000-01-18  0:00 ` Jeff Carter [this message]
2000-01-18  0:00   ` Matthew Heaney
2000-01-19  0:00     ` Jeff Carter
2000-01-19  0:00   ` Statements per function point Herv� BITTEUR
2000-01-19  0:00     ` Ehud Lamm
2000-01-19  0:00     ` Ted Dennison
2000-01-18  0:00 ` Finalization of package instance Robert A Duff
replies disabled

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