comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: CONSTRAINT_ERROR - why?
Date: Wed, 18 Dec 2002 15:45:18 -0600
Date: 2002-12-18T15:45:18-06:00	[thread overview]
Message-ID: <v01r4nnpdod349@corp.supernews.com> (raw)
In-Reply-To: wcc8yyn40ul.fsf@shell01.TheWorld.com


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.









  reply	other threads:[~2002-12-18 21:45 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-14 18:28 CONSTRAINT_ERROR - why? Alfred Hilscher
2002-12-14 18:45 ` James S. Rogers
2002-12-14 19:15   ` Robert A Duff
2002-12-14 19:20     ` James S. Rogers
2002-12-14 19:42     ` Alfred Hilscher
2002-12-16  2:39       ` AG
2002-12-14 19:20   ` Alfred Hilscher
2002-12-14 20:58     ` Dennis Lee Bieber
2002-12-15 13:45       ` Alfred Hilscher
2002-12-15 19:04         ` Robert A Duff
2002-12-16  1:12         ` Dennis Lee Bieber
2002-12-16  2:20           ` Jeffrey Carter
2002-12-16 18:48             ` Alfred Hilscher
2002-12-16 19:53               ` James S. Rogers
2002-12-17 18:20                 ` Alfred Hilscher
2002-12-17 19:29                   ` Robert A Duff
2002-12-17 21:05                     ` Dennis Lee Bieber
2002-12-18 18:36                     ` Alfred Hilscher
2002-12-18 18:51                       ` Robert A Duff
2002-12-18 21:45                         ` Randy Brukardt [this message]
2002-12-19 16:49                           ` Robert Spooner
2002-12-19 20:57                             ` Randy Brukardt
2002-12-14 20:19 ` Jeffrey Carter
2002-12-14 22:26   ` Alfred Hilscher
2002-12-15  0:49     ` Dennis Lee Bieber
2002-12-15  2:18     ` Jeffrey Carter
2002-12-15 18:23       ` Jeffrey Carter
2002-12-15 14:01   ` Alfred Hilscher
2002-12-15 18:20     ` Jeffrey Carter
2002-12-15 19:00       ` Alfred Hilscher
2002-12-16  1:16         ` Dennis Lee Bieber
2002-12-16 18:50           ` Alfred Hilscher
2002-12-17  2:17             ` Dennis Lee Bieber
2002-12-17 18:21               ` Alfred Hilscher
     [not found]                 ` <26tuvu4ekeigls0bnd2okavrdhfu0tir4a@4ax.com>
2002-12-18 18:47                   ` Alfred Hilscher
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox