comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: "access constant" discriminant
Date: 19 Feb 2003 18:17:24 -0800
Date: 2003-02-20T02:17:24+00:00	[thread overview]
Message-ID: <1ec946d1.0302191817.7993c2a0@posting.google.com> (raw)
In-Reply-To: lnJ1a.50689$SD6.3473@sccrnsc03

tmoran@acm.org wrote in message news:<lnJ1a.50689$SD6.3473@sccrnsc03>...
> Can this be done?  How?

I discuss how in my design patterns tutorial, which I'll be giving in
Toulouse in June.

You basically have two options:

1) If your type is limited, you can use the Rosen Trick:


type T;

type Handle_Type (O : access T) is 
   limited null record;

type T is
   limited record
      H : Handle_Type (T'Access);
      <other components here>
   end record;

Now you can do this:

procedure Op (Read_Only : in T) is
 
   Read_Write : T renames Read_Only.H.O.all;
begin
   <modify Read_Write as necessary>
end;

This is completly portable and safe.


2) If your type isn't limited, you can't use the Rosen Trick.  But you
can do something similar if you have a pass-by-reference type.

Tagged types are pass-by-reference, as are types with volatile
components.

So you do something like this:

type T is tagged record ... end record;

package P is
   new System.Address_To_Access_Conversions (T);

procedure Op (Read_Only : in T) is

   Pointer : constant P.Object_Pointer := 
      P.To_Pointer (Read_Only'Address);

   Read_Write : T renames Pointer.all;
begin
   <modify Read_Write as necessary>
end;


You might also be able to use the GNAT 'Unrestricted_Access attribute.

It is indeed unfortunate that method #2 requires that we circumvent
the type system just to cast away const.  The GNAT attribute has the
benefit that strong typing is preserved.

I acually use the Rosen Trick to implement the bounded vectors in the
Charles library.  Incredibly, types whose full view is limited aren't
implicitly aliased when passed as subprogram parameters (that's only
true for tagged types -- don't ask me why...), so you need to use the
Rosen Trick to get an aliased view.

http://home.earthlink.net/~matthewjheaney/charles/charles-vectors-bounded.html
http://home.earthlink.net/~matthewjheaney/charles/index.html



      parent reply	other threads:[~2003-02-20  2:17 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-10  8:26 "access constant" discriminant tmoran
2003-02-10 14:43 ` Frank J. Lhota
2003-02-10 18:57   ` tmoran
2003-02-15 19:17     ` Richard Riehle
2003-02-15 19:59       ` Larry Kilgallen
2003-02-15 23:53         ` Richard Riehle
2003-02-16  1:50           ` Eric G. Miller
2003-02-20  2:23         ` Matthew Heaney
2003-02-20 17:34         ` Stephen Leake
2003-02-21  0:42           ` Matthew Heaney
2003-02-21 10:41             ` Lutz Donnerhacke
2003-02-21 20:21               ` Randy Brukardt
2003-02-23 12:22                 ` Simon Wright
2003-02-24  7:06                 ` Dale Stanbrough
2003-02-24 18:58                 ` Matthew Heaney
2003-02-24 21:05                   ` Randy Brukardt
2003-02-25 14:15                     ` Frank J. Lhota
2003-02-26  1:05                       ` Randy Brukardt
2003-02-24 16:03               ` Matthew Heaney
2003-02-21 15:03             ` Hyman Rosen
2003-02-21 20:09               ` Randy Brukardt
2003-02-21 21:33               ` Matthew Heaney
2003-02-21 20:07             ` Randy Brukardt
2003-02-24 19:11               ` Matthew Heaney
2003-02-24 21:17                 ` Randy Brukardt
2003-02-25 17:49                   ` Richard Riehle
2003-02-20  2:23       ` Matthew Heaney
2003-02-20  2:20     ` Matthew Heaney
2003-02-10 19:26 ` Robert A Duff
2003-02-10 22:27 ` Rod Chapman
2003-02-11  2:00   ` Jeffrey Carter
2003-02-20  2:28   ` Matthew Heaney
2003-02-20  9:45     ` Lutz Donnerhacke
2003-02-20  2:17 ` Matthew Heaney [this message]
replies disabled

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