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,7be3870dd9c9518f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-18 13:46:00 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: CONSTRAINT_ERROR - why? Date: Wed, 18 Dec 2002 15:45:18 -0600 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <3DFB7841.F898C02@t-online.de> <3DFB8495.A655C512@t-online.de> <616gta.hk3.ln@beastie.ix.netcom.com> <3DFC878D.B6966C37@t-online.de> <2a9jta.1p3.ln@beastie.ix.netcom.com> <3DFD38A7.6080204@acm.org> <3DFE1FFB.E1BB660E@t-online.de> <3DFF6AFB.A536A0CE@t-online.de> <3E00C040.A038AFCE@t-online.de> X-Newsreader: Microsoft Outlook Express 4.72.3612.1700 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3719.2500 X-Complaints-To: abuse@supernews.com Xref: archiver1.google.com comp.lang.ada:32055 Date: 2002-12-18T15:45:18-06:00 List-Id: Robert A Duff wrote in message ... >You're trying to convert a size in bits (of type universal_integer) to a >size in bytes (of type BYTE). And you're doing that all over the place. >So it seems to me you should encapsulate that operation. It's not hard >to write a function to do that. I also suggested a generic, which is a >bit heavy, but does the job. Yes, you'll have to change all your code >to refer to the encapsulated operation. Bob didn't mention that your code isn't even right (ignoring the overflow issue). You have to take care when doing this conversion to round up, because the 'Size value may not be a multiple of the byte size. (Boolean'Size = 1, for instance.) So your code should really be: X.Struct_Length := BYTE(Integer'((Fac_Conf_Struct'Size+7) / 8)); But, then that could be a problem if it ever needs to run on a system with a byte size other than 8. (We ran into this when hosting Janus/Ada on the U2200). So we ended up using constants for this: package Host is IO_BIT_SIZE : Constant := 8; -- Size, in bits, of the basic I/O element. IO_ROUNDING_SIZE : Constant := IO_BIT_SIZE - 1; -- Size to round bits by; usually added to a 'Size value. ... end Host; and then your expression would look like: X.Struct_Length := BYTE(Integer'((Fac_Conf_Struct'Size+Host.IO_ROUNDING_SIZE) / Host.IO_BIT_SIZE)); Making this a subprogram hides most of this, and makes it much easier to change if you need to do so. I wish we had done that in the first place. (We called these IO_Bit_Size, because most of these expressions occur doing I/O. In Ada 95, you'd do that with Streams, so this code could go in the junk [where it belongs]. There is a separate constant for target byte sizes, because the I/O element size may not be the same as the byte size (i.e. Stream_Element'Size does not need to be the same as Storage_Element'Size). Randy.