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,e80a1497a689d8a5 X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Ammo-zilla Date: 1999/10/30 Message-ID: <381b7e73_1@news1.prserv.net>#1/1 X-Deja-AN: 542565920 Distribution: world Content-transfer-encoding: 7bit References: <38120FAF.945ADD7D@hso.link.com> <7uutgd$87h$1@nnrp1.deja.com> <19991024.18033546@db3.max5.com> <38189268.43EB150F@mail.earthlink.net> <86ogdjtdwz.fsf@ppp-115-70.villette.club-internet.fr> <7vadsp$8q61@news.cis.okstate.edu> Content-Type: text/plain; charset="US-ASCII" X-Complaints-To: abuse@prserv.net X-Trace: 30 Oct 1999 23:25:39 GMT, 129.37.62.57 Organization: Global Network Services - Remote Access Mail & News Services Mime-version: 1.0 Newsgroups: comp.lang.ada Date: 1999-10-30T00:00:00+00:00 List-Id: In article , 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; 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; 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; 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. 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. > 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 to the ACM mailing list server. -- 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