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,2e66fe8b30e3ee2c X-Google-Attributes: gid103376,public From: Marin David Condic Subject: Re: S'Write and How To Count Bytes Date: 2000/10/02 Message-ID: <39D88ACE.92DF8897@acm.org>#1/1 X-Deja-AN: 676616071 Content-Transfer-Encoding: 7bit References: <39D6891A.7DC448B6@acm.org> X-Accept-Language: en X-Server-Date: 2 Oct 2000 13:16:38 GMT Content-Type: text/plain; charset=us-ascii Organization: Quadrus Corporation Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-10-02T13:16:38+00:00 List-Id: "David C. Hoos, Sr." wrote: > If your record contains an unbounded string, I would in the My_Record'Write > procedure convert the unbounded string to a Standard.String, and write its > length to the stream with Natural'Write, followed by a call to String'Write. > This will put the length of the string on the stream, followed by the > string itself. > > Then, the My_Record'Read procedure would read the length from the stream, > using Natural'Read, use that length to declare a string of the proper size > (in a declare block), then read the string with String'Read. > Finally, you would convert the string to the unbounded string component in > your My_Record'Read's out parameter of type My_Record. Well, there are a couple of questions here. One if which is "How does 'Write" normally handle an Unbounded_String or any other private type for that matter?" The ARM says they are defined for any non-limited type (ARM 13.13.2{36}) but I could easily see a private type (or any type) containing Access variables - no mention of that. It mentions Elementary types, Array types, Record types, but not Access types. (Or is Access an Elementary type? If so, its still a special case) Obviously, transmitting Access variables is *not* a good idea. (What if I store an unbounded string using Stream_IO? Is it going to come back properly, or will I have just stored a now-useless access variable that used to point to the string?) The other question is: "What if you extended the type and still wanted to handle the general case with dispatching => T'Class'Write?" That's really what I want to do - not so much a specific case of Unbounded_Strings. In other words, I need to somehow get the number of bytes I'm transmitting into the stream and I can't count on the listener at the other end to be using Ada or anything else. (The device expects one of several messages with varying lengths - it needs the number of bytes as the first datum to correctly read all the data for the message before attempting to process it.) I believe I can use the 'Read and 'Write attributes to correctly get the individual data pieces into the stream, but I need to know how many bytes that is going to be before putting it in the stream. So far as I can tell, there is no way to do this except convert it to a stream, count the bytes that turned into, insert the byte count in the record, then convert it to a stream again. Awfully wasteful! Maybe for Ada0x, there should be an attribute T'Stream_Size and/or T'Class'Stream_Size. Not sure that this would not simply have to degenerate to converting everything and counting it in the background though. Thanks for the help. MDC -- ====================================================================== Marin David Condic - Quadrus Corporation - http://www.quadruscorp.com/ Send Replies To: m c o n d i c @ q u a d r u s c o r p . c o m Visit my web site at: http://www.mcondic.com/ "Giving money and power to Government is like giving whiskey and car keys to teenage boys." -- P. J. O'Rourke ======================================================================