Re: smart pointer dangerous (no -> operator)



On Fri, 16 Jan 2009 11:04:57 +0100, Oliver Kowalke wrote:

Ada doesn't support the dereference operator -> as C++.

So Ada provides only two ways to access the managed object?

- returning it via value : function Get(Pointer : Smart_Pointer) return
Object;

- returning it via access type : function Get(Pointer : Smart_Pointer)
return Access_Object;

- returning a proxy which implements the functions applicable to the managed
object

In the first case I can not manipulate the state of the managed object
(setting some internal data).
The second case is dangerous because the ownership 'constraint' can be
violated -> other code can call Unchecked_Delete on the returned access
type or assign it to another one.

How can case three be implemented in Ada if the smart ponter package is
generic?

It is difficult, since there is no any delegation support in Ada.

I extensively used two approaches:

1. In Ada 95 I declare the proxy type tagged controlled. The handle is
created through a instantiation in the private part of the generic package
Handle:

type X_Handle is new Ada.Finalization.Controlled with private;
-- Repeat all operations of the target
procedure Foo (Object : X_Handle);
... -- and so on
private
package Handles is not Object.Handle (...); -- Instantiation
type X_Handle is new Handles.Handle with null record;

The implementation of Foo goes as follows:

procedure Foo (Object : X_Handle) is
begin
Foo (Ptr (Object).all);
end Foo;

this is extremely tedious, but the best way I know.

Important, the target object is derived from Object.Entity. This is
necessary for reference counting. There is also another base for
persistence support.

2. In Ada 2005 things are slightly better. There I use interfaces. The
target object receives an interface:

type X_Interface is ...;
procedure Foo (Object : X_Interface) is abstract;

Then I define the object's implementation type derived from Object.Entity
as before, but also implementing X_Interface. Usually I do it privately,
though this requires some additional efforts in package hierarchy
structuring (the proxy must see the object implementation type at the
handle instantiation point).

Now the proxy type:

type X_Handle is new Ada.Finalization.Controlled
and X_Interface with private;

No more need to repeat everything.

The implementation is still needed:

procedure Foo (Object : X_Handle) is
begin
Ptr (Object).Foo; -- Using prefix notation
end Foo;

This is less error prone, but still endlessly boring. Two major problems
are:

1. Lack of delegation in order to automate generation of wrappers like Foo.
2. Lack of MI, because one base type is required and used further extension
of the target type becomes practically impossible.

There also are more difficult problem of building hierarchies of proxy
types corresponding to the hierarchy of the targets as well as container
types for proxies. But this is another story.

---------------
P.S. My implementation of proxy object can be found here

http://www.dmitry-kazakov.de/ada/components.htm#Objects_etc

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.



Relevant Pages

  • Re: pascal system
    ... parameters and type-risky pointer schemes. ... The improvements made to type checking in Ada are one of my favorite ... For foo in bar'first to bar'last loop ... less 'typey' languages, and in order to create efficiency in critical ...
    (comp.sys.apple2)
  • Re: how do i implement double-dispatching?
    ... I'm trying to write a C to Ada translator. ... Foo: constant Boolean:= True ... function Max(a: blah; ... other tools (maybe converters from other languages). ...
    (comp.lang.ada)
  • Re: is decorator the right thing to use?
    ... ProxyMethod declarations instead of a dict; ... from proxies import Proxy, ProxyMethod ... bmethod = ProxyMethod ... B::bmethod 10 foo ...
    (comp.lang.python)
  • Re: The coming death of all RISC chips.
    ... Corporate encourages reuse of the code written by project that was using FOO in project BAR. ... Because of licensing, so very often reuse of the library that uses FOO is converted to binary only reuse, whereas if there is no licensing then the users of FOO can receive source code distributions, and contribute fixes to bugs they discover through use. ... Now, this may not apply so much to something like a commercial Ada compiler, so long as there is Gnu Ada to be used by those who don't want to pay the licensing. ...
    (comp.arch)
  • Re: is decorator the right thing to use?
    ... auto-population of proxy methods since for each class/method those are ... # do NOT delegate C.cmethod2 ... A::mymethod foo ... if cls in self._cls2delegate: ...
    (comp.lang.python)