comp.lang.ada
 help / color / mirror / Atom feed
From: kst@thomsoft.com (Keith Thompson)
Subject: Re: How to print Task_ID type? (GNAT SunOS)
Date: 1996/04/07
Date: 1996-04-07T00:00:00+00:00	[thread overview]
Message-ID: <DpHsuw.JsM@thomsoft.com> (raw)
In-Reply-To: DpF82v.FBz@thomsoft.com

In <DpF82v.FBz@thomsoft.com> 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.




  parent reply	other threads:[~1996-04-07  0:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-03  0:00 How to print Task_ID type? (GNAT SunOS) Greg Bond
1996-04-04  0:00 ` Robert Dewar
1996-04-04  0:00   ` Greg Bond
1996-04-04  0:00   ` Greg Bond
1996-04-05  0:00     ` Keith Thompson
1996-04-05  0:00       ` Greg Bond
1996-04-05  0:00       ` Robert Dewar
1996-04-06  0:00         ` Keith Thompson
1996-04-06  0:00           ` Robert Dewar
1996-04-07  0:00           ` Keith Thompson [this message]
1996-04-05  0:00     ` Robert Dewar
1996-04-05  0:00   ` Laurent Guerby
1996-04-04  0:00     ` Joel Sherrill
1996-04-06  0:00       ` Fergus Henderson
1996-04-06  0:00         ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox