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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,7e490a18b9688bd9 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.131.2 Path: g2news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!nntpfeed.proxad.net!news.dougwise.org!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Stream_Element_Array Date: Wed, 14 Sep 2011 11:40:01 +0200 Organization: cbb software GmbH Message-ID: <1e6rw4vto3ldb.i8d7fxixapx4.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: FbOMkhMtVLVmu7IwBnt1tw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: g2news2.google.com comp.lang.ada:21900 Date: 2011-09-14T11:40:01+02:00 List-Id: On Wed, 14 Sep 2011 02:09:15 -0700 (PDT), Alexander Korolev wrote: > On Sep 14, 12:31�pm, Simon Wright wrote: >> Alexander Korolev 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 : ); Get (Source : Stream_Element_Array; Index : in out Stream_Element_Offset; Data : out ); 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