|
Prev: Gant, tasking, and Vista
Next: ADA abstract
From: Grein, Christoph (Fa. ESG) on 21 Jan 2008 02:12 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.
From: Dmitry A. Kazakov on 21 Jan 2008 04:05 On Sun, 20 Jan 2008 11:57:09 -0800 (PST), Gene wrote: > 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. As it was already suggested it will dispatch after dereferncing. In Ada 2005 which seems you are using, you will not need P.all with prefix notation: Node.Next.Next, Next(Node).Next, both Next in both variants dispatch. Alternatively you can use tagged handles to nodes wrapping an access type. The operations on nodes become private. The operations on handles are public. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de
From: Robert A Duff on 21 Jan 2008 10:37 Gene <gene.ressler(a)gmail.com> writes: > In concept, what I'd need to do is something like this: > > type Node_Ptr_Type is access Node_Type'Class; > > procedure Op(P : in Threat_Type; Result : out Node_Ptr_Type) is > begin > Result := P'Access; -- identity operation > end Op; I'm not sure exactly what you're trying to do, but the following might be what you want: type Node_Ptr_Type is access constant Node_Type'Class; ^^^^^^^^ procedure Op(P : in Threat_Type; Result : out Node_Ptr_Type) is begin Result := P'Unchecked_Access; -- identity operation ^^^^^^^^^^ end Op; or: type Node_Ptr_Type is access all Node_Type'Class; ^^^ procedure Op(P : in out Threat_Type; Result : out Node_Ptr_Type) is ^^^ begin Result := P'Unchecked_Access; -- identity operation ^^^^^^^^^^ end Op; 'Unchecked_Access can lead to dangling pointers -- you need to make sure you don't pass nested objects to Op. - Bob
From: Jeffrey R. Carter on 21 Jan 2008 13:15 Gene wrote: > > 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. There should be no need for public access types or values in such a data structure. Hiding them should make the package easier to use, easier to implement, and safer. -- Jeff Carter "Apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, the fresh water system, and public health, what have the Romans ever done for us?" Monty Python's Life of Brian 80
From: Gene on 21 Jan 2008 22:56
On Jan 21, 1:15 pm, "Jeffrey R. Carter" <spam.jrcarter....(a)acm.nospam.org> wrote: > Gene wrote: > > > 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. > > There should be no need for public access types or values in such a data > structure. Hiding them should make the package easier to use, easier to > implement, and safer. First, I think the collective wisdom has put me straight. Thanks. Another, more succinct explanation... The graph has many node types, all with common ancestor. Edges are pointers--named or anonymous access Node_Type'Class; don't care which. Required is a primitive "transform" procedure that dispatches on node type and must return a pointer to an _arbitrary node type_ (i.e. again of type access Node_Type'Class). Frequently the transform should just return its parameter without modification. Using primitive functions (which would enable return of anonymous access Node_Type'Class) is not good due to need for multiple return values. Jeff, in view of all the above, a named "access all Node_Type'Class" type seems necessary for the return values. I failed earlier because the out parameter wouldn't convert automatically from named to unnamed access, and I didn't realize type conversion would work on L-values. Further comments very welcome if there is a better way. Again, thanks. This compiles and dispatches as expected: ---- hoo.ads package Hoo is type Node_Type is abstract tagged null record; type Node_Ptr_Type is access all Node_Type'Class; procedure XForm(P : access Node_Type; Rtn : out Node_Ptr_Type) is abstract; type Binary_Type is new Node_Type with record Left, Right : access Node_Type'Class; end record; overriding procedure XForm(P : access Binary_Type; Rtn : out Node_Ptr_Type); end Hoo; ---- hoo.adb package body Hoo is overriding procedure XForm (P : access Binary_Type; Rtn : out Node_Ptr_Type) is begin Rtn := Node_Ptr_Type(P); end XForm; end Hoo; ---- foo.adb procedure Foo is P : access Binary_Type; begin P := new Binary_Type; Xform(P.Left, Node_Ptr_Type(P.Left)); end Foo; |