comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Stream_Element_Array
Date: Wed, 14 Sep 2011 11:40:01 +0200
Date: 2011-09-14T11:40:01+02:00	[thread overview]
Message-ID: <1e6rw4vto3ldb.i8d7fxixapx4.dlg@40tude.net> (raw)
In-Reply-To: cedfbdf9-5ca7-4159-a4fc-d7b6331d1395@p37g2000prp.googlegroups.com

On Wed, 14 Sep 2011 02:09:15 -0700 (PDT), Alexander Korolev wrote:

> On Sep 14, 12:31�pm, Simon Wright <si...@pushface.org> wrote:
>> Alexander Korolev <antonovkab...@gmail.com> writes:
>>> I have a Type
>>
>>> type Message_Header is
>>> � � record
>>> � � � �-- Components � � � � � � � � �-- � 8 bit
>>> � � � �Length � �: Unsigned_16; � �-- 16 bit �(MSB)
>>> � � � �-- other components � � � � �-- � 8 bit
>>> � � end record;
>>
>>> How I could split the Lenght component on two subsequent
>>> Stream_Element ( 8-bit)?
>>
>>> Command: Stream_Element_Array (1 .. 64);
>>
>> I think the best way is using unchecked conversion. Below adds the
>> complication of converting to network byte order if not already so:
>>
>> � �subtype Two_Byte_Slice is Ada.Streams.Stream_Element_Array (1 .. 2);
>>
>> � �Big_Endian : constant Boolean
>> � � �:= System."=" (System.Default_Bit_Order, System.High_Order_First);
>>
>> � �function To_Two_Byte_Slice (S : Unsigned_16) return Two_Byte_Slice is
>> � � � function Convert is new Ada.Unchecked_Conversion (Unsigned_16,
>> � � � � � � � � � � � � � � � � � � � � � � � � � � � � Two_Byte_Slice);
>> � � � Tmp : constant Two_Byte_Slice := Convert (S);
>> � �begin
>> � � � if Big_Endian then
>> � � � � �return Tmp;
>> � � � else
>> � � � � �return (1 => Tmp (2),
>> � � � � � � � � �2 => Tmp (1));
>> � � � end if;
>> � �end To_Two_Byte_Slice;
>>
>> � �Command (11 .. 12) := To_Two_Byte_Slice (42);
> 
> Thanks Simon
> I'll try your code anyway.
> -- I thought I might made the issue more complex
> The Unsigned_16 came from transformation of the Header_Type component
> by Interfaces.Unsigned_16 (Last).
> It means I have Last: Stream_Element_Count (8 bit) set correctly.
> -- (Note For Per: I can not send the lenth to target stream because
> the whole
> -- message (stream_element_array) assembled needs further computation
> over + one transformation
> -- (staffing - something like replace 111 with 444 + 555 if 111
> occures in some components of the message including
> -- the letgh ) after the computation as an external dev requirement

Here is the pattern I am using:

   Put (Destination : in out Stream_Element_Array;
      Index : in out Stream_Element_Offset;
      Data : <the data type>
   );

   Get (Source : Stream_Element_Array;
      Index : in out Stream_Element_Offset;
      Data : out <the data type>
   );

Index indicates the position to start at. It is advanced after the put/get
elements of the buffer.

You fill the output buffer using consequent calls to Put. Then send its
filled slice Buffer (Buffer'First..Index - 1) to the hardware.

The opposite direction works similarly. You take data from the input buffer
with consequent calls to Get.

Get and Put for composite types are implemented in terms of Get and Put of
integral types. Note that their representations need not to correspond to
the hardware layouts. You are free to use Ada types best suitable to the
application.

P.S. Do not use representation clauses or unchecked conversion, I *do* mean
it:
      -- Little endian implementation of Get for Unsigned_16
   procedure Get
             (  Source : Stream_Element_Array;
                Index  : in out Stream_Element_Offset;
                Data   : out Unsigned_16
             )  is
   begin
      Data := Stream_Element'Pos (Source (Index)) +
              Stream_Element'Pos (Source (Index + 1));
      Index := Index + 2;
   end Get;

P.P.S. Stream element might be not the best choice if you are communicating
with the external world. You could also consider taking Unsigned_8 and an
array built upon it, because in most cases the hardware talks in octets.
That does not change the pattern, of course.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2011-09-14  9:40 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-14  3:13 Stream_Element_Array Alexander Korolev
2011-09-14  5:29 ` Stream_Element_Array Per Sandberg
2011-09-14  8:34   ` Stream_Element_Array Alexander Korolev
2011-09-14  8:31 ` Stream_Element_Array Simon Wright
2011-09-14  9:09   ` Stream_Element_Array Alexander Korolev
2011-09-14  9:40     ` Dmitry A. Kazakov [this message]
2011-09-14  9:41       ` Stream_Element_Array Dmitry A. Kazakov
2011-09-14 10:18         ` Stream_Element_Array Simon Wright
2011-09-14 12:42           ` Stream_Element_Array Dmitry A. Kazakov
2011-09-14 16:20             ` Stream_Element_Array Simon Wright
2011-09-14 19:53               ` Stream_Element_Array Dmitry A. Kazakov
2011-09-14 10:53       ` Stream_Element_Array Simon Wright
2011-09-14 12:48         ` Stream_Element_Array Dmitry A. Kazakov
2011-09-14 14:48           ` Stream_Element_Array Alexander Korolev
2011-09-14 15:08             ` Stream_Element_Array Dmitry A. Kazakov
2011-09-14 17:16               ` Stream_Element_Array Alexander Korolev
2011-09-14 20:13                 ` Stream_Element_Array Dmitry A. Kazakov
2011-09-14 21:29                   ` Stream_Element_Array Alexander Korolev
2011-09-15  8:20                     ` Stream_Element_Array Dmitry A. Kazakov
2011-09-15 18:58                       ` Stream_Element_Array Alexander Korolev
2011-09-15 20:48                         ` Stream_Element_Array Dmitry A. Kazakov
2011-09-16  0:20                           ` Stream_Element_Array Alexander Korolev
2011-09-15 19:15                       ` Stream_Element_Array Alexander Korolev
2011-09-15 20:11                         ` Stream_Element_Array Simon Wright
2011-09-15 20:34                           ` Stream_Element_Array Alexander Korolev
2011-09-15 21:42                             ` Stream_Element_Array Simon Wright
2011-09-15 21:50                               ` Stream_Element_Array Simon Wright
2011-09-16  0:01                                 ` Stream_Element_Array Alexander Korolev
2011-09-16  0:18                               ` Stream_Element_Array Adam Beneschan
2011-09-16  7:22                                 ` Stream_Element_Array Dmitry A. Kazakov
2011-09-16 10:21                                   ` Stream_Element_Array Simon Wright
2011-09-16 12:13                                     ` Stream_Element_Array Dmitry A. Kazakov
2011-09-16 17:20                                       ` Stream_Element_Array Simon Wright
2011-09-16 19:32                                         ` Stream_Element_Array Dmitry A. Kazakov
2011-09-16 22:18                                           ` Stream_Element_Array Simon Wright
2011-09-17  8:18                                             ` Stream_Element_Array Dmitry A. Kazakov
2011-09-19 23:22                                   ` Stream_Element_Array Randy Brukardt
2011-09-15 21:28                           ` Stream_Element_Array Alexander Korolev
2011-09-15  2:33                   ` Stream_Element_Array Alexander Korolev
2011-09-19 23:11           ` Stream_Element_Array Randy Brukardt
2011-09-14 12:19 ` Stream_Element_Array Gautier write-only
2011-09-16 11:17 ` Stream_Element_Array anon
replies disabled

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