Re: Unchecked_Conversion and task pointer.



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
.



Relevant Pages

  • Unchecked_Conversion and task pointer.
    ... task type Cain is ... task type Abel is ... end loop; ...
    (comp.lang.ada)
  • Re: The Borrowings of the Koran
    ... Here's the conclusion from "On The Sources Of The Story Of Cain & Abel ... to have been borrowed from Jewish sources like the Targum ... "It is not said here blood in the singular, but blood in the plural, ...
    (soc.religion.islam)
  • Re: OT: In the Begining
    ... Cain turned the tape of, informing Abel of the hose ... Abel wanted Adam to go and get more cleaner as ...
    (uk.politics.misc)
  • Re: The Borrowings of the Koran
    ... sincere seekers can refer to "Refutation Of The Borrowing ... and "On The Sources Of The Story Of Cain & Abel In The Qur'an", ... Abel as narrated in the Qur'an were borrowed from the Jewish sources ...
    (soc.religion.islam)
  • RE: Cain a& Abel Question
    ... Subject: Cain a& Abel Question ... Your browser negotiates an SSL connection with C&A. C&A negotiates ... that are enforced to protect WLANs from known vulnerabilities and threats. ...
    (Pen-Test)