comp.lang.ada
 help / color / mirror / Atom feed
* Access conversions
@ 1998-10-12  0:00 Simon Wright
  1998-10-12  0:00 ` Matthew Heaney
  1998-10-12  0:00 ` Tucker Taft
  0 siblings, 2 replies; 4+ messages in thread
From: Simon Wright @ 1998-10-12  0:00 UTC (permalink / raw)


I need to write a function

  function Is_Member (G : Graph'Class; V : Vertex'Class) return Boolean;

where there is a type

  type Graph_Ptr is access all Graph'Class;

and a Vertex has a component Rep with a component Enclosing of type
Graph_Ptr which accesses its enclosing Graph.

In GNAT I can say in Is_Member

      return V.Rep.Enclosing = G'Unrestricted_Access;

but of course this isn't portable.

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?

-Simon




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Access conversions
  1998-10-12  0:00 Access conversions Simon Wright
@ 1998-10-12  0:00 ` Matthew Heaney
  1998-10-12  0:00 ` Tucker Taft
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1998-10-12  0:00 UTC (permalink / raw)


Simon Wright <simon@pogner.demon.co.uk> writes:

> I need to write a function
> 
>   function Is_Member (G : Graph'Class; V : Vertex'Class) return Boolean;
> 
> where there is a type
> 
>   type Graph_Ptr is access all Graph'Class;
> 
> and a Vertex has a component Rep with a component Enclosing of type
> Graph_Ptr which accesses its enclosing Graph.
> 
> In GNAT I can say in Is_Member
> 
>       return V.Rep.Enclosing = G'Unrestricted_Access;
> 
> but of course this isn't portable.
> 
> 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?
> 
> -Simon

Instead of converting the address to an access type, and then comparing
access types, why not compare addresses directly:

   return V.Rep.Enclosing.all'Address = G'Address;

Isn't this portable?






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Access conversions
  1998-10-12  0:00 Access conversions Simon Wright
  1998-10-12  0:00 ` Matthew Heaney
@ 1998-10-12  0:00 ` Tucker Taft
  1998-10-13  0:00   ` Simon Wright
  1 sibling, 1 reply; 4+ messages in thread
From: Tucker Taft @ 1998-10-12  0:00 UTC (permalink / raw)


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;

: where there is a type

:   type Graph_Ptr is access all Graph'Class;

: and a Vertex has a component Rep with a component Enclosing of type
: Graph_Ptr which accesses its enclosing Graph.

: In GNAT I can say in Is_Member

:       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.

: 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.

: -Simon

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA
An AverStar Company




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Access conversions
  1998-10-12  0:00 ` Tucker Taft
@ 1998-10-13  0:00   ` Simon Wright
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 1998-10-13  0:00 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1998-10-13  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-12  0:00 Access conversions Simon Wright
1998-10-12  0:00 ` Matthew Heaney
1998-10-12  0:00 ` Tucker Taft
1998-10-13  0:00   ` Simon Wright

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