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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,c7ff90b15f15636c X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Runtime type selection Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com> <2100432e-0c92-4776-9878-51a75fffc90b@k33g2000yqa.googlegroups.com> Date: Tue, 13 Oct 2009 17:50:38 +0200 Message-ID: NNTP-Posting-Date: 13 Oct 2009 17:50:39 CEST NNTP-Posting-Host: 0420d4fb.newsspool4.arcor-online.net X-Trace: DXC=Yc5BS;6_eQgEB;5>eE0T7m4IUK8^;Jm2Coki[6LHn;2LCVn7enW;^6ZC`dIXm65S@:3>o05^QbP]G2lk X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8699 Date: 2009-10-13T17:50:39+02:00 List-Id: On Tue, 13 Oct 2009 08:11:39 -0700 (PDT), xorque wrote: > 'Lo. > > I'm currently still a little lost as to how to use > Ada.Tags.Generic_Dispatching_Constructor > correctly. > > I have the following test package: > > with Ada.Numerics.Discrete_Random; > with Ada.Tags.Generic_Dispatching_Constructor; > with Ada.Tags; > > with Circle; > with Square; > > package body Shape_Selector is > package Tags renames Ada.Tags; > > -- > -- Names associated with types. > -- > > type Shape_Name_t is > (Shape_Class, > Circle_Class, > Square_Class); > > package Shape_Name_Random is new Ada.Numerics.Discrete_Random > (Result_Subtype => Shape_Name_t); > > Random_Context : Shape_Name_Random.Generator; > > Shape_Names : constant array (Shape_Name_t) of Tags.Tag := > (Shape_Class => Shape.Shape_t'Tag, > Circle_Class => Circle.Circle_t'Tag, > Square_Class => Square.Square_t'Tag); > > type Unused_t is null record; > > function Construct > (Params : not null access Unused_t) return Shape.Shape_t'Class is > begin > -- What?! > end Construct; Construct must be a primitive operation of the class instances of which you are going to create. Construct must be overridden in each instance and return this instance's object. The whole idea of is: given the tag of an instance (concrete type), create the object of that type using a primitive operation of that type. The operation is Construct in this case. It must be overridden in each instance. Working example: with Ada.Tags; use Ada.Tags; package P is type T is abstract tagged limited null record; function Create (Params : not null access Integer) return T is abstract; -- Abstract constructor end P; with Ada.Tags; use Ada.Tags; with P; use P; package Q is type T1 is new T with null record; overriding function Create (Params : not null access Integer) return T1; -- Implementation of the constructor type T2 is new T with null record; overriding function Create (Params : not null access Integer) return T2; end Q; package body Q is function Create (Params : not null access Integer) return T1 is begin return (T with null record); end Create; function Create (Params : not null access Integer) return T2 is begin return (T with null record); end Create; end Q; with P; use P; with Q; use Q; with Ada.Tags.Generic_Dispatching_Constructor; procedure Main is function Create is new Ada.Tags.Generic_Dispatching_Constructor (T, Integer, Create); Parameter : aliased Integer; X : T'Class := Create (T1'Tag, Parameter'Access); -- Creates T1 using Q.Create begin null; end Main; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de