From: Duncan Sands <baldrick@free.fr>
To: comp.lang.ada@ada-france.org
Cc: Randy Brukardt <randy@rrsoftware.com>
Subject: Re: Return_By_Reference or Return_By_Copy (GNAT bug?)
Date: Sat, 1 Jan 2005 23:00:56 +0100
Date: 2005-01-01T23:00:56+01:00 [thread overview]
Message-ID: <mailman.21.1104616882.527.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <lvKdndhnk8OQWEjcRVn-iw@megapath.net>
Hi Randy,
> A_Type certainly is return-by-reference.
thanks for confirming.
> function Get_an_A return access A_Type;
>
> Note that this makes the reference return explicit (no magical rules to page
> through).
I was playing around with return by reference to see if it is possible to
get unserialized access to a protected variable using the Rosen trick (it is,
see example below, especially the protected object P in package C). Will this
be illegal in Ada 2005?
All the best,
Duncan.
package B is
type A_Type is limited private;
function Get_The_A return A_Type;
procedure Increment (An_A : A_Type);
procedure Print (An_A : A_Type);
private
type R_Type (An_A : access A_Type) is limited null record;
type A_Type is record
R : R_Type (A_Type'Access);
I : Integer := 0;
end record;
end;
with Ada.Text_IO; use Ada.Text_IO;
package body B is
The_A : A_Type;
function Get_The_A return A_Type is
begin
return The_A;
end;
procedure Increment (An_A : A_Type) is
begin
An_A.R.An_A.I := An_A.R.An_A.I + 1;
end;
procedure Print (An_A : A_Type) is
begin
Put_Line (An_A.I'Img);
end;
end;
with B; use B;
package C is
protected P is
function Get_A return A_Type;
private
P_A : A_Type;
end P;
end;
package body C is
protected body P is
function Get_A return A_Type is
begin
return P_A;
end Get_A;
end P;
end;
with Ada.Text_IO; use Ada.Text_IO;
with B; use B;
with C; use C;
procedure Y is
procedure Look is
The_A_Reference : A_Type renames P.Get_A;
begin
Print (The_A_Reference);
Increment (The_A_Reference);
Print (The_A_Reference);
end;
begin
Put_Line ("Mechanism:" & Integer'Image (Get_The_A'Mechanism_Code));
New_Line;
Look;
New_Line;
Look;
end;
next prev parent reply other threads:[~2005-01-01 22:00 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-31 11:07 Return_By_Reference or Return_By_Copy (GNAT bug?) Duncan Sands
2004-12-31 21:32 ` Randy Brukardt
2005-01-01 22:00 ` Duncan Sands [this message]
2005-01-03 23:11 ` Randy Brukardt
2005-01-04 4:20 ` Larry Kilgallen
2005-01-04 23:27 ` Randy Brukardt
2005-01-08 13:06 ` Duncan Sands
2005-01-10 21:05 ` Randy Brukardt
2005-03-08 16:12 ` Duncan Sands
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox