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,f428ff2031155951 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsfeed.freenet.ag!news.netcologne.de!ramfeed1.netcologne.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Equivalent of dynamic_cast (downcast) for tagged types Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.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: <375fb596-ab12-4cb0-a190-53d62b94b2e4@e9g2000vbi.googlegroups.com> Date: Thu, 27 Jan 2011 17:18:03 +0100 Message-ID: NNTP-Posting-Date: 27 Jan 2011 17:18:04 CET NNTP-Posting-Host: 1d93270c.newsspool1.arcor-online.net X-Trace: DXC=\@5S4Fo<]lROoR1<`=YMgDjhg2m5^1PnP`4Z6[6LHn;2LCV>7enW;^6ZC`4\`mfM[68DC3O<0h5PiP]W6 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:16740 Date: 2011-01-27T17:18:04+01:00 List-Id: On Thu, 27 Jan 2011 07:46:33 -0800 (PST), Maciej Sobczak wrote: > Unfortunately I need to HACK some equivalent of dynamic_cast in Ada. > > There is a limited tagged type T deriving from Limited_Controlled and > Some_Interface as well. > There is also a procedure like this: > > procedure Foo (X : in Some_Interface'Class); > > Inside Foo I would like to check if X is indeed T and if so, I would > like to dynamic_cast to it and execute some operation on T. That > operation is in fact declared in Some_Interface anyway, but I would > like to avoid the dispatching call. I would also like to avoid any > potential data copying, so access types are welcome. You don't need access type, tagged types are by-reference anyway. > In C++ it would look like this: > > void foo(some_interface & x) > { > T * p = dynamic_cast(&x); > if (p != NULL) > { > // indeed x is of type T > p->do_something_for_me(); > } > } if X in T'Class then Do_Something (T (X)); -- No dispatch end if; The above is a bad idea, because X might be a descendant of T which has overridden Do_Something. So you might be looking for this if X'Tag = T'Tag then -- X is of T Do_Something (T (X)); -- No dispatch end if; > If you say that I should not do this because it is not good practice, > then I will quickly answer that GNAT should not randomly call Adjust > on my limited (!) objects. Adjust is not called on limited objects. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de