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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,760a0492b97ae06e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-18 16:17:52 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!news-out.visi.com!petbe.visi.com!news2.telebyte.nl!news.completel.fr!fr.ip.ndsoftware.net!teaser.fr!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: How to test object hierarchy Date: 18 Dec 2003 19:15:34 -0500 Organization: Cuivre, Argent, Or Message-ID: References: <93172edb.0312181024.9a536b2@posting.google.com> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: melchior.cuivre.fr.eu.org 1071792946 40689 80.67.180.195 (19 Dec 2003 00:15:46 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Fri, 19 Dec 2003 00:15:46 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: <93172edb.0312181024.9a536b2@posting.google.com> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.3 Precedence: list List-Id: Gateway to the comp.lang.ada Usenet newsgroup List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: archiver1.google.com comp.lang.ada:3544 Date: 2003-12-18T19:15:34-05:00 favierp@sofreavia.fr (Pierre Favier) writes: > I am wondering how to test for two objects which types both derive from a > common root type if one of them derives from the other. Interesting. Here's one way: with Ada.Strings.Unbounded; package Objects is type Root_Object is abstract tagged record Name : Ada.Strings.Unbounded.Unbounded_String; end record; type Object_1 is new Root_Object with record I : Integer; end record; type Object_1_1 is new Object_1 with record J : Integer; end record; end Objects; with Objects; with Ada.Text_Io; use Ada.Text_Io; with Ada.Tags; with Ada.Strings.Fixed; with Ada.Strings.Unbounded; procedure Test_Hierarchy is function "+" (Item : in String) return Ada.Strings.Unbounded.Unbounded_String renames Ada.Strings.Unbounded.To_Unbounded_String; function "+" (Item : in Ada.Strings.Unbounded.Unbounded_String) return String renames Ada.Strings.Unbounded.To_String; procedure Test (Obj1 : in Objects.Root_Object'Class; Obj2 : in Objects.Root_Object'Class) is Obj1_Type_Name : constant String := Ada.Tags.Expanded_Name (Obj1'Tag); Obj2_Type_Name : constant String := Ada.Tags.Expanded_Name (Obj2'Tag); begin if 0 /= Ada.Strings.Fixed.Index (Source => Obj1_Type_Name, Pattern => Obj2_Type_Name) then Put_Line (+Obj1.Name & " is in " & (+Obj2.Name)); else Put_Line (+Obj1.Name & " is not in " & (+Obj2.Name)); end if; end Test; Obj1 : Objects.Object_1 := (+"higher", 1); Obj2 : Objects.Object_1_1 := (+"lower", 2, 3); begin Test (Obj1, Obj2); Test (Obj2, Obj1); end Test_Hierarchy; make -r test_hierarchy.run gnatmake -k -O3 -gnatn -gnatwa -I.. test_hierarchy -largs -bargs -cargs gcc -c -I./ -O3 -gnatn -gnatwa -I.. -I- ..\test_hierarchy.adb gnatbind -aO./ -I.. -I- -x test_hierarchy.ali gnatlink test_hierarchy.ali ./test_hierarchy.exe higher is not in lower lower is in higher It does seem like you aught to be able to do this without string operations. -- -- Stephe