From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Smart pointers and delegation
Date: Tue, 1 Aug 2017 12:32:07 +0200
Date: 2017-08-01T12:32:07+02:00 [thread overview]
Message-ID: <olplb6$jq0$1@gioia.aioe.org> (raw)
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
next reply other threads:[~2017-08-01 10:32 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-01 10:32 Dmitry A. Kazakov [this message]
2017-08-01 15:06 ` Smart pointers and delegation Dmitry A. Kazakov
2017-08-01 23:06 ` Randy Brukardt
2017-08-02 6:20 ` Dmitry A. Kazakov
2017-08-03 3:36 ` Randy Brukardt
2017-08-03 7:40 ` Dmitry A. Kazakov
2017-08-04 23:03 ` Randy Brukardt
2017-08-05 8:33 ` Dmitry A. Kazakov
2017-08-07 22:39 ` Randy Brukardt
2017-08-08 6:27 ` Dmitry A. Kazakov
2017-08-09 0:27 ` Randy Brukardt
2017-08-09 7:37 ` Dmitry A. Kazakov
2017-08-09 22:57 ` Randy Brukardt
2017-08-10 7:56 ` Dmitry A. Kazakov
2017-08-11 0:17 ` Randy Brukardt
2017-08-11 6:43 ` Dmitry A. Kazakov
2017-08-11 20:37 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox