comp.lang.ada
 help / color / mirror / Atom feed
From: Tero Koskinen <tero.koskinen@iki.fi>
Subject: Re: smart pointer dangerous (no -> operator)
Date: Fri, 16 Jan 2009 22:08:16 +0200
Date: 2009-01-16T22:08:16+02:00	[thread overview]
Message-ID: <20090116220816.81773601.tero.koskinen@iki.fi> (raw)
In-Reply-To: gkpluq$vda$1@online.de

Hi,

On Fri, 16 Jan 2009 11:04:57 +0100 Oliver Kowalke wrote:
> Hello,
> Ada doesn't support the dereference operator -> as C++.
> 
> So Ada provides only two ways to access 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.

If you want to manipulate the original object behind the smart pointer
without using access types, you can use generics (or access to procedure):

generic
   type Element (<>) is limited private;
   type Element_Access is access Element;
package Smart_Pointer is
   type Pointer is new Ada.Finalization.Controlled with private;
   function Create (Object : Element_Access) return Pointer;

   generic
      with procedure Action (Object : in out Element) is <>;
   procedure Update (Pointer_Object : Pointer);
...
end Smart_Pointer;

Usage would be something like this:
package body Test is
   type Integer_Access is access Integer;

   package Smart_Int is new Smart_Pointer (Integer, Integer_Access);

   procedure Test_Update is
      use Smart_Int;

      procedure Do_Update (I : in out Integer) is
      begin
         I := 2;
      end Do_Update;
      A_Int : Smart_Int.Pointer := Smart_Int.Create(new Integer'(1));

      procedure Int_Update is new Smart_Int.Update (Do_Update);
   begin
      Int_Update (A_Int);
   end Test_Update;
end Test;

-- 
Tero Koskinen - http://iki.fi/tero.koskinen/



  parent reply	other threads:[~2009-01-16 20:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-16 10:04 smart pointer dangerous (no -> operator) Oliver Kowalke
2009-01-16 11:09 ` Dmitry A. Kazakov
2009-01-16 11:42   ` Georg Bauhaus
2009-01-16 12:43     ` Dmitry A. Kazakov
2009-01-16 21:21     ` Maciej Sobczak
2009-01-17 19:07       ` Georg Bauhaus
2009-01-16 11:46   ` Oliver Kowalke
2009-01-16 12:45     ` Dmitry A. Kazakov
2009-01-17  0:43   ` Brian Drummond
2009-01-17  9:28     ` Dmitry A. Kazakov
2009-01-16 20:08 ` Tero Koskinen [this message]
2009-01-16 21:16   ` Maciej Sobczak
2009-01-18 12:21 ` Samuel Tardieu
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox