comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Constructor for a class with a task defined
Date: Thu, 01 Sep 2011 11:13:32 +0100
Date: 2011-09-01T11:13:32+01:00	[thread overview]
Message-ID: <m2r540efjn.fsf@pushface.org> (raw)
In-Reply-To: dc23e007-fd67-41c4-a00b-1c137961b4a0@glegroupsg2000goo.googlegroups.com

"Rego, P." <pvrego@gmail.com> writes:

> I'm trying to implement a Constructor method for a class which have a
> task type pointed to the class, so
>
> in spec:
> type Small_Class;
>
> task type My_Task_Type (This_Small_Class : access Small_Class);
>
> type Small_Class is tagged limited
>       record
>          My_Task      : My_Task_Type (Small_Class'Access);
>       end record;
>
> and in body:
>
> function Construct return access Small_Class is
>    begin
>       return new Small_Class'(My_Task => ???????);
> end Construct;
>
>
> So, how can I do it?

You don't need to do anything.

The code below works (I realise now that I've changed the names, sorry
about that):

   package Small_Class is
      type Instance is limited private;
      type Instance_P is access all Instance;
      function Create (Index : Integer) return Instance_P;
      procedure Start (This : Instance_P);
   private
      task type T (This : not null access Instance) is
         entry Start;
      end T;
      type Instance is limited record
         The_T : T (This => Instance'Access);
         Index : Integer := 0;
      end record;
   end Small_Class;

   with Ada.Text_IO; use Ada.Text_IO;
   package body Small_Class is

      task body T is
      begin
         accept Start;
         Put_Line ("T (" & Integer'Image (This.Index) & " ) started.");
      end T;

      function Create (Index : Integer) return Instance_P
      is
         Result : Instance_P := new Instance;
      begin
         Result.Index := Index;
         return Result;
      end Create;

      procedure Start (This : Instance_P) is
      begin
         This.The_T.Start;
      end Start;

   end Small_Class;

   with Small_Class;
   with Ada.Text_IO; use Ada.Text_IO;
   procedure Small_Class_Test is
      H : Small_Class.Instance_P;
   begin
      H := Small_Class.Create (42);
      Put_Line ("waiting ...");
      delay 1.0;
      Put_Line ("starting ...");
      Small_Class.Start (H);
   end Small_Class_Test;

As others have suggested, you could try compiling using GNAT GPL 2011
and -gnat12 and changing Create to

   function Create (Index : Integer) return Instance_P
   is
   begin
      return new Instance'(Index => Index, others => <>);
   end Create;

but when I tried it I got a bug box (which I shall report directly!)



  parent reply	other threads:[~2011-09-01 10:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-01  0:31 Constructor for a class with a task defined Rego, P.
2011-09-01  1:40 ` Adam Beneschan
2011-09-01  4:54   ` Jeffrey Carter
2011-09-01 10:13 ` Simon Wright [this message]
2011-09-03 15:18   ` Rego, P.
2011-09-03 22:11     ` Robert A Duff
2011-09-05  9:37       ` Georg Bauhaus
2011-09-08 10:22       ` Rego, P.
replies disabled

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