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 Path: g2news2.google.com!postnews.google.com!r31g2000prh.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: newbie problem Date: Wed, 13 May 2009 08:14:14 -0700 (PDT) Organization: http://groups.google.com Message-ID: <99968fcb-2fc4-4816-87bb-f88e27143a2a@r31g2000prh.googlegroups.com> References: <4a0ad646$0$2854$ba620e4c@news.skynet.be> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1242227655 21798 127.0.0.1 (13 May 2009 15:14:15 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 13 May 2009 15:14:15 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r31g2000prh.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5816 Date: 2009-05-13T08:14:14-07:00 List-Id: On May 13, 7:16 am, Olivier Scalbert wrote: > Hello, > > I am doing a little package and I have problems just for writing the > specification ... > > In short, this package should offer services to create sequential files > that store stereo sound samples. > > Here is the code: > > with Sequential_Io; -- Problem 2 not nice should be hidden but how ? > > package adasound is > > type Amplitude_T is new Float; > type Frequency_T is new Float; > type Time_T is new Float; > > type Stereo_Amplitude_T is record > Left : Amplitude_T; > Right: Amplitude_T; > end record; > > type Als_File_T is private; > > function Als_Create(File_Name: String) return Als_File_T; > procedure Als_Write(Als_File: Als_File_T; Stereo_Amplitude: > Stereo_Amplitude_T); > procedure Als_Close(Als_File: Als_File_T); > > private > package Stereo_Amplitude_Io is new Sequential_Io (Stereo_Amplitude_T); > > type Als_File_T is record > File_Type: Stereo_Amplitude_Io.File_Type; -- Problem 1 !! > -- perhaps more stuff later > end record; > > end adasound; > > Problem 1: it does not compile ! > adasound.ads:23:10: completion of nonlimited type cannot be limited > adasound.ads:23:10: component "File_Type" of type "Als_File_T" has > limited type You could make it an access to a File_Type, as Martin mentioned. But you may also want to make *your* type limited. Making it limited would prevent the program from copying objects of that type, and that could be exactly what you want. A record that represents information about a file that's currently being used can be a natural candidate for a "limited" type. Suppose, hypothetically, that you're writing your own file I/O package, and not using the standard Ada libraries. You'll probably have some sort of record somewhere to store information about the current file position, perhaps some cached data from the file, etc.: type File_Info is record Curr_Position : Long_Integer; Buffer : Byte_Array(1..Buf_Size); ... end record; Most likely, this would be in the private part of your package. Suppose you declare this without using "limited": type File_Info is private; Now some other part of the program that WITH's your package can say F1 : File_Info; F2 : File_Info; and then, after F1 has been created and some I/O done using it: F2 := F1; This could screw up your whole file package's operations. Suppose the program later does some operations on F1, and it modifies the Curr_Position and fills some data in the Buffer. This will change the information in F1, but not the information in F2. If the program then calls an I/O procedure using F2, the package will go bonkers, because F2 doesn't have the correct file position or the correct buffered data, and it's very likely that something bad will happen, such as data being flushed to the wrong position of the file and turning your file into junk. A solution here is to prevent assignment from happening, by declaring the type File_Info as "limited private". This would make the above assignment statement illegal, as well as other Ada constructs that could result in two copies of the record being created. I'm not saying that this is definitely what you want. But you should consider it. If you do, you'd change the first declaration to type Als_File_T is limited private; You'll also have to look into Ada 2005's "extended return" statement in order to write the body of Als_Create. Anyway, that's one thing I think you should consider. If that isn't what you want---i.e. if Als_File_T is supposed to represent a "reference" or "handle" to a file rather than the file itself---or if this is all too confusing, then access types will work. It's a design decision. -- Adam