Finalization of package instance 
Author Message
 Finalization of package instance

How do I define the finalization of an instantiation of a generic package?

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?

Thanks.

| |,| | | |RuaFranciscoTaborda24RcD 2815-249CharnecaCaparica 351+212976751
|M|A|R|I|O|                                                  mob 939354005
|A|M|A|D|O|DepartmentoDeInformaticaFCT/UNL 2825-114 Caparica 351+212958536
|A|L|V|E|S|                                                  fax 212948541

 Sent via Deja.com http://www.*-*-*.com/
 Before you buy.



Sat, 06 Jul 2002 03:00:00 GMT  
 Finalization of package instance

Quote:
> How do I define the finalization of an instantiation of a generic package?

Declare a controlled type, and put a single of object of the type in the
package body.  The finalization of this object can do whatever package
finalization you want to do.

- Bob



Sat, 06 Jul 2002 03:00:00 GMT  
 Finalization of package instance

Quote:

> 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.

Quote:

> 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



Sat, 06 Jul 2002 03:00:00 GMT  
 Finalization of package instance

Quote:

> 3. In the body of Datalink, declare a controlled type and an object of
> that type; the Finalize for the type does the disconnect.

This is the simplest of your solutions.

Quote:
> 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.

I think there's a new rule (I don't know the AI) that says a
Limited_Controlled object cannot be optimized away.

This issue has come up on CLA before; visit DejaNews to find the thread.

--
Philosophy may be ignored but not escaped; and those who most ignore
least escape.

David Hawkins



Sat, 06 Jul 2002 03:00:00 GMT  
 Finalization of package instance

Quote:



> > 3. In the body of Datalink, declare a controlled type and an object of
> > that type; the Finalize for the type does the disconnect.

> This is the simplest of your solutions.

I forgot to mention that the solution with the visible type requires
that the Initialize procedure for the type do the connect. Sorry.

Quote:

> > 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.

> I think there's a new rule (I don't know the AI) that says a
> Limited_Controlled object cannot be optimized away.

> This issue has come up on CLA before; visit DejaNews to find the thread.

I recall the thread, with the information that an unused controlled type
could be optimized away, but that GNAT doesn't. If there's a new AI
preventing it in general, that's good news.

--
Jeff Carter
"English bed-wetting types."
Monty Python & the Holy Grail



Sun, 07 Jul 2002 03:00:00 GMT  
 Finalization of package instance
Jeff,

I recall a message of yours on comp.lang.ada, a couple of days ago, saying
approximately that :
- Ada needs 41 statements to implement a function point, while
- C needs 128 statements.

I'm right in the process of trying to guess a whole development effort, for a
project that may actually start in the next coming months. I'm pushing for an Ada
choice as far as the language is concerned. For this I need concrete arguments.

My question is : where do these figures come from ? And as a bonus : where could
I find related ratios on mean project cost related to either line of code or
function point ?

Difficult question I agree.
But thanks in advance for any clue.

-- Herv



Sun, 07 Jul 2002 03:00:00 GMT  
 Finalization of package instance
Look here:
Linkname: What Are Function Points? (Capers Jones)
URL: http://www.spr.com/library/0funcmet.htm
and follow the link about "language level"

The whole paper should be of interest to you.

Other links about metrics can be found on my Ada page.

A recent discussion on comp.software-eng had the topic of "estimating
programming effort." You maight want to try deja.

All in all, I am quite skeptic about all the "scientific methods." But
sometimes this is all you have. A good estimate by someone who knows the
system, env. etc. seems to me the best way...


http://purl.oclc.org/NET/ehudlamm <== My home on the web
Check it out and subscribe to the E-List- for interesting essays and more!



Sun, 07 Jul 2002 03:00:00 GMT  
 Finalization of package instance


Quote:
> Jeff,
> I'm right in the process of trying to guess a whole development
> effort, for a project that may actually start in the next coming
> months. I'm pushing for an Ada choice as far as the language is
> concerned. For this I need concrete arguments.
> My question is : where do these figures come from ? And as a bonus :
> where could I find related ratios on mean project cost related to
> either line of code or function point ?

Most metrics folks will tell you that any absolute figures are
meaningless outside of the orginazation in which they were collected.

That being said, if you are comparing the relative costs of development
in Ada vs. C, the following report is the one to read:

http://www.rational.com/sitewide/support/whitepapers/dynamic.jtmpl?doc_k
ey=337&borschtid=30404058791780322405

(If you have problems with the link, go to www.rational.com and search
for a paper titled "Comparing Development Costs of C and Ada")

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 07 Jul 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Package require fails with multiple instances of Img package

2. Q. on Automatic Package Finalization

3. Problems with Finalization and Generic Packages

4. instances of classes and instances of instances

5. Class instance variable in package

6. Package Instances???

7. Package instance???

8. Multiple Instances of a package

9. Can event from instance propagate to other instances?

10. range(Instance) vs. foo[Instance]

11. Instances incorrectly sharing instance variables?

12. Convert single instance tcl program to multiple instances...

 

 
Powered by phpBB® Forum Software