comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Re: The "()" operator revisited.
Date: Fri, 16 Jan 2004 13:56:53 GMT
Date: 2004-01-16T13:56:53+00:00	[thread overview]
Message-ID: <F4SNb.1$%w2.0@nwrdny03.gnilink.net> (raw)
In-Reply-To: wcczncpgjzv.fsf@shell01.TheWorld.com

"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
news:wcczncpgjzv.fsf@shell01.TheWorld.com...
> The C++ way has some advantages over the Ada way (where the user's
> control is split between Finalize and Adjust) because it's sometimes
> convenient to get your hands on both sides of the assignment at the same
> time.

You can get access to both sides of an assignment in Ada 95, using a variant
of the Rosen trick. The idea is to add a component to the controlled object
that holds an access value for the object itself, e.g.

   type My_Controlled_Access is access all My_Controlled_Type;

   type My_Controlled_Type is new Ada.Finalization.Controlled with
      record
         Self : My_Controlled_Access;    -- Always accesses the containing
object.
         ...
      end record;

   procedure Initialize(Object : in out My_Controlled_Type) is
   begin
      Object.Self := Object'Access;
      ...
   end Initialize;

Now when Adjust is called, the object will be a bit copy of the right hand
side of the assignment. In particular, this gives you a component with an
access value for the right hand side. In Adjust, we can simply save the
value of this component, and reset it to access the left hand of the
assignment (i.e. the current object). Now we have access to both sides of
the assignment, e.g.

   procedure Adjust(Object : in out My_Controlled_Type) is
      Right_Hand_Side : constant My_Controlled_Access := Object.Self;
   begin
      Object.Self := Object'Access;
      -- Now the left hand side of the assignment is in Object,
      -- and the Right Hand side is in the saved value of Object.Self.
      ...
   end Adjust;

Note that although the Rosen trick works, it is, after all, still a trick. I
agree that there are probably cases where it would be nice to be able to
define the ":=" operator in Ada. As someone who has coded in both Ada and
C++, I must however admit that in most cases I have found the Initialize /
Adjust / Finalize coding to be easier than doing the C++ equivalents.





  parent reply	other threads:[~2004-01-16 13:56 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-12 17:53 The "()" operator revisited Frank J. Lhota
2004-01-12 18:38 ` Frank J. Lhota
2004-01-12 22:26 ` Robert A Duff
2004-01-13 16:29   ` Frank J. Lhota
2004-01-13  9:24 ` Dmitry A. Kazakov
2004-01-13 16:44   ` Frank J. Lhota
2004-01-13 17:13     ` Hyman Rosen
2004-01-13 22:27     ` Randy Brukardt
2004-01-14  2:30     ` Stephen Leake
2004-01-14  9:04     ` Dmitry A. Kazakov
2004-01-17  0:15       ` Kenneth Almquist
2004-01-17 21:15         ` Robert A Duff
2004-01-19 10:25         ` Dmitry A. Kazakov
2004-01-13 13:13 ` Marin David Condic
2004-01-13 17:38   ` Warren W. Gay VE3WWG
2004-01-13 19:09     ` Robert A Duff
2004-01-15 17:30       ` Warren W. Gay VE3WWG
2004-01-15 18:11         ` Robert A Duff
2004-01-15 19:36           ` tmoran
2004-01-15 20:35             ` Robert A Duff
2004-01-17  5:48               ` Robert I. Eachus
2004-01-16  1:52           ` Redefining := (was: The "()" operator revisited.) Jeffrey Carter
2004-01-16 21:37             ` Randy Brukardt
2004-01-19 11:33               ` Dmitry A. Kazakov
2004-01-16  3:11           ` The "()" operator revisited Mark A. Biggar
2004-01-16 13:28             ` Hyman Rosen
2004-01-16 16:19             ` Robert A Duff
2004-01-16 18:09             ` Warren W. Gay VE3WWG
2004-01-16 13:56           ` Frank J. Lhota [this message]
2004-01-16 16:14             ` Robert A Duff
2004-01-16 21:29               ` Frank J. Lhota
  -- strict thread matches above, loose matches on Subject: below --
2004-01-13 17:46 amado.alves
2004-01-13 22:21 ` Randy Brukardt
2004-01-13 17:53 amado.alves
2004-01-14  9:09 ` Dmitry A. Kazakov
2004-01-14 12:55   ` Georg Bauhaus
2004-01-14 15:05     ` Dmitry A. Kazakov
2004-01-15  1:21       ` Georg Bauhaus
2004-01-15  8:50         ` Dmitry A. Kazakov
2004-01-15 11:09           ` Georg Bauhaus
2004-01-15 13:23             ` Dmitry A. Kazakov
2004-01-17  6:26               ` Robert I. Eachus
2004-01-14 13:04   ` Hyman Rosen
2004-01-14 15:22 amado.alves
2004-01-14 16:16 ` Dmitry A. Kazakov
replies disabled

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