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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,927ae253da8bb547 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-31 06:34:07 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!hammer.uoregon.edu!skates!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: Specialization Date: 31 May 2002 09:27:56 -0400 Organization: NASA Goddard Space Flight Center (skates.gsfc.nasa.gov) Message-ID: References: <4519e058.0205300909.5bfb317d@posting.google.com> NNTP-Posting-Host: anarres.gsfc.nasa.gov Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: skates.gsfc.nasa.gov 1022852123 17565 128.183.220.71 (31 May 2002 13:35:23 GMT) X-Complaints-To: usenet@news.gsfc.nasa.gov NNTP-Posting-Date: 31 May 2002 13:35:23 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: archiver1.google.com comp.lang.ada:25077 Date: 2002-05-31T13:35:23+00:00 List-Id: "Baugereau" writes: > > This is the problem. You should (almost certainly :) have an array of > > ELEMENTPTR, not an array of ELEMENT. When an element is added to > > Vector, you call 'new' for it. When an element is erased from the > > vector you call an instantiation of Unchecked_Deallocation on it. Then > > Finalize will be called automatically, if necessary. > > > > See the file sal-poly-unbounded_arrays.ads > > in SAL at http://users.erols.com/leakstan/Stephe/Ada/sal.html > > for an example of a similar package. > > > > You will find that you will have to allow your generic ELEMENT type to > > be unconstrained and/or indefinite, if the Vector package is to be > > truly useful. For example, try to define a Vector of String. Once you > > do that, you are forced to have an array of pointer-to-element. > > > > Ok so this is the difference of philosophy with STL's vector, which is plain > value-oriented. > What I dislike here is the lack of locality of the elements, and the need to > "new" every time I add an element... > Any comment? I should have mentioned that there are other ways to get the Finalize procedure called, without using Unchecked_Deallocation. Perhaps the cleanest is to require the client to pass in a "Null_Value" object: generic type Element is private; Null_Value : in Element; package Foo is type Element_array is array (1 .. 10) of Element; procedure Empty (Item : in out Element_Array); end Foo; Then in the body of Empty you fill the array with Null_Value by assignment. If Element is Controlled, this will call Finalize on the values in the array. It is up to the client to ensure that Null_Value is sufficiently small to not be a burden when copied lots of times. Another way is to require the type to be derived from Controlled: generic type element is new Ada.Finalization.Controlled; package foo is ... end foo; But then this package is not useful for non-controlled types. One more: require the client to pass in a Finalize procedure: generic type Element is private; with procedure Finalize (Item : in out Element) is <>; package Foo is type Element_array is array (1 .. 10) of Element; procedure Empty (Item : in out Element_Array); end Foo; Now Empty can call Finalize on each element of the array. For non-controlled types, Finalize can be a null operation. This is the approach I use in SAL; it allows a generic package to be instantiated with the widest range of types. -- -- Stephe