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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Smart pointers and delegation Date: Tue, 1 Aug 2017 12:32:07 +0200 Organization: Aioe.org NNTP Server Message-ID: NNTP-Posting-Host: MajGvm9MbNtGBKE7r8NgYA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 X-Notice: Filtered by postfilter v. 0.8.2 X-Mozilla-News-Host: news://news.aioe.org Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:47548 Date: 2017-08-01T12:32:07+02:00 List-Id: I would like to discuss delegation proposal as a replacement for crude, inside out Implicit_Dereference aspect. It goes as follows: type I is interface ... procedure Foo (X : I); function Bar return I; procedure Baz (X : in out I); [anonymous access argument and result are handled like Foo and Bar] type T is new I with ... record ... end record with Delegate[_In|_Out] => Relay; -- May appear several times Here Relay is one of the following: 1. discriminant or component of T (named or anonymous) of access to I'Class or an expression of this result; 2. discriminant or component of T (named or anonymous) of access to T'Class or an expression of this result; 3. function with the profile: function Relay_In (X : T) return [constant] access [all] I'Class; 4. function with the profile: function Relay_In (X : T) return I'Class; 5. procedure with the profile: procedure Relay_Out (X : [in] out T; Y : [in] out I'Class); For 1, 2, 3 the compiler overrides Foo, Bar, Baz with the following bodies: procedure Foo (X : T) is -- If appears in Delegate[_In] begin X.Relay_In.Foo; end Foo; function Bar return T is -- If appears in Delegate[_In] begin return X.Relay_In.all; end Bar; procedure Baz (X : in out T) is -- If appears in Delegate and begin -- not constant X.Relay_In.Baz; end Baz; For 4 the compiler overrides Foo and Bar with the bodies like above. Baz remains abstract. procedure Foo (X : T) is -- If appears in Delegate[_In] begin X.Relay_In.Foo; end Foo; function Bar return T is -- If appears in Delegate[_In] begin return X.Relay_In; end Bar; For 5 the compiler overrides Baz with the body: procedure Baz (X : in out T) is -- If Relay_In appears in Delegate_In begin -- and Relay_Out does in Delegate_Out Relay_Out (X, X.Relay_In.Baz); end Baz; Any overriding induced by the Delegate aspect happens only if the corresponding operation has not yet been explicitly overridden. Conflicting overriding per two Delegate aspects is an error. ----------------------------------------------------------- Example 1. Smart pointers (handles) type File_Interface is interface; function Read (File : in out File_Interface) return Stream_Element_Array; procedure Write (File : in out File_Interface; Data : Stream_Element_Array); type File_Handle is new File_Interface with private; private type File_Descriptor is new Ada.Finalization.Limited_Controlled and File_Interface with record Use_Count : Natural := 0; ... end record; type File_Descriptor_Ptr is access File_Descriptor'Class; type File_Handle is new Ada.Finalization.Controlled and File_Interface with record Descriptor : File_Descriptor_Ptr; end record with Delegate => Descriptor; -------------------------------------------------------------- Example 2. Full multiple inheritance type A_Interface is limited interface; type A is new A_Interface with ...; type B is tagged ...; type AB is new B and A_Interface with record Alter_Ego : aliased A; end record with Delegate => A'Access; -------------------------------------------------------------- Example 3. Parallel pointers hierarchy type A_Interface is limited interface; type A_Implementation is new A_Interface with ...; type A_Reference (Implementation : access A_Implementation'Class) is new A_Interface with null record; type B_Interface is limited interface; type B_Implementation is new A and B_Interface with ...; type B_Reference is new A_Reference and B_Interface with null record with Delegate => B_Imlementation'Class (Implementation.all)'Access; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de