comp.lang.ada
 help / color / mirror / Atom feed
* Access idiom
@ 2008-01-21  7:12 Grein, Christoph (Fa. ESG)
  0 siblings, 0 replies; 15+ messages in thread
From: Grein, Christoph (Fa. ESG) @ 2008-01-21  7:12 UTC (permalink / raw)
  To: comp.lang.ada

You need *all* on the access type:

  type Node_Ptr_Type is access all Node_Type'Class;

  procedure Op (P: access Threat_Type; Result: out Node_Ptr_Type) is 
  begin 
    Result := Node_Ptr_Type (P);  --  OK now
  end Op;


Eurocopter Deutschland GmbH
Sitz der Gesellschaft/Registered Office: Donauwoerth
Registergericht/Registration Court: Amtsgericht Augsburg HRB 16508
Vorsitzender des Aufsichtsrates/Chairman of the Supervisory Board: Dr. Lutz Bertling
Geschaeftsfuehrung/Board of Management:
Dr. Wolfgang Schoder, Vorsitzender/CEO; Friedrich-Wilhelm Hormel; Ralf Barnscheidt

CONFIDENTIALITY NOTICE 

This communication and the information it contains is intended for the addressee(s) named above and for no other persons or organizations. It is confidential and may be legally privileged and protected by law. The unauthorized use, copying or disclosure of this communication or any part of it is prohibited and may be unlawful. 
If you have received this communication in error, kindly notify us by return e-mail and discard and/or delete the communication. Thank you very much. 
It is possible for e-mails to be intercepted or affected by viruses. Whilst we maintain virus checks on our e-mails, we accept no liability for viruses or other material which might be introduced with this message. 




^ permalink raw reply	[flat|nested] 15+ messages in thread
* Access idiom
@ 2008-01-20 19:57 Gene
  2008-01-21  1:01 ` Ludovic Brenta
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Gene @ 2008-01-20 19:57 UTC (permalink / raw)


First, thanks for past help from this group.

I'm at an impasse developing a graph data structure.  There are many
node types derived from a "most general" one.  Most of the node types
contain fields that are classwide access to child nodes.  Various
primitive procedures operate on nodes, dispatching on unnamed node
access types.  In many cases, the operations return such an access
child value.  Other "identity" ops just return the dispatching
parameter as classwide access.

Here is the idea:

----- hoo.ads ----
package Hoo is

   type Node_Type is tagged record
      I : Integer := 0;
   end record;
   function Op(P : access Node_Type) return access Node_Type'Class;

   type Threat_Type is new Node_Type with record
      Target : access Node_Type'Class;
   end record;
   overriding
   function Op(P : access Threat_Type) return access Node_Type'Class;

end Hoo;

---- hoo.adb ----
package body Hoo is

   function Op(P : access Node_Type) return access Node_Type'Class is
   begin
      return P; -- identity operation
   end Op;

   overriding
   function Op(P : access Threat_Type) return access Node_Type'Class
is
   begin
      return P.Target; -- just return a child
   end Op;

end Hoo;

---- foo.adb (main procedure) ----
with Hoo; use Hoo;

procedure Foo is
   Threat : access Threat_Type;
begin
   -- Nonsense just to verify type compatibility.
   Threat := new Threat_Type;
   Threat.Target := Op(Threat);
end Foo;
-----

This is all fine.  The trouble is that some of the primitive ops need
to return multiple values, some of which will be classwide access to
node.  The "natural" implementation seems to be a procedure with
several "out" parameters; but, "seem" is the operative word.  To use
out parameters, the classwide access node type must be named.  The
problem here is I can't see a way to dispatch on this named access
type, which will be necessary to recursively operate on child nodes.

One idea is to return a record type that holds the needed multiple
values, but this seems ugly.  It will require defining a bunch of
ephemeral record types that don't have clear meaning as objects.

I tentatively have given up on access dispatching entirely,
dispatching instead on node types (not access) and copying to form
fresh values of a named classwide access type for the return
value(s).  This same named type is used for node child pointers.
While this okay, there is a lot of useless copying.

Feels like I'm playing whack-a-mole with the Ada type system.  What's
the idomatic way to get this job done without the copying?

Thanks,
Gene



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

end of thread, other threads:[~2008-01-22 18:47 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-21  7:12 Access idiom Grein, Christoph (Fa. ESG)
  -- strict thread matches above, loose matches on Subject: below --
2008-01-20 19:57 Gene
2008-01-21  1:01 ` Ludovic Brenta
2008-01-21  4:16   ` Gene
2008-01-21 15:37     ` Robert A Duff
2008-01-22  4:11     ` Randy Brukardt
2008-01-22  4:11     ` Randy Brukardt
2008-01-21  9:05 ` Dmitry A. Kazakov
2008-01-21 18:15 ` Jeffrey R. Carter
2008-01-22  3:56   ` Gene
2008-01-22  5:10     ` Gene
2008-01-22  9:01     ` Dmitry A. Kazakov
2008-01-22 18:47     ` Jeffrey R. Carter
2008-01-22  4:15   ` Randy Brukardt
2008-01-22  4:15   ` Randy Brukardt

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