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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,573be8c453ecbff4 X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: 'Read for pointers Date: 2000/07/28 Message-ID: #1/1 X-Deja-AN: 651969592 References: <8lndgv$1om$1@nnrp1.deja.com> Content-Type: text/plain; charset=us-ascii X-Complaints-To: dscoggin@cne-odin.gsfc.nasa.gov X-Trace: skates.gsfc.nasa.gov 964822010 5210 128.183.220.71 (28 Jul 2000 22:06:50 GMT) Organization: NASA Goddard Space Flight Center Mime-Version: 1.0 User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 NNTP-Posting-Date: 28 Jul 2000 22:06:50 GMT Newsgroups: comp.lang.ada Date: 2000-07-28T22:06:50+00:00 List-Id: Ted Dennison writes: > I've seen it suggested in several places to handle Stream writing of > pointers by writing what the pointer points to instead. That's simple > enough. > > But say I want to make this automatic. Lets say I have a pointer type > declared thusly: > > package Example is > type Handle is private; > > ... > private > > type Instance; > > type Handle is access all Instance; > > end Example; > > The user doesn't even really know or care that Handle is really a > pointer. But they may need to do stream I/O with it. > > The obvious solution is to create my own 'Write and 'Read routines for > Handle. But there's a problem. Following is the profile for 'Read: > > procedure Read > (Stream : access Ada.Streams.Root_Stream_Type'Class; > Item : out Handle > ); > for Handle'Read use Read; > > The problem is that Item is an *out* parameter. That means I won't have > acces to the pointer's old value inside Read. There's no way I can put > the newly read data from the stream into the Instance that Handle > currently points to! But if you are reading from a stream, Handle doesn't yet point to anything! So 'Read needs to allocate an Instance, and set Handle to that. I guess Handle could point to an Instance that the user no longer needs; it should be free'd before the new Instance pointer is assigned. So Handle must be a controlled type. My, this is getting fun :). -- -- Stephe