comp.lang.ada
 help / color / mirror / Atom feed
* Constructor for a class with a task defined
@ 2011-09-01  0:31 Rego, P.
  2011-09-01  1:40 ` Adam Beneschan
  2011-09-01 10:13 ` Simon Wright
  0 siblings, 2 replies; 8+ messages in thread
From: Rego, P. @ 2011-09-01  0:31 UTC (permalink / raw)


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?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  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
  1 sibling, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2011-09-01  1:40 UTC (permalink / raw)


On Aug 31, 5:31 pm, "Rego, P." <pvr...@gmail.com> wrote:
> 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?

I would think

  return new Small_Class'(My_Task => <>);

should work.

                       -- Adam





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  2011-09-01  1:40 ` Adam Beneschan
@ 2011-09-01  4:54   ` Jeffrey Carter
  0 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2011-09-01  4:54 UTC (permalink / raw)


On 08/31/2011 06:40 PM, Adam Beneschan wrote:
>
> I would think
>
>    return new Small_Class'(My_Task =>  <>);
>
> should work.

That's equivalent to

return new Small_Class'(others => <>);

which is equivalent to

return new Small_Class;

-- 
Jeff Carter
"You a big nose have it."
Never Give a Sucker an Even Break
107



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  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 10:13 ` Simon Wright
  2011-09-03 15:18   ` Rego, P.
  1 sibling, 1 reply; 8+ messages in thread
From: Simon Wright @ 2011-09-01 10:13 UTC (permalink / raw)


"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!)



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  2011-09-01 10:13 ` Simon Wright
@ 2011-09-03 15:18   ` Rego, P.
  2011-09-03 22:11     ` Robert A Duff
  0 siblings, 1 reply; 8+ messages in thread
From: Rego, P. @ 2011-09-03 15:18 UTC (permalink / raw)


I tried all the suggestions (thanks to all!), but the ones which uses "<>" I got the message box when I included other constructor subclasses inside Small_Class (but worked pretty well in the simpler version):
raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:423
gnatmake: "D:\src\mypkg.adb" compilation error

The one suggested by Simon worked as well too (and it was which I choose to use), but maybe a basic question: what is the difference between using the direct access approach in a local procedure and using the _P approach? I mean, before I had tried to use

   function Construct return access Small_Class is
      B : access Small_Class := new Small_Class;
   begin
      return B;
   end Construct;

and got the message "cannot convert local pointer to non-local access type"

so why does the (type Small_Class_Acc is access all Small_Class;) code 

   function Construct return access Small_Class is
      B : Small_Class_Acc := new Small_Class;
   begin
      return B;
   end Construct;

works? Should not it get me the same msg?




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  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.
  0 siblings, 2 replies; 8+ messages in thread
From: Robert A Duff @ 2011-09-03 22:11 UTC (permalink / raw)


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

>    function Construct return access Small_Class is
>       B : access Small_Class := new Small_Class;
>    begin
>       return B;
>    end Construct;
>
> and got the message "cannot convert local pointer to non-local access type"

The type of B is declared inside Construct, whereas the return type
is declared outside, so it thinks you might be creating a dangling
pointer.  Hence the error.

I suggest you avoid anonymous access types.  They are confusing.
And the rules change in Ada 2012.

> so why does the (type Small_Class_Acc is access all Small_Class;) code 
>
>    function Construct return access Small_Class is
>       B : Small_Class_Acc := new Small_Class;
>    begin
>       return B;
>    end Construct;
>
> works? Should not it get me the same msg?

No, because now B has a global pointer type.

I suggest:

   function Construct return Small_Class_Acc is
      B : Small_Class_Acc := new Small_Class;
   begin
      return B;
   end Construct;

That way, you know where Small_Class_Acc is declared
(probably at library level).

- Bob



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  2011-09-03 22:11     ` Robert A Duff
@ 2011-09-05  9:37       ` Georg Bauhaus
  2011-09-08 10:22       ` Rego, P.
  1 sibling, 0 replies; 8+ messages in thread
From: Georg Bauhaus @ 2011-09-05  9:37 UTC (permalink / raw)


On 04.09.11 00:11, Robert A Duff wrote:

> I suggest you avoid anonymous access types.  They are confusing.
> And the rules change in Ada 2012.

+1. I'll put a frame around this.

Is this issue worth a compiler switch? Maybe -gnat^ ?




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Constructor for a class with a task defined
  2011-09-03 22:11     ` Robert A Duff
  2011-09-05  9:37       ` Georg Bauhaus
@ 2011-09-08 10:22       ` Rego, P.
  1 sibling, 0 replies; 8+ messages in thread
From: Rego, P. @ 2011-09-08 10:22 UTC (permalink / raw)


Got it. I shall use this approach. Thanks Bob.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-09-08 10:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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.

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