From: Simon Wright <simon@pogner.demon.co.uk>
Subject: Re: Access conversions
Date: 1998/10/13
Date: 1998-10-13T00:00:00+00:00 [thread overview]
Message-ID: <x7v4st9otut.fsf@pogner.demon.co.uk> (raw)
In-Reply-To: F0pu4n.EFE.0.-s@inmet.camb.inmet.com
stt@houdini.camb.inmet.com (Tucker Taft) writes:
> Simon Wright (simon@pogner.demon.co.uk) wrote:
> : I need to write a function
> : function Is_Member (G : Graph'Class; V : Vertex'Class) return Boolean;
[...]
> : return V.Rep.Enclosing = G'Unrestricted_Access;
>
> : but of course this isn't portable.
>
> If, inside Is_Member, you defined:
>
> type Graph_Const_Ptr is access constant Graph'Class;
>
> then you could portably say:
>
> return Graph_Const_Ptr(V.Rep.Enclosing) = G'Access;
>
> because all tagged formal parameters are considered aliased.
And this also works if the parameter is just G : Graph. Cool.
I must say, this area is a touch complex; I tried
return V.Rep.Enclosing = G'Access;
bc-graphs.adb:191:32: object has deeper accessibility level than access type
return V.Rep.Enclosing = Graph_Ptr (G'Access);
bc-graphs.adb:191:32: argument of conversion cannot be access
bc-graphs.adb:191:44: operand type has deeper accessibility level than target
return V.Rep.Enclosing.all'Access = G'Access;
bc-graphs.adb:191:41: two access attributes cannot be compared directly
bc-graphs.adb:191:41: they must be converted to an explicit type for comparison
so as you can see I was scratching around rather reprehensibly.
The right way to do things is usually obviously right, though not
always obvious.
> : If I say
> : function To_Ptr is new Ada.Unchecked_Conversion (System.Address,
> : Graph_Ptr);
> : and then in Is_Member
> : return V.Rep.Enclosing = To_Ptr (G'Address);
>
> : which appears to work as expected in GNAT and ObjectAda 7.1, am I in
> : fact riding for a fall?
>
> The other suggestion of using 'Address on both sides of the
> equality operator seems cleaner, though as mentioned above,
> G'Access for Graph_Const_Ptr is probably the cleanest, as this survives
> the case when V.Rep.Enclosing is null, whereas the .all'Address solution
> would raise Constraint_Error.
Well, V.Rep.Enclosing _shouldn't_ ever be null; there should perhaps
be a validation in there ..
> -Tucker Taft stt@inmet.com http://www.inmet.com/~stt/
> Intermetrics, Inc. Burlington, MA USA
> An AverStar Company
-Simon
next prev parent reply other threads:[~1998-10-13 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-10-12 0:00 Access conversions Simon Wright
1998-10-12 0:00 ` Tucker Taft
1998-10-13 0:00 ` Simon Wright [this message]
1998-10-12 0:00 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox