comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Runtime type selection
Date: Tue, 13 Oct 2009 17:50:38 +0200
Date: 2009-10-13T17:50:39+02:00	[thread overview]
Message-ID: <sysqyguhd9d2.11v5uels72mrs$.dlg@40tude.net> (raw)
In-Reply-To: 2100432e-0c92-4776-9878-51a75fffc90b@k33g2000yqa.googlegroups.com

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



  reply	other threads:[~2009-10-13 15:50 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-11 16:41 Runtime type selection xorque
2009-10-11 17:02 ` Niklas Holsti
2009-10-11 17:15   ` xorque
2009-10-11 19:54     ` Niklas Holsti
2009-10-12  9:26       ` Georg Bauhaus
2009-10-12 12:02         ` xorque
2009-10-12 23:42       ` Randy Brukardt
2009-10-13  6:50         ` Niklas Holsti
2009-10-13  0:59       ` Adam Beneschan
2009-10-13  7:01         ` Niklas Holsti
2009-10-11 17:15 ` mockturtle
2009-10-11 20:06 ` Dmitry A. Kazakov
2009-10-13 10:45   ` Colin Paul Gloster
2009-10-13 10:17     ` Dmitry A. Kazakov
2009-10-13 15:11 ` xorque
2009-10-13 15:50   ` Dmitry A. Kazakov [this message]
2009-10-13 16:02     ` xorque
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox