From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2843c5eea3415584 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: APQ Date: Wed, 5 Jan 2005 12:58:05 +0100 Organization: cbb software GmbH Message-ID: <14tct9l9a8k00$.18ys1gknymyvo.dlg@40tude.net> References: <2hju7i5ft73q$.x4md24pt913v.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: individual.net 55v2yAdJiauEAQLsod13sg+wTqjfKvWVhGJx4Q6lb8STJcvRI= User-Agent: 40tude_Dialog/2.0.12.1 Xref: g2news1.google.com comp.lang.ada:7469 Date: 2005-01-05T12:58:05+01:00 List-Id: On Wed, 05 Jan 2005 08:32:53 +1100, Brian May wrote: >>>>>> "Dmitry" == Dmitry A Kazakov writes: > > Dmitry> You probably mean this: > > Dmitry> function Copy (C : Root_Connection_Type'Class) > Dmitry> return Root_Connection_Ptr is > Dmitry> begin > Dmitry> return new Root_Connection_Type'Class'(C); > Dmitry> end Copy; > > Dmitry> This indeed cannot work for limited types. This was the > Dmitry> reason why Copy was declared abstract primitive instead: > > Dmitry> function Copy (C : Root_Connection_Type) > Dmitry> return Root_Connection_Ptr is abstract; > > Dmitry> It will dispatch to an implementation which knows how to > Dmitry> copy itself: > > I am under the impression (perhaps wrong impression, I haven't tested > it) that > > A := Copy(C); > > will not work if C is a limited type, as there is an implied copy > operation from the RHS to the LHS. The following is an fully operational implementation of what I meant based on Simple Components (http://www.dmitry-kazakov.de/ada/components.htm): -- The test program first: with Connections; use Connections; with Connections.My_Connections; procedure Test_Connections is A : Connection; -- Illegal, will not compile B : Connection := Connections.My_Connections.Create (10); C : Connection := Copy (B); -- Make a copy D : Connection := B; -- Reference begin null; end Test_Connections; Here B and D are handles to the same internal connection object. They share it. C is a handle to a copy of the object pointed by B,D. The connection objects are automatically destroyed when the last handle disappear. User sees only the type Connection. -- Abstract connection interface. A connection is a handle to -- the connection object. Handles are not limited, objects are. with Object; with Object.Handle; package Connections is type Connection (<>) is private; function Copy (C : Connection) return Connection; private type Root_Connection_Object is abstract new Object.Entity with null record; type Root_Connection_Ptr is access Root_Connection_Object'Class; function Copy (C : Root_Connection_Object) return Root_Connection_Ptr is abstract; package Connection_Handles is new Object.Handle (Root_Connection_Object, Root_Connection_Ptr); type Connection is new Connection_Handles.Handle with null record; function Ref (C : Root_Connection_Ptr) return Connection; end Connections; package body Connections is function Copy (C : Connection) return Connection is begin return Ref (Copy (Ptr (C).all)); end Copy; function Ref (C : Root_Connection_Ptr) return Connection is begin return (Connection_Handles.Ref (C) with null record); end Ref; end Connections; -- An implementation: My_Connection package Connections.My_Connections is function Create (Parameter : Integer) return Connection; private type My_Connection_Object is new Root_Connection_Object with record I : Integer; end record; function Copy (C : My_Connection_Object) return Root_Connection_Ptr; end Connections.My_Connections; package body Connections.My_Connections is function Create (Parameter : Integer) return Connection is This : Root_Connection_Ptr := new My_Connection_Object; Object : My_Connection_Object renames My_Connection_Object (This.all); begin Object.I := Parameter; return Ref (This); end Create; function Copy (C : My_Connection_Object) return Root_Connection_Ptr is This : Root_Connection_Ptr := new My_Connection_Object; Object : My_Connection_Object renames My_Connection_Object (This.all); begin Object.I := C.I; return This; end Copy; end Connections.My_Connections; > Also the thread "Return_By_Reference or Return_By_Copy (GNAT bug?)" might > be relevant here. Only in Ada 2005 there will be a way to have limited object constructors. Which will be BTW still too little for a proper construction model. There still will be no constructors with parameters. No copy constructors. No class-wide constructors. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de