From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7769c087a4d30c0e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news2.google.com!proxad.net!proxad.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Unchecked_Conversion and task pointer. Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1120751455.846822.141050@z14g2000cwz.googlegroups.com> Date: Thu, 7 Jul 2005 22:18:00 +0200 Message-ID: <1xuh1gs97pwwg$.1v4w9ruw1n2x$.dlg@40tude.net> NNTP-Posting-Date: 07 Jul 2005 22:18:01 MEST NNTP-Posting-Host: 46162070.newsread4.arcor-online.net X-Trace: DXC=9bPSjMDCH^GRG:gi]CKmZG:ejgIfPPldDjW\KbG]kaMHliQbn6H@_EI^SX:IP6;Y_J[6LHn;2LCVNI^><>f3dFYJ1T0eaf_YJAI X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:11949 Date: 2005-07-07T22:18:01+02:00 List-Id: 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