From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Unchecked_Conversion and task pointer.
Date: Thu, 7 Jul 2005 22:18:00 +0200
Date: 2005-07-07T22:18:01+02:00 [thread overview]
Message-ID: <1xuh1gs97pwwg$.1v4w9ruw1n2x$.dlg@40tude.net> (raw)
In-Reply-To: 1120751455.846822.141050@z14g2000cwz.googlegroups.com
On 7 Jul 2005 08:50:55 -0700, e.coli wrote:
> look at this exemple:
> ----------------------------------------------------
> with Ada.Unchecked_Conversion;
> with Ada.Text_Io;
>
> procedure Main is
>
> task type Cain is
> entry Who_Are_You;
> end Cain;
>
> task type Abel is
> entry Who_Are_You;
> end Abel;
>
> type Ptr_Cain is access Cain;
> type Ptr_Abel is access Abel;
>
> task body Cain is
> begin
> loop
> accept Who_Are_You;
> Ada.Text_Io.Put_Line(Item => "I'm Cain");
> end loop;
> end Cain;
>
>
> task body Abel is
> begin
> loop
> accept Who_Are_You;
> Ada.Text_Io.Put_Line(Item => "I'm Abel");
> end loop;
> end Abel;
>
>
> function Mess_Up is
> new Ada.Unchecked_Conversion
> (
> Source => Ptr_Cain,
> Target => Ptr_Abel);
>
> X : Ptr_Cain;
> Y : Ptr_Abel;
>
> begin
> X:= new Cain;
> Y:= new Abel;
> X.Who_Are_You;
> Y.Who_Are_You;
> Y:=Mess_Up(X);
> -- where are your brother?
> X.Who_Are_You;
> Y.Who_Are_You;
> end Main;
> ------------------------------------------------
>
> how work Unchecked_Conversion with task pointer?
Better not to know... (:-))
> the behavior depend by the compiler?
Task types are not tagged in Ada (alas). So if you are trying to have a
kind of task types hierarchy you should use mix-in. Example:
with Ada.Unchecked_Deallocation;
package Persons is
type Corpse is abstract tagged null record;
function Get_Name (Context : Corpse) return String is abstract;
task type Soul (Context : access Corpse'Class) is
entry Who_Are_You;
end Soul;
type Soul_Ptr is access all Soul;
procedure Free is new Ada.Unchecked_Deallocation (Soul, Soul_Ptr);
type Abel is new Corpse with null record;
function Get_Name (Context : Abel) return String;
type Cain is new Corpse with null record;
function Get_Name (Context : Cain) return String;
end Persons;
----------------------
with Ada.Text_IO;
package body Persons is
task body Soul is
begin
loop
select
accept Who_Are_You;
Ada.Text_IO.Put_Line ("I'm " & Get_Name (Context.all));
or terminate;
end select;
end loop;
end Soul;
function Get_Name (Context : Abel) return String is
begin
return "Abel";
end Get_Name;
function Get_Name (Context : Cain) return String is
begin
return "Cain";
end Get_Name;
end Persons;
------------------------
with Persons; use Persons;
procedure Test is
X : aliased Abel;
Y : aliased Cain;
Ptr : Soul_Ptr;
begin
Ptr := new Soul (X'Unchecked_Access);
Ptr.Who_Are_You;
Free (Ptr);
Ptr := new Soul (Y'Unchecked_Access);
Ptr.Who_Are_You;
Free (Ptr);
delay 1.0; -- Let Text_IO task flush all buffers
end Test;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent reply other threads:[~2005-07-07 20:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-07 15:50 Unchecked_Conversion and task pointer e.coli
2005-07-07 18:56 ` Randy Brukardt
2005-07-07 21:39 ` e.coli
2005-07-09 6:23 ` Simon Wright
2005-07-07 20:18 ` Dmitry A. Kazakov [this message]
2005-07-07 21:31 ` Randy Brukardt
2005-07-08 7:48 ` Dmitry A. Kazakov
2005-07-10 1:42 ` Randy Brukardt
2005-07-10 8:26 ` Dmitry A. Kazakov
2005-07-11 18:28 ` Randy Brukardt
2005-07-12 8:13 ` Dmitry A. Kazakov
2005-07-12 20:30 ` Randy Brukardt
2005-07-13 8:11 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox