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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,30821ecf97416798 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-09-24 09:50:04 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!small.news.tele.dk!213.56.195.71!fr.usenet-edu.net!usenet-edu.net!enst!enst.fr!not-for-mail From: Samuel Tardieu Newsgroups: comp.lang.ada Subject: Re: How Can I Create A Class Dynamically Date: Mon, 24 Sep 2001 18:48:54 +0200 Organization: RFC 1149 (see http://www.rfc1149.net/) Sender: comp.lang.ada-admin@ada.eu.org Message-ID: References: <1bJr7.11813$po4.573186@e420r-atl1.usenetserver.com> Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit X-Trace: avanie.enst.fr 1001350203 74854 137.194.161.2 (24 Sep 2001 16:50:03 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Mon, 24 Sep 2001 16:50:03 +0000 (UTC) To: comp.lang.ada@ada.eu.org Return-Path: Content-Disposition: inline In-Reply-To: <1bJr7.11813$po4.573186@e420r-atl1.usenetserver.com> User-Agent: Mutt/1.3.22.1i Precedence: special-delivery X-WWW: http://www.rfc1149.net/sam X-Mail-Processing: Sam's procmail tools X-ICQ: 21547599 X-Sam-Laptop: yes Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.4 X-Reply-To: Samuel Tardieu List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , List-Archive: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:13294 Date: 2001-09-24T18:48:54+02:00 On 24/09, Anthony E. Glover wrote: | I have a class hierarchy defined and I would like to be able to read a | string and then dynamically creating a pointer to an object of the type | specified by the string. Similar I think to class loading and class | factories in Java. Can this be done in Ada95. If so, how. I have looked at | the Ada.Tags packages and this looks like it might be helpfull, but I'm not | quite sure how to make use of it. I would basically like to be able to avoid | having to specify a case statement or if block when initially creating my | objects. Let's assume that the root of your hierarchy is called Root. One modular and portable way which would work with any language supporting elaboration would be to: - have a package (probably using a protected object behind the scene) which exports a Register subprogram such as: type Root_Access is access all Root'Class; type Factory is access function return Root_Access; procedure Register (Name : String; F : Factory); Register would use a hash table to associate names to factories. - in this package, also have a Create subprogram looking like: function Create (Name : String) return Root_Access; whose body would look like: return Lookup (Name) .all; (with of course added checks) where Lookup looks up in the hash table - have each type register itself during the elaboration by declaring a factory function and registering it using Register This way, you get a factory which knows only the types present in your final application. Note that you are likely to run into elaboration issues with the root type if your "factory-like package" is not the same as the one where the root type is declared. If your root type is abstract (or more generally if it cannot be used with a factory), then you can more easily solve elaboration problems. Also, it may be worth making a generic package to implement this solution. HopI hope you got the idea.e this helps. Sam