comp.lang.ada
 help / color / mirror / Atom feed
From: xorque <xorquewasp@googlemail.com>
Subject: Re: Runtime type selection
Date: Tue, 13 Oct 2009 08:11:39 -0700 (PDT)
Date: 2009-10-13T08:11:39-07:00	[thread overview]
Message-ID: <2100432e-0c92-4776-9878-51a75fffc90b@k33g2000yqa.googlegroups.com> (raw)
In-Reply-To: 2c15c8a6-b555-4d08-8fe6-f77cb207e7a6@k33g2000yqa.googlegroups.com

'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;

  function Dispatching_Constructor is new
Ada.Tags.Generic_Dispatching_Constructor
    (T           => Shape.Shape_t'Class,
     Parameters  => Unused_t,
     Constructor => Construct);

  --
  -- Select a shape at random from Shape_Names.
  --

  function Select_Shape return Shape.Shape_t'Class is
    Shape_Name : constant Shape_Name_t := Shape_Name_Random.Random
(Random_Context);
    Unused     : aliased Unused_t;
  begin
    return Dispatching_Constructor
      (The_Tag => Shape_Names (Shape_Name),
       Params  => Unused'Unchecked_Access);
  end Select_Shape;

end Shape_Selector;

I have selected a Tag at random from the Shape_Names array and have
passed it
to the Dispatching_Constructor procedure but don't know what I'm
supposed to
put in Construct in this case. I don't see how I can know what type
I'm supposed
to be constructing (or how I would, even if I was to pass the Tag via
Params).



  parent reply	other threads:[~2009-10-13 15:11 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 [this message]
2009-10-13 15:50   ` Dmitry A. Kazakov
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