comp.lang.ada
 help / color / mirror / Atom feed
* Re: Record -> Array in Ada
  1996-08-20  0:00 Record -> Array in Ada Mike Roske
@ 1996-08-20  0:00 ` George Haddad
  1996-08-20  0:00 ` Dave Marshall
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: George Haddad @ 1996-08-20  0:00 UTC (permalink / raw)



Mike Roske wrote:
> 1) Unchecked_Conversion into a fixed size array.  (And if I do this,
> how do I get the array bounds set correctly?)

   First, if your record contains discriminated, unconstrained arrays, 
be aware that some compilers may include a "dope-vector" _as part of the 
record_.  So, when you get ready to send your record data down the 
"pipe", you can try to "filter" the extra data out or make sure that the 
dope vector is part of your interface specification.  I recommend the 
second (even though it makes your solution less portable), but as 
interface specs are frequently written by hardware types who've no idea 
what "dope vector" means -- except they're fairly certain that it's 
somehow "naughty"  :-) -- that may not be an option for you.

   So, let's pretend for simplicity's sake that we're just dealing with 
unconstrained array types.  You can declare a function which accepts a 
parameter of the unconstrained array type.  Locally this function 
declares a constrained subtype of the parameter's type, _and_ declares a 
constrained subtype of some unconstrained array of 16-bit "things".  You 
can then instantiate Unchecked_Conversion between the two constrained 
subtypes, and pass the result back as a function result of the 
unconstrained 16-bit array type.  Here's an _uncompiled_, _untested_ 
example.

package X is
  type 32_Block is array(INTEGER range <>) of INTEGER;
  -- assume 32-bit INTEGER

  type 16_Block is array (SHORT_INTEGER range <>) of SHORT_INTEGER;
  -- assume 16-bit SHORT_INTEGER

  function Split(This : 32_Block) return 16_Block;
end X;

with Unchecked_Conversion;
package body X is
  Scale : constant SHORT_INTEGER := INTEGER'SIZE / SHORT_INTEGER'SIZE;

  function Split(This : 32_Block) return 16_Block is
    subtype This_Block_Subtype is 32_Block(This'RANGE);
    subtype Return_Subtype is
      16_Block(1..(This'LENGTH * Scale));
    function Convert is
      new Unchecked_Conversion(Source => This_Block_Subtype,
                               Target => Return_Subtype);
  begin   -- Split
    return Convert(This);
  end Split;
end X;

   YMMV.  There will almost certainly be a performance hit, compared to 
the overlay technique, but this general technique (as modified for 
various compiler shortcomings) has worked for me.  And, I've never had a 
problem porting it to new platforms.  (Of course, that may just be the 
platforms I worked on.)  The approach is adaptable to discriminated 
record types (with fully discriminated record subtypes, of course), but 
watch out for those "hidden" inclusions.

   There is one big irritation factor (for me anyway).  Our project is 
one which avoids any of the predefined types.  Anyway, our atomic types 
(like INT_32) are declared in one package, while my communication arrays 
(and their operations) are defined in another.  To avoid Ada array type 
conversion (which is more restrictive and less well implemented on our 
compiler than I'd like), I would like to derive from the array type and 
inherit a Split operation.  _However_, I also may need to (for example) 
checksum the array, so I would like to derive from INT_32 type (avoiding 
the dreaded "use clause"  :-)) to get its arithmetic operations. I know 
of no way in Ada to get a derived array type, composed of derived array 
elements, in a third package.
-- 
I found these opinions on my doorstep, would you please give them a good 
home?




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Record -> Array in Ada
  1996-08-20  0:00 ` Dave Marshall
@ 1996-08-20  0:00   ` Robert I. Eachus
  0 siblings, 0 replies; 6+ messages in thread
From: Robert I. Eachus @ 1996-08-20  0:00 UTC (permalink / raw)



In article <dmarshalDwFow7.4Fx@netcom.com> dmarshal@netcom.com (Dave Marshall) writes:

  > *FROWNERZ*, sometimes we are prevented from doing the cool things that
  > work.  LRM 13.5(8):

  > "Address clauses should not be used to achieve overlays of objects or
  > overlays or program units.  Nor should a given interrupt be linked to
  > more than one entry.  Any program using address clauses to achieve
  > such effects is erroneous."

   Hmmm.  If that statement prevents you from doing something that
works, try the Ada 95 wording:  "If an Address is specified, it is the
programmer's responsibility to ensure that the address is valid;
otherwise, program execution is erroneous."  There is no change in the
semantics from Ada 83 to Ada 95, just a realization that the meaning
of erroneous here exists only in the programmer's head.  If the
programmer defines the behavior as working, it is not erroneous, but
that determination is way outside the bounds of the RM.

   Also, in Ada 95 the Unchecked_Conversion approach is much better
defined.  But again, whether the actions correspond to the
programmer's intent is very implementation specific.  Unchecked
conversion of hidden fields and dope vectors can ruin your whole
day--or make your job easier.


--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Record -> Array in Ada
  1996-08-20  0:00 Record -> Array in Ada Mike Roske
                   ` (2 preceding siblings ...)
  1996-08-20  0:00 ` Robert Dewar
@ 1996-08-20  0:00 ` Ted Dennison
  3 siblings, 0 replies; 6+ messages in thread
From: Ted Dennison @ 1996-08-20  0:00 UTC (permalink / raw)



Mike Roske wrote:
> What's the best way to split the record into 16-bit slices for
> transmission?
> 
> I have identified the following options:
> 
> 1) Unchecked_Conversion into a fixed size array.  (And if I do this, how
> do I get the array bounds set correctly?)
> 
> 2) Pass Record'Address and Record'Size into a subprogram.  Inside the
> subprogram, declare an array of the appropriate size, located at the
> appropriate address.  (have prototyped this one, and it works.)
> 
> 3) Ask in c.l.a for suggestions.  (hi there!)
> 

Why not do all three? :-)

Simply merge approaches 1 and 2 (and you are already doing 3). Set the array
type's size to be (Record'size + 7)/8 bytes.

-- 
T.E.D.          
                |  Work - mailto:dennison@escmail.orl.mmc.com  |
                |  Home - mailto:dennison@iag.net              |
                |  URL  - http://www.iag.net/~dennison         |




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Record -> Array in Ada
  1996-08-20  0:00 Record -> Array in Ada Mike Roske
  1996-08-20  0:00 ` George Haddad
  1996-08-20  0:00 ` Dave Marshall
@ 1996-08-20  0:00 ` Robert Dewar
  1996-08-20  0:00 ` Ted Dennison
  3 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1996-08-20  0:00 UTC (permalink / raw)



Michael asks

"1) Unchecked_Conversion into a fixed size array.  (And if I do this, how
do I get the array bounds set correctly?)

2) Pass Record'Address and Record'Size into a subprogram.  Inside the
subprogram, declare an array of the appropriate size, located at the
appropriate address.  (have prototyped this one, and it works.)
"

method 2 is fine, be sure to use object'size and not type'size when
you do this (they can be different).

method 1 will also work, just declare a local subtype of the appropriate
size for the array, so that the sizes match, you can use 'Size of the
record object to compute the right bounds for the target array subtype.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Record -> Array in Ada
@ 1996-08-20  0:00 Mike Roske
  1996-08-20  0:00 ` George Haddad
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Mike Roske @ 1996-08-20  0:00 UTC (permalink / raw)



I am looking for opinions.  Do you think I might find some here? ;-)

I have the following programming need in some Ada83 software and am
looking for the "best" solution:

I have a record structure that must be transmitted via comm channel
(MIL-STD-1553B, but let's say any 16-bit oriented channel).

What's the best way to split the record into 16-bit slices for 
transmission?

I have identified the following options:

1) Unchecked_Conversion into a fixed size array.  (And if I do this, how
do I get the array bounds set correctly?)

2) Pass Record'Address and Record'Size into a subprogram.  Inside the
subprogram, declare an array of the appropriate size, located at the
appropriate address.  (have prototyped this one, and it works.)

3) Ask in c.l.a for suggestions.  (hi there!)

Thanks in advance for the help!

--> Mike



-- 
*-------------------------------------------------------*
* Michael Roske
* Sanders, A Lockheed Martin Company
* mroske@mailgw.sanders.lockheed.com
* (603) 885-9240
*-------------------------------------------------------*
* "I'd rather be flying RC..."                     
*-------------------------------------------------------*




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Record -> Array in Ada
  1996-08-20  0:00 Record -> Array in Ada Mike Roske
  1996-08-20  0:00 ` George Haddad
@ 1996-08-20  0:00 ` Dave Marshall
  1996-08-20  0:00   ` Robert I. Eachus
  1996-08-20  0:00 ` Robert Dewar
  1996-08-20  0:00 ` Ted Dennison
  3 siblings, 1 reply; 6+ messages in thread
From: Dave Marshall @ 1996-08-20  0:00 UTC (permalink / raw)



Mike Roske <mroske@mailgw.sanders.lockheed.com> writes:

[snipped the introduction: need to transmit a record as a number of
16-bit values.]

>I have identified the following options:

>1) Unchecked_Conversion into a fixed size array.  (And if I do this, how
>do I get the array bounds set correctly?)

I've done this rather frequently.  The number of words is computed
thusly, use whatever stylistics you like:

Number_Of_Words : constant := (My_Record_Type'Size + 15) / 16;

>2) Pass Record'Address and Record'Size into a subprogram.  Inside the
>subprogram, declare an array of the appropriate size, located at the
>appropriate address.  (have prototyped this one, and it works.)

*FROWNERZ*, sometimes we are prevented from doing the cool things that
work.  LRM 13.5(8):

"Address clauses should not be used to achieve overlays of objects or
overlays or program units.  Nor should a given interrupt be linked to
more than one entry.  Any program using address clauses to achieve
such effects is erroneous."

-- 
Dave Marshall
dmarshal@netcom.com





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1996-08-20  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-20  0:00 Record -> Array in Ada Mike Roske
1996-08-20  0:00 ` George Haddad
1996-08-20  0:00 ` Dave Marshall
1996-08-20  0:00   ` Robert I. Eachus
1996-08-20  0:00 ` Robert Dewar
1996-08-20  0:00 ` Ted Dennison

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