From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Ammo-zilla
Date: 1999/10/30
Date: 1999-10-30T00:00:00+00:00 [thread overview]
Message-ID: <381b7e73_1@news1.prserv.net> (raw)
In-Reply-To: slrn81mia6.uth.lutz@belenus.iks-jena.de
In article <slrn81mia6.uth.lutz@belenus.iks-jena.de> , lutz@iks-jena.de
(Lutz Donnerhacke) wrote:
> Returning a pointer to an uplevel scope is bad design. If tagged type are
> used (most cases, due to the well known abstract factory pattern), you can
> replace your code in almost all cases in the following way:
[snipped]
>by
> package Factories is
> type Factory is private;
> type Object is tagged private;
> function Make_A_Class_Wide_Object(f: in Factory) return Object'Class;
> ...
> end Factories;
>
> procedure Do_Something(aFactory: in Factory)
> an_Object: Object'Class := Make_A_Class_Wide_Object(aFactory);
> begin
> ...
> end Do_Something;
This will work for only trivial cases, because type Object is
nonlimited. It's *very* common in Ada95 to use access discriminants to
compose abstractions, and this requires a limited type.
In fact, I recommend that for doing class-wide programming, you use a
limited and indefinite type, like this:
package P is
type T (<>) is abstract limited private;
type T_Access is access all T'Class;
<primitive ops takes access params>
procedure Free (O : in out T_Access);
end P;
package P.C is
type NT is new T with private;
function New_NT (...) return T_Access;
<override primitive ops>
end P.C;
Yes, this returns an access object. Yes, you have to manually call
Free. Yes, it's possible to have memory leaks.
But wait! You can use a smart pointer to do the memory reclamation
automatically, with only a small amount of additional syntactic
overhead. The modified packages look like this:
package P is
type T (<>) is abstract limited private;
type T_Access is access all T'Class;
<primitive ops takes access params>
type T_Handle is private;
-- Implemented as a private derivation from Controlled.
function "+" (Handle : T_Handle) return T_Access;
end P;
package P.C is
type NT is new T with private;
function New_NT (...) return T_Handle;
-- Constructor now returns a smart pointer, not raw access object.
<override primitive ops>
end P.C;
I have discussed this issue several times in the Ada95 Design Patterns
list, and there's an article (and several more examples) of how to
implement a smart pointer.
<http://www.acm.org/archives/patterns.html>
> In the rare cases where real pointers must be returned to build up a larger
> dynamic structure the whole structure should be implemented as a seperate
> package providing a Controlled object.
Consider using a smart pointer.
> It took me several weeks to determine this. Thanks to the help of the group
> regulars. I hope I wrote not so much bullshit.
Another resource is the Ada95 Design Patterns mailing list. You can
subscribe by sending the message (body)
subscribe patterns <your full name>
to the ACM mailing list server.
<mailto:listserv@acm.org>
--
Why stop at evolution and cosmology, though? Let's make sure that the
schoolkids of Kansas get a really first-rate education by loosening up
the teaching standards for other so-called scientific ideas that are,
after all, just theories. The atomic theory, for example. The theory of
relativity. Heck, the Copernican theory--do we really know that the
universe doesn't revolve around the earth?
John Rennie, Scientific American, Oct 1999
next prev parent reply other threads:[~1999-10-30 0:00 UTC|newest]
Thread overview: 92+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-10-23 0:00 Ammo-zilla Stanley R. Allen
1999-10-24 0:00 ` Ammo-zilla Aidan Skinner
1999-10-24 0:00 ` Ammo-zilla Robert Dewar
1999-10-24 0:00 ` Ammo-zilla Aidan Skinner
1999-10-25 0:00 ` Ammo-zilla Jean-Pierre Rosen
1999-10-24 0:00 ` Ammo-zilla David Botton
1999-10-28 0:00 ` Ammo-zilla Charles Hixson
1999-10-28 0:00 ` Ammo-zilla Laurent Guerby
1999-10-28 0:00 ` Ammo-zilla Charles Hixson
1999-10-29 0:00 ` Ada and GC (Was Re: Ammo-zilla) Vladimir Olensky
1999-10-29 0:00 ` David Botton
1999-10-31 0:00 ` Vladimir Olensky
1999-10-30 0:00 ` Samuel T. Harris
1999-10-30 0:00 ` David Botton
1999-10-28 0:00 ` Ammo-zilla David Starner
1999-10-29 0:00 ` Ammo-zilla Larry Kilgallen
1999-10-29 0:00 ` Ammo-zilla David Starner
1999-10-29 0:00 ` Ammo-zilla Tucker Taft
1999-10-30 0:00 ` Ammo-zilla Lutz Donnerhacke
1999-10-29 0:00 ` Ammo-zilla Matthew Heaney
1999-10-29 0:00 ` Ammo-zilla Charles Hixson
1999-10-29 0:00 ` Ammo-zilla David Botton
1999-10-29 0:00 ` Ammo-zilla mike
1999-10-29 0:00 ` Ammo-zilla David Botton
1999-10-31 0:00 ` Ammo-zilla Robert Dewar
1999-11-02 0:00 ` Ammo-zilla Charles Hixson
1999-11-03 0:00 ` Ammo-zilla Wes Groleau
1999-11-01 0:00 ` Ammo-zilla Geoff Bull
1999-10-31 0:00 ` Ammo-zilla Robert Dewar
1999-10-31 0:00 ` Ammo-zilla David Starner
1999-11-01 0:00 ` Ammo-zilla Robert Dewar
1999-11-01 0:00 ` Ada and GC. Was: Ammo-zilla Vladimir Olensky
1999-11-01 0:00 ` Vladimir Olensky
1999-11-01 0:00 ` Tucker Taft
1999-11-02 0:00 ` Robert Dewar
1999-11-02 0:00 ` Charles Hixson
1999-11-03 0:00 ` Robert Dewar
1999-11-03 0:00 ` Charles Hixson
1999-11-02 0:00 ` Vladimir Olensky
1999-11-01 0:00 ` Ammo-zilla Robert Dewar
1999-11-01 0:00 ` Ammo-zilla Robert A Duff
1999-11-01 0:00 ` Ammo-zilla Robert Dewar
1999-11-02 0:00 ` Ammo-zilla Robert A Duff
1999-11-02 0:00 ` Ammo-zilla Robert Dewar
1999-11-03 0:00 ` Ammo-zilla Vladimir Olensky
1999-11-03 0:00 ` Ammo-zilla Robert Dewar
1999-11-04 0:00 ` Ada GC (was Re: Ammo-zilla) Vladimir Olensky
1999-11-06 0:00 ` Robert Dewar
1999-11-06 0:00 ` Vladimir Olensky
1999-11-06 0:00 ` Vladimir Olensky
1999-11-06 0:00 ` Robert Dewar
1999-11-09 0:00 ` Robert A Duff
1999-11-10 0:00 ` Vladimir Olensky
1999-11-10 0:00 ` Richard D Riehle
1999-11-10 0:00 ` Robert A Duff
1999-11-10 0:00 ` Nick Roberts
1999-11-12 0:00 ` Robert Dewar
1999-11-12 0:00 ` Robert I. Eachus
1999-11-12 0:00 ` Didier Utheza
1999-11-12 0:00 ` Robert I. Eachus
1999-11-04 0:00 ` Ada GC (was Ammo-zilla) Nick Roberts
1999-11-04 0:00 ` Wes Groleau
1999-11-01 0:00 ` Ammo-zilla Vladimir Olensky
1999-10-31 0:00 ` Ammo-zilla Robert Dewar
1999-10-31 0:00 ` Garbage colletion Lutz Donnerhacke
1999-11-01 0:00 ` Robert Dewar
1999-11-01 0:00 ` Lutz Donnerhacke
1999-11-01 0:00 ` Robert Dewar
1999-11-01 0:00 ` Gnat IDE (was: Garbage colletion) Ted Dennison
1999-11-01 0:00 ` Garbage colletion Lutz Donnerhacke
1999-11-01 0:00 ` Robert Dewar
1999-11-04 0:00 ` Didier Utheza
1999-11-04 0:00 ` David Starner
1999-11-01 0:00 ` Larry Kilgallen
1999-10-30 0:00 ` Ammo-zilla Lutz Donnerhacke
1999-10-30 0:00 ` Matthew Heaney [this message]
1999-10-31 0:00 ` Ammo-zilla Robert Dewar
1999-10-29 0:00 ` Ammo-zilla Robert I. Eachus
1999-10-28 0:00 ` Ammo-zilla Tucker Taft
1999-10-31 0:00 ` Ammo-zilla Brian Rogoff
1999-11-01 0:00 ` Ammo-zilla Robert Dewar
1999-11-01 0:00 ` Ammo-zilla Brian Rogoff
1999-11-02 0:00 ` Ammo-zilla Robert Dewar
1999-11-02 0:00 ` Ammo-zilla Brian Rogoff
1999-11-02 0:00 ` Ammo-zilla Robert A Duff
1999-10-28 0:00 ` Ammo-zilla Matthew Heaney
1999-10-28 0:00 ` Ammo-zilla mitch
1999-10-29 0:00 ` Ammo-zilla Matthew Heaney
1999-10-31 0:00 ` Ammo-zilla Robert Dewar
1999-10-24 0:00 ` Ammo-zilla Matthew Heaney
1999-10-24 0:00 ` Ammo-zilla Robert Dewar
1999-10-24 0:00 ` Ammo-zilla David Botton
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox