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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,aa7b0448abeecbba X-Google-Attributes: gid103376,public From: kst@thomsoft.com (Keith Thompson) Subject: Re: How to print Task_ID type? (GNAT SunOS) Date: 1996/04/07 Message-ID: #1/1 X-Deja-AN: 146226228 sender: news@thomsoft.com (USENET News Admin @flash) x-nntp-posting-host: pulsar references: <316308F0.6375@ee.ubc.ca> <31642F61.1D2B@ee.ubc.ca> organization: Thomson Software Products, San Diego, CA, USA newsgroups: comp.lang.ada originator: kst@pulsar Date: 1996-04-07T00:00:00+00:00 List-Id: In I wrote: [...] > How does Address_To_Access_Conversions help in this case? It only > lets you convert between System.Addess and an access type declared in > Address_To_Access_Conversions; it doesn't provide operations on arbitrary > user-declared access types. There's something else that I had forgotten until just a few minutes ago. Ada 95 allows conversions between access types in some cases, particularly when the target is a general access type (one declared with "access constant" or "access all"), as long as the designated types are the same. (It's more complicated than that; see RM95 4.6 for details.) Since the type Object_Pointer in System.Adddress_To_Access_Conversions is a general access type, you can convert other access types to it. Thus the following generic Image function for access types will work in most cases. (Assume with clauses for System.Storage_Elements and System.Address_To_Access_Conversions). generic type Object(<>) is limited private; type Pointer is access Object; function Image(Item: Pointer) return String; function Image(Item: Pointer) return String is package SSE renames System.Storage_Elements; package Conv is new System.Address_To_Access_Conversions(Object); begin return SSE.Integer_Address'Image (SSE.To_Integer (Conv.To_Address (Conv.Object_Pointer(Item)))); end Image; For example: declare type Pointer is access Integer; function Pointer_Image is new Image(Integer, Pointer); P0: Pointer := null; P1: Pointer := new Integer'(42); begin Put_Line("P0 = " & Pointer_Image(P0)); Put_Line("P1 = " & Pointer_Image(P1)); end; Some things to be aware of: 1. This won't work for "access constant" types. 2. This won't work for access-to-subprogram types (but then I don't think anything short of Unchecked_Conversion will). 3. This won't work if the access type declaration is nested too deeply. It can't be deeper than the declaration of either the instantiation of Image or Image itself. 4. The image of a null access value will be some implementation-dependent integer literal. It's very likely to be 0, but this isn't guaranteed. 5. Robert Dewar's suggestion of using P.all'Address is almost certainly good enough in most cases, as long as you first check that P /= null. It also works for "access constant" types and nested types. So why am I bothering to post this? 8-)} 6..N. Numerous other things I haven't thought of; I should probably know better than to post at this hour. -- Keith Thompson (The_Other_Keith) kst@thomsoft.com TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products 10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718 This sig uses the word "Exon" in violation of the Communications Decency Act.