From: Mats Karlssohn <mats@mida.se>
Subject: records containing variable length arrays [long]
Date: Wed, 06 Jun 2001 21:59:53 +0200
Date: 2001-06-06T19:58:19+00:00 [thread overview]
Message-ID: <3B1E8BB9.42BF95D2@mida.se> (raw)
I'm currently wrestling with an interesting problem. I feel that this
have probably been discussed previously, but I couldn't find anything
on it neither on AdaPower nor on the cla. archives on google.
I would like to declare a record type containing two (possibly more)
arrays of variable lengths, the length of each array is another element
of the record. I also need to specify an exact representation for the
record since it is a message that I cant change the format of.
A piece of code to illustrate (please not that this is NOT compilable)
package Communication is
-- Basic types
type Byte is mod 2**8;
for Byte'size use 8;
type Word is mod 2**16;
for Word'size use 16;
-- Variable length buffer
type Buffer is array(Byte range <>) of Byte;
pragma Pack(Buffer);
-- Declaring the variable length elements is one major headache
-- note that at least Output_Length may legally be 0
type Message is
record
Magic : Word;
Operation : Word;
Status : Word;
Response_Length : Byte;
Output_Length : Byte;
Response_Data : Buffer(0..(Response_Length - 1));
Output_Data : Buffer(0..(Output_Length - 1));
CRC : Word;
end record;
-- the other big problem is to get the correct representation
for Message use
record
Magic at 0 range 0..15;
Operation at 2 range 0..15;
Status at 4 range 0..15;
Response_Length at 4 range 0.. 7;
Output_Length at 5 range 0.. 7;
Response_Data at 6 range 0..(Response_Length * Byte'size - 1);
Response_Data at (6 + Response_Length) range 0..(Output_Length * Byte'size - 1);
CRC at (6 + Response_Length + Output_Length) range 0..15;
end record;
end Communication;
The compiler (Gnat 3.13p on Linux) says:
$ gnatgcc -c -g -gnata -gnatf communication.ads
communication.ads:23:39: component "Response_Length" cannot be used before end of record declaration
communication.ads:24:39: component "Output_Length" cannot be used before end of record declaration
communication.ads:34:11: component "Response_Length" overlaps "Status" at line 33
communication.ads:35:11: component "Output_Length" overlaps "Status" at line 33
communication.ads:36:42: "Response_Length" is undefined
communication.ads:37:35: "Response_Length" is undefined
communication.ads:37:62: "Output_Length" is undefined
communication.ads:38:35: "Response_Length" is undefined
communication.ads:38:53: "Output_Length" is undefined
$
I have currently solved this by overlaying the record with a large
enough array of Byte, and then calculating what portions of that I
must use Unchecked_Conversion on to find the data I need.
This works BUT:
* It is ugly and I would like to do this as as much of a schoolexample
that I can possibly do.
* Performance may be a bottleneck (probably not in the particular case
above) so I would really like to avoid copying any of the elements.
Any suggestions ?
Thanks for any help!
--
Mats Karlssohn, developer mailto:mats@mida.se
Mida Systemutveckling AB http://www.mida.se
Box 64, S-732 22 ARBOGA, SWEDEN
Phone: +46-(0)589-89808 Fax: +46-(0)589-89809
next reply other threads:[~2001-06-06 19:59 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-06 19:59 Mats Karlssohn [this message]
2001-06-06 23:06 ` records containing variable length arrays [long] Jeffrey Carter
2001-06-07 11:44 ` Mats Karlssohn
2001-06-08 2:10 ` Jeffrey Carter
2001-06-07 21:38 ` Stephen Leake
2001-06-08 12:32 ` Mats Karlssohn
2001-06-08 16:42 ` Jeffrey Carter
2001-06-08 22:28 ` Jeffrey Carter
2001-06-19 11:43 ` Mats Karlssohn
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox