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,6dcdd5b561500c28 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!news.germany.com!feeder2.news.saunalahti.fi!newsfeed2.funet.fi!newsfeeds.funet.fi!news.cc.tut.fi!not-for-mail From: Tero Koskinen Newsgroups: comp.lang.ada Subject: Re: smart pointer dangerous (no -> operator) Date: Fri, 16 Jan 2009 22:08:16 +0200 Message-ID: <20090116220816.81773601.tero.koskinen@iki.fi> References: NNTP-Posting-Host: ip154.otanner14.opintanner.fi Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.cc.tut.fi 1232136496 23977 195.148.53.154 (16 Jan 2009 20:08:16 GMT) X-Complaints-To: abuse@tut.fi NNTP-Posting-Date: Fri, 16 Jan 2009 20:08:16 +0000 (UTC) X-Newsreader: Sylpheed 2.5.0 (GTK+ 2.14.5; i386-unknown-openbsd4.4) Xref: g2news1.google.com comp.lang.ada:3397 Date: 2009-01-16T22:08:16+02:00 List-Id: 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/