comp.lang.ada
 help / color / mirror / Atom feed
* How to convert record?
@ 2001-10-21  9:56 Joseph Kwan
  2001-10-22 11:30 ` John McCabe
  0 siblings, 1 reply; 24+ messages in thread
From: Joseph Kwan @ 2001-10-21  9:56 UTC (permalink / raw)


Hello,

I just started to learn Ada and have been playing/working with Ada for
two weeks now.

I want to send a command buffer to serial port using florist's POSIX
interface but I have encountered problems in converting record to
POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
overcome this problem?

package ABCD is

   type Command is
      record
          A : Character;
          B : Character;
          C : Integer;
          D : Long_Integer;
      end record;    -- total 8 bytes;

...
end ABCD;


POSIX.IO.write (File_Descriptor, Command, Length);

where 

   procedure Write
     (File           : in File_Descriptor;
      Buffer         : in IO_Buffer;
      Last           : out POSIX.IO_Count;
      Masked_Signals : in POSIX.Signal_Masking
                     := POSIX.RTS_Signals);

I have searched the CLA but do not find any solutions. Can you please
help?

Thank you.



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

* Re: How to convert record?
  2001-10-21  9:56 How to convert record? Joseph Kwan
@ 2001-10-22 11:30 ` John McCabe
  2001-10-25 11:40   ` Joseph Kwan
  0 siblings, 1 reply; 24+ messages in thread
From: John McCabe @ 2001-10-22 11:30 UTC (permalink / raw)


On 21 Oct 2001 02:56:25 -0700, joseph_kwan@greenlime.com (Joseph Kwan)
wrote:

>I want to send a command buffer to serial port using florist's POSIX
>interface but I have encountered problems in converting record to
>POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
>overcome this problem?

Without looking at the Reference Manual, I would initially suggest
looking at Ada.Unchecked_Conversion to do the job.




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

* Re: How to convert record?
  2001-10-22 11:30 ` John McCabe
@ 2001-10-25 11:40   ` Joseph Kwan
  2001-10-25 12:38     ` sk
                       ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Joseph Kwan @ 2001-10-25 11:40 UTC (permalink / raw)


john.mccabe@emrad.com.nospam (John McCabe) wrote in message news:<3bd402f9.8783319@news.demon.co.uk>...
> On 21 Oct 2001 02:56:25 -0700, joseph_kwan@greenlime.com (Joseph Kwan)
> wrote:
> 
> >I want to send a command buffer to serial port using florist's POSIX
> >interface but I have encountered problems in converting record to
> >POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
> >overcome this problem?
> 
> Without looking at the Reference Manual, I would initially suggest
> looking at Ada.Unchecked_Conversion to do the job.


I thought that would work too but theoretically,
Ada.Unchecked_Conversion will not work in converting record to
POSIX.IO.IO_Buffer which is a string of characters (See spec in
florist's implementation).

Anyhow I tried to do Unchecked_Conversion and gnat gave me error
message:

"unchecked conversion to unconstrained array not allowed".

What should I do?



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

* Re: How to convert record?
  2001-10-25 11:40   ` Joseph Kwan
@ 2001-10-25 12:38     ` sk
  2001-10-25 17:12       ` Jeffrey L. Susanj
  2001-10-25 12:48     ` David C. Hoos
  2001-10-25 12:48     ` Marc A. Criley
  2 siblings, 1 reply; 24+ messages in thread
From: sk @ 2001-10-25 12:38 UTC (permalink / raw)
  To: comp.lang.ada

--Anyhow I tried to do Unchecked_Conversion and gnat gave me error
--message:
--
--"unchecked conversion to unconstrained array not allowed".
--
--What should I do?

Constrain it 

 o o
  |
 \_/

declare

    type X_Type is record
        a : Natural;
        b : Boolean;
    end record;

    Record_Character_Size : Natural := (
         X_Type'Size / Character'Size
    );

    type Buffer_Type is new String;
    -- Unconstrained Buffer_Type

    subtype String_For_Record is 
        Buffer_Type (1 .. Record_Character_Size);
    
    function To_Buffer is new Ada.Unchecked_Conversion (
        Source => X_Type, String_For_Record
    );

    X : X_Type;

begin
    Do_Something (To_Buffer(X));

end;



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

* Re: How to convert record?
  2001-10-25 11:40   ` Joseph Kwan
  2001-10-25 12:38     ` sk
@ 2001-10-25 12:48     ` David C. Hoos
  2001-10-29  1:42       ` Joseph Kwan
  2001-10-25 12:48     ` Marc A. Criley
  2 siblings, 1 reply; 24+ messages in thread
From: David C. Hoos @ 2001-10-25 12:48 UTC (permalink / raw)
  To: comp.lang.ada

Presumably, you know the size of the record.  In your original post
you showed a record with 8 bytes.

So, you would declare a constrained string subtype of the correct size
e.g., subtype Record_String is POSIX.POSIX_String (1 .. 8);

Now you can instantiate Unchecked_Conversion from the Record to
Record_String,   because Record_String is constrained.

A Record_String is a valid buffer to pass to POSIX.IO.Read.

----- Original Message -----
From: "Joseph Kwan" <joseph_kwan@greenlime.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Thursday, October 25, 2001 6:40 AM
Subject: Re: How to convert record?


> john.mccabe@emrad.com.nospam (John McCabe) wrote in message
news:<3bd402f9.8783319@news.demon.co.uk>...
> > On 21 Oct 2001 02:56:25 -0700, joseph_kwan@greenlime.com (Joseph Kwan)
> > wrote:
> >
> > >I want to send a command buffer to serial port using florist's POSIX
> > >interface but I have encountered problems in converting record to
> > >POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
> > >overcome this problem?
> >
> > Without looking at the Reference Manual, I would initially suggest
> > looking at Ada.Unchecked_Conversion to do the job.
>
>
> I thought that would work too but theoretically,
> Ada.Unchecked_Conversion will not work in converting record to
> POSIX.IO.IO_Buffer which is a string of characters (See spec in
> florist's implementation).
>
> Anyhow I tried to do Unchecked_Conversion and gnat gave me error
> message:
>
> "unchecked conversion to unconstrained array not allowed".
>
> What should I do?
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




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

* Re: How to convert record?
  2001-10-25 11:40   ` Joseph Kwan
  2001-10-25 12:38     ` sk
  2001-10-25 12:48     ` David C. Hoos
@ 2001-10-25 12:48     ` Marc A. Criley
  2001-10-25 12:54       ` sk
  2 siblings, 1 reply; 24+ messages in thread
From: Marc A. Criley @ 2001-10-25 12:48 UTC (permalink / raw)


Joseph Kwan wrote:
> 
> john.mccabe@emrad.com.nospam (John McCabe) wrote in message news:<3bd402f9.8783319@news.demon.co.uk>...
> > On 21 Oct 2001 02:56:25 -0700, joseph_kwan@greenlime.com (Joseph Kwan)
> > wrote:
> >
> > >I want to send a command buffer to serial port using florist's POSIX
> > >interface but I have encountered problems in converting record to
> > >POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
> > >overcome this problem?
> >
> > Without looking at the Reference Manual, I would initially suggest
> > looking at Ada.Unchecked_Conversion to do the job.
> 
> I thought that would work too but theoretically,
> Ada.Unchecked_Conversion will not work in converting record to
> POSIX.IO.IO_Buffer which is a string of characters (See spec in
> florist's implementation).
> 
> Anyhow I tried to do Unchecked_Conversion and gnat gave me error
> message:
> 
> "unchecked conversion to unconstrained array not allowed".
> 
> What should I do?

Caveat:  I don't have POSIX stuff here, so I'm making some assumptions.

1) How many storage units (i.e., bytes) do objects of that record type
require?  Use 'Size on a _variable_ of that type, not the type itself,
and divide by System.Storage_Unit (i.e., 8).

2) How many elements of IO_Buffer are required to occupy that same
number of storage units?  (If IO_Buffer is an array of Character, then
it is almost certainly the same as the number of storage units from
above.)

3) Define a subtype of IO_Buffer with an index constraint containing
that number of elements.

4) Instantiate Unchecked_Conversion with your record type and that
IO_Buffer subtype.

5) Convert.

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



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

* Re: How to convert record?
  2001-10-25 12:48     ` Marc A. Criley
@ 2001-10-25 12:54       ` sk
  2001-10-25 15:41         ` Ted Dennison
  0 siblings, 1 reply; 24+ messages in thread
From: sk @ 2001-10-25 12:54 UTC (permalink / raw)
  To: comp.lang.ada

-- ... Use 'Size on a _variable_ of that type, not the type itself,
-- and divide by System.Storage_Unit (i.e., 8). ...

Why variable and NOT type ?



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

* Re: How to convert record?
  2001-10-25 12:54       ` sk
@ 2001-10-25 15:41         ` Ted Dennison
  2001-10-26  3:21           ` Smark
  0 siblings, 1 reply; 24+ messages in thread
From: Ted Dennison @ 2001-10-25 15:41 UTC (permalink / raw)


In article <mailman.1004014779.9400.comp.lang.ada@ada.eu.org>, sk says...
>
>-- ... Use 'Size on a _variable_ of that type, not the type itself,
>-- and divide by System.Storage_Unit (i.e., 8). ...
>
>Why variable and NOT type ?

Because the type will return the number of bits it could theoreticly fit into,
not the number it takes up. Thus you could get a result that isn't evenly
divisible by your storage unit (usually 8).

Personally, I prefer to just adjust for that, rather than worry about it.

(Typename'size + System.Storage_Unit - 1) / System.Storage_Unit


..but that's just me.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html

No trees were killed in the sending of this message. 
However a large number of electrons were terribly inconvenienced.



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

* Re: How to convert record?
  2001-10-25 12:38     ` sk
@ 2001-10-25 17:12       ` Jeffrey L. Susanj
  0 siblings, 0 replies; 24+ messages in thread
From: Jeffrey L. Susanj @ 2001-10-25 17:12 UTC (permalink / raw)


There is no guarantee about the actual layout of a record, particularly
between compilers.  I would add a representation clause to fix the layout of
the data.


Jeff S.

"sk" <noname@myob.com> wrote in message
news:mailman.1004013820.8836.comp.lang.ada@ada.eu.org...
> --Anyhow I tried to do Unchecked_Conversion and gnat gave me error
> --message:
> --
> --"unchecked conversion to unconstrained array not allowed".
> --
> --What should I do?
>
> Constrain it
>
>  o o
>   |
>  \_/
>
> declare
>
>     type X_Type is record
>         a : Natural;
>         b : Boolean;
>     end record;
>
>     Record_Character_Size : Natural := (
>          X_Type'Size / Character'Size
>     );
>
>     type Buffer_Type is new String;
>     -- Unconstrained Buffer_Type
>
>     subtype String_For_Record is
>         Buffer_Type (1 .. Record_Character_Size);
>
>     function To_Buffer is new Ada.Unchecked_Conversion (
>         Source => X_Type, String_For_Record
>     );
>
>     X : X_Type;
>
> begin
>     Do_Something (To_Buffer(X));
>
> end;





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

* Re: How to convert record?
  2001-10-25 15:41         ` Ted Dennison
@ 2001-10-26  3:21           ` Smark
  2001-10-26  5:45             ` James Rogers
                               ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Smark @ 2001-10-26  3:21 UTC (permalink / raw)



"Ted Dennison" <dennison@telepath.com> wrote in message
news:%oWB7.96$xS6.155@www.newsranger.com...
> In article <mailman.1004014779.9400.comp.lang.ada@ada.eu.org>, sk says...
> >
> >-- ... Use 'Size on a _variable_ of that type, not the type itself,
> >-- and divide by System.Storage_Unit (i.e., 8). ...
> >
> >Why variable and NOT type ?
>
> Because the type will return the number of bits it could theoreticly fit
into,
> not the number it takes up. Thus you could get a result that isn't evenly
> divisible by your storage unit (usually 8).
>
> Personally, I prefer to just adjust for that, rather than worry about it.
>
> (Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
>

Well, would you worry about it with a type like this:

type Bubba_Type is record
    C : Character; -- one byte
    I   : Integer; -- 4 bytes
end record;

Bubba : Bubba_Type;

Suppose that Integers must align on 4-byte boundaries.  Then you might get:

Bubba_Type'Size = 40 (= 5 bytes)

(Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
= ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)

But actually:

Bubba'Size = 64 (= 8 bytes)

GNAT provides a nice attribute 'Object_Size that can be used on the type to
give you what the size of an object of that type would be.

Markku Kotiaho







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

* Re: How to convert record?
  2001-10-26  3:21           ` Smark
@ 2001-10-26  5:45             ` James Rogers
  2001-10-26 17:46               ` Smark
  2001-10-26 14:30             ` Ted Dennison
  2001-10-26 23:10             ` Jeffrey Carter
  2 siblings, 1 reply; 24+ messages in thread
From: James Rogers @ 2001-10-26  5:45 UTC (permalink / raw)


Smark wrote:
> 
> Well, would you worry about it with a type like this:
> 
> type Bubba_Type is record
>     C : Character; -- one byte
>     I   : Integer; -- 4 bytes
> end record;
> 
> Bubba : Bubba_Type;
> 
> Suppose that Integers must align on 4-byte boundaries.  Then you might get:
> 
> Bubba_Type'Size = 40 (= 5 bytes)
> 
> (Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
> = ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)
> 
> But actually:
> 
> Bubba'Size = 64 (= 8 bytes)
> 

Of course, you might be able to fix the problem by simply
reordering the components. The soundest way to do this is via a
representation clause.

type Bubba_Type is record
   I : Integer;
   C : Character;
end record;

for Bubba_Type use record
   I : at 0 range 0..31;
   C : at 4 range 0..7;
end record;

Jim Rogers
Colorado Springs, Colorado USA



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

* Re: How to convert record?
  2001-10-26  3:21           ` Smark
  2001-10-26  5:45             ` James Rogers
@ 2001-10-26 14:30             ` Ted Dennison
  2001-10-26 17:42               ` Smark
  2001-10-26 23:10             ` Jeffrey Carter
  2 siblings, 1 reply; 24+ messages in thread
From: Ted Dennison @ 2001-10-26 14:30 UTC (permalink / raw)


In article <FE4C7.31012$pb4.15204037@news2.rdc2.tx.home.com>, Smark says...
>
>
>type Bubba_Type is record
>    C : Character; -- one byte
>    I   : Integer; -- 4 bytes
>end record;
>
>Bubba : Bubba_Type;
>
>Suppose that Integers must align on 4-byte boundaries.  Then you might get:
>
>Bubba_Type'Size = 40 (= 5 bytes)
>
>(Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
>= ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)
>
>But actually:
>
>Bubba'Size = 64 (= 8 bytes)

I do not believe that is the case. If objects of this type are going to have
holes in the middle for alignment purposes, then that should be reflected in the
type's size.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html

No trees were killed in the sending of this message. 
However a large number of electrons were terribly inconvenienced.



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

* Re: How to convert record?
  2001-10-26 14:30             ` Ted Dennison
@ 2001-10-26 17:42               ` Smark
  2001-10-26 20:02                 ` Ted Dennison
  0 siblings, 1 reply; 24+ messages in thread
From: Smark @ 2001-10-26 17:42 UTC (permalink / raw)


"Ted Dennison" <dennison@telepath.com> wrote in message
news:MreC7.1456$xS6.2002@www.newsranger.com...
> In article <FE4C7.31012$pb4.15204037@news2.rdc2.tx.home.com>, Smark
says...
> >
> >
> >type Bubba_Type is record
> >    C : Character; -- one byte
> >    I   : Integer; -- 4 bytes
> >end record;
> >
> >Bubba : Bubba_Type;
> >
> >Suppose that Integers must align on 4-byte boundaries.  Then you might
get:
> >
> >Bubba_Type'Size = 40 (= 5 bytes)
> >
> >(Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
> >= ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)
> >
> >But actually:
> >
> >Bubba'Size = 64 (= 8 bytes)
>
> I do not believe that is the case. If objects of this type are going to
have
> holes in the middle for alignment purposes, then that should be reflected
in the
> type's size.
>

Try it out, or check out what the GNAT reference manual has to say about
GNAT's
'Object_Size attribute (don't you suppose there is a reason that GNAT added
this attribute?).

Markku







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

* Re: How to convert record?
  2001-10-26  5:45             ` James Rogers
@ 2001-10-26 17:46               ` Smark
  0 siblings, 0 replies; 24+ messages in thread
From: Smark @ 2001-10-26 17:46 UTC (permalink / raw)


"James Rogers" <jimmaureenrogers@worldnet.att.net> wrote in message
news:3BD8F891.BD8792B1@worldnet.att.net...
> Smark wrote:
> >
> > Well, would you worry about it with a type like this:
> >
> > type Bubba_Type is record
> >     C : Character; -- one byte
> >     I   : Integer; -- 4 bytes
> > end record;
> >
> > Bubba : Bubba_Type;
> >
> > Suppose that Integers must align on 4-byte boundaries.  Then you might
get:
> >
> > Bubba_Type'Size = 40 (= 5 bytes)
> >
> > (Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
> > = ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)
> >
> > But actually:
> >
> > Bubba'Size = 64 (= 8 bytes)
> >
>
> Of course, you might be able to fix the problem by simply
> reordering the components. The soundest way to do this is via a
> representation clause.
>
> type Bubba_Type is record
>    I : Integer;
>    C : Character;
> end record;
>
> for Bubba_Type use record
>    I : at 0 range 0..31;
>    C : at 4 range 0..7;
> end record;
>

Okay, but reordering is not always an option.  Also, it won't help if you
need an
array of Bubba_Type.

Markku





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

* Re: How to convert record?
  2001-10-26 17:42               ` Smark
@ 2001-10-26 20:02                 ` Ted Dennison
  0 siblings, 0 replies; 24+ messages in thread
From: Ted Dennison @ 2001-10-26 20:02 UTC (permalink / raw)


In article <mghC7.32891$pb4.16100029@news2.rdc2.tx.home.com>, Smark says...
>
>"Ted Dennison" <dennison@telepath.com> wrote in message
>news:MreC7.1456$xS6.2002@www.newsranger.com...
>> In article <FE4C7.31012$pb4.15204037@news2.rdc2.tx.home.com>, Smark
>says...
>> >
>> >
>> >type Bubba_Type is record
>> >    C : Character; -- one byte
>> >    I   : Integer; -- 4 bytes
>> >end record;
>> >
>> >Bubba : Bubba_Type;
>> >
>> >Suppose that Integers must align on 4-byte boundaries.  Then you might
>> >get:
>> >
>> >Bubba_Type'Size = 40 (= 5 bytes)
>> >
>> >(Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
>> >= ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)
>> >
>> >But actually:
>> >
>> >Bubba'Size = 64 (= 8 bytes)
>>
>> I do not believe that is the case. If objects of this type are going to
>> have holes in the middle for alignment purposes, then that should be 
>> reflected in the type's size.
>>
>
>Try it out, or check out what the GNAT reference manual has to say about
>GNAT's 'Object_Size attribute 

I did. I'm right. With Gnat on Win32, both Bubba_Type'size and Bubba'size are
64.

The example in the UG has the fields reversed from your example. I'll give you
that it does say that the type's 'size might be more than 7 bits smaller than
the object's 'size. However, it does *not* say that it would return some kind of
"packed" size for a type, where holes in the *middle* of a record are ignored,
as you seem to be implying. I do not believe an Ada compiler is allowed to do
this.

>(don't you suppose there is a reason that GNAT added this attribute?).

I thought it was so that you didn't have to have an object hanging around to
take a 'size on to get an exact size. As a matter of fact, I still think that.
:-)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html

No trees were killed in the sending of this message. 
However a large number of electrons were terribly inconvenienced.



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

* Re: How to convert record?
  2001-10-26  3:21           ` Smark
  2001-10-26  5:45             ` James Rogers
  2001-10-26 14:30             ` Ted Dennison
@ 2001-10-26 23:10             ` Jeffrey Carter
  2 siblings, 0 replies; 24+ messages in thread
From: Jeffrey Carter @ 2001-10-26 23:10 UTC (permalink / raw)


Smark wrote:
> 
> Bubba_Type'Size = 40 (= 5 bytes)
> 
> (Typename'size + System.Storage_Unit - 1) / System.Storage_Unit
> = ( 40 + 8 - 1)/8 = 47/8 = 6 (bytes)

47/8=5. Integer division truncates. That's why this technique works.

-- 
Jeff Carter
"I wave my private parts at your aunties."
Monty Python & the Holy Grail



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

* Re: How to convert record?
  2001-10-25 12:48     ` David C. Hoos
@ 2001-10-29  1:42       ` Joseph Kwan
  2001-10-29 13:35         ` Marc A. Criley
  0 siblings, 1 reply; 24+ messages in thread
From: Joseph Kwan @ 2001-10-29  1:42 UTC (permalink / raw)


"David C. Hoos" <david.c.hoos.sr@ada95.com> wrote in message news:<mailman.1004014002.9071.comp.lang.ada@ada.eu.org>...
> Presumably, you know the size of the record.  In your original post
> you showed a record with 8 bytes.
> 
> So, you would declare a constrained string subtype of the correct size
> e.g., subtype Record_String is POSIX.POSIX_String (1 .. 8);
> 
> Now you can instantiate Unchecked_Conversion from the Record to
> Record_String,   because Record_String is constrained.
> 
> A Record_String is a valid buffer to pass to POSIX.IO.Read.


So, it must be a constrained type/subtype. This solve my problems.
Thanks.

OTOH, I read about 13.13.2 Stream-Oriented Attributes in LRM. Can I
use 'Read and 'Write or 'Input and 'Output for [this] purpose?

Can someone please explain in details (an example of the
implementation will be of great help) how to implement these
attributes? Please forgive my stupidity because I can't find a good
examples of such implementation in books and LRM and certainly am
unable to figure it out.

Thank you.





> >
> > I thought that would work too but theoretically,
> > Ada.Unchecked_Conversion will not work in converting record to
> > POSIX.IO.IO_Buffer which is a string of characters (See spec in
> > florist's implementation).
> >
> > Anyhow I tried to do Unchecked_Conversion and gnat gave me error
> > message:
> >
> > "unchecked conversion to unconstrained array not allowed".



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

* Re: How to convert record?
  2001-10-29  1:42       ` Joseph Kwan
@ 2001-10-29 13:35         ` Marc A. Criley
  0 siblings, 0 replies; 24+ messages in thread
From: Marc A. Criley @ 2001-10-29 13:35 UTC (permalink / raw)


Joseph Kwan wrote:
> 
> So, it must be a constrained type/subtype. This solve my problems.
> Thanks.
> 
> OTOH, I read about 13.13.2 Stream-Oriented Attributes in LRM. Can I
> use 'Read and 'Write or 'Input and 'Output for [this] purpose?
> 
> Can someone please explain in details (an example of the
> implementation will be of great help) how to implement these
> attributes? Please forgive my stupidity because I can't find a good
> examples of such implementation in books and LRM and certainly am
> unable to figure it out.
> 
> Thank you.

You can take a look at my article in Ada Letters--A Socket-Based
Manifestation of Streams
(www.acm.org/sigs/sigada/ada_letters/issues/june2001/socket_streams.pdf)
for a discussion and full working example of how to use the stream
attributes with sockets.  Replace the socket functionality with the
necessary buffering and I/O.

Part of the motivation for the article was to demonstrate how to extend
streams for storage and communication mechanisms other than files, just
as you're asking about.

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



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

* RE: How to convert record?
@ 2001-10-29 18:16 Beard, Frank
  2001-10-30  1:17 ` Adrian Hoe
  0 siblings, 1 reply; 24+ messages in thread
From: Beard, Frank @ 2001-10-29 18:16 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

One other thing you might want to consider:


procedure Write_ABCD (the_Command : Command) is

   Length : Posix.IO_Count :=
Posix.IO_Count(the_Command'size/Posix.Posix_Character'size);
   Buffer : IO_Buffer (1 .. positive(length));
   for Buffer use at the_Command'address;

begin

   POSIX.IO.write (File_Descriptor, Buffer, Length);

end Write_ABCD;


I've used it on both Windows and a couple of Unix flavors without
any problems, but we were always communicating between homogenous 
platforms (same hardware, same OS).  If you are in a heterogeneous
environment, you have to use a different method, but you have the
same problem with Unchecked_Conversion.

I like it because it's fairly straight forward and it doesn't copy
data like Unchecked_Conversion, which is particularly good when
dealing with large buffers.

Frank


-----Original Message-----
From: joseph_kwan@greenlime.com [mailto:joseph_kwan@greenlime.com]


Hello,

I just started to learn Ada and have been playing/working with Ada for
two weeks now.

I want to send a command buffer to serial port using florist's POSIX
interface but I have encountered problems in converting record to
POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
overcome this problem?

package ABCD is

   type Command is
      record
          A : Character;
          B : Character;
          C : Integer;
          D : Long_Integer;
      end record;    -- total 8 bytes;

...
end ABCD;


POSIX.IO.write (File_Descriptor, Command, Length);

where 

   procedure Write
     (File           : in File_Descriptor;
      Buffer         : in IO_Buffer;
      Last           : out POSIX.IO_Count;
      Masked_Signals : in POSIX.Signal_Masking
                     := POSIX.RTS_Signals);

I have searched the CLA but do not find any solutions. Can you please
help?

Thank you.
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada



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

* Re: How to convert record?
  2001-10-29 18:16 Beard, Frank
@ 2001-10-30  1:17 ` Adrian Hoe
  0 siblings, 0 replies; 24+ messages in thread
From: Adrian Hoe @ 2001-10-30  1:17 UTC (permalink / raw)


"Beard, Frank" <beardf@spawar.navy.mil> wrote in message news:<mailman.1004379548.31870.comp.lang.ada@ada.eu.org>...
> One other thing you might want to consider:
> 
> 
> procedure Write_ABCD (the_Command : Command) is
> 
>    Length : Posix.IO_Count :=
> Posix.IO_Count(the_Command'size/Posix.Posix_Character'size);
>    Buffer : IO_Buffer (1 .. positive(length));
>    for Buffer use at the_Command'address;
> 
> begin
> 
>    POSIX.IO.write (File_Descriptor, Buffer, Length);
> 
> end Write_ABCD;
> 
> 
> I've used it on both Windows and a couple of Unix flavors without
> any problems, but we were always communicating between homogenous 
> platforms (same hardware, same OS).  If you are in a heterogeneous
> environment, you have to use a different method, but you have the
> same problem with Unchecked_Conversion.
> 
> I like it because it's fairly straight forward and it doesn't copy
> data like Unchecked_Conversion, which is particularly good when
> dealing with large buffers.
> 
> Frank


Then you would have to take care of the_Command'Size. The 8-byte
record will give you no problem at all. See my post thread "Size and
Pack".



> 
> -----Original Message-----
> From: joseph_kwan@greenlime.com [mailto:joseph_kwan@greenlime.com]
> 
> 
> Hello,
> 
> I just started to learn Ada and have been playing/working with Ada for
> two weeks now.
> 
> I want to send a command buffer to serial port using florist's POSIX
> interface but I have encountered problems in converting record to
> POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
> overcome this problem?
> 
> package ABCD is
> 
>    type Command is
>       record
>           A : Character;
>           B : Character;
>           C : Integer;
>           D : Long_Integer;
>       end record;    -- total 8 bytes;
> 
> ...
> end ABCD;
> 
> 
> POSIX.IO.write (File_Descriptor, Command, Length);
> 
> where 
> 
>    procedure Write
>      (File           : in File_Descriptor;
>       Buffer         : in IO_Buffer;
>       Last           : out POSIX.IO_Count;
>       Masked_Signals : in POSIX.Signal_Masking
>                      := POSIX.RTS_Signals);
> 
> I have searched the CLA but do not find any solutions. Can you please
> help?
> 
> Thank you.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada



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

* RE: How to convert record?
@ 2001-10-30 19:05 Beard, Frank
  2001-10-31  4:10 ` Adrian Hoe
  0 siblings, 1 reply; 24+ messages in thread
From: Beard, Frank @ 2001-10-30 19:05 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

But using this method it doesn't matter whether it's packed
or not, or whether it's 14 or 11 bytes.  the_Command'size
will give you the size of the "object" being passed in, which
will be translated to the equivalent size IO_Buffer.  So long 
as the receiver is on the same platform (same hardware and
same OS), or represents the data the same way, it will work.
So, there is no need to "take care of" the_Command'Size.

Frank

-----Original Message-----
From: byhoe@greenlime.com [mailto:byhoe@greenlime.com]
Sent: Monday, October 29, 2001 8:18 PM
To: comp.lang.ada@ada.eu.org
Subject: Re: How to convert record?


"Beard, Frank" <beardf@spawar.navy.mil> wrote in message
news:<mailman.1004379548.31870.comp.lang.ada@ada.eu.org>...
> One other thing you might want to consider:
> 
> 
> procedure Write_ABCD (the_Command : Command) is
> 
>    Length : Posix.IO_Count :=
> Posix.IO_Count(the_Command'size/Posix.Posix_Character'size);
>    Buffer : IO_Buffer (1 .. positive(length));
>    for Buffer use at the_Command'address;
> 
> begin
> 
>    POSIX.IO.write (File_Descriptor, Buffer, Length);
> 
> end Write_ABCD;
> 
> 
> I've used it on both Windows and a couple of Unix flavors without
> any problems, but we were always communicating between homogenous 
> platforms (same hardware, same OS).  If you are in a heterogeneous
> environment, you have to use a different method, but you have the
> same problem with Unchecked_Conversion.
> 
> I like it because it's fairly straight forward and it doesn't copy
> data like Unchecked_Conversion, which is particularly good when
> dealing with large buffers.
> 
> Frank


Then you would have to take care of the_Command'Size. The 8-byte
record will give you no problem at all. See my post thread "Size and
Pack".



> 
> -----Original Message-----
> From: joseph_kwan@greenlime.com [mailto:joseph_kwan@greenlime.com]
> 
> 
> Hello,
> 
> I just started to learn Ada and have been playing/working with Ada for
> two weeks now.
> 
> I want to send a command buffer to serial port using florist's POSIX
> interface but I have encountered problems in converting record to
> POSIX.IO.IO_Buffer or Ada.Streams.Stream_Element_Array. How can I
> overcome this problem?
> 
> package ABCD is
> 
>    type Command is
>       record
>           A : Character;
>           B : Character;
>           C : Integer;
>           D : Long_Integer;
>       end record;    -- total 8 bytes;
> 
> ...
> end ABCD;
> 
> 
> POSIX.IO.write (File_Descriptor, Command, Length);
> 
> where 
> 
>    procedure Write
>      (File           : in File_Descriptor;
>       Buffer         : in IO_Buffer;
>       Last           : out POSIX.IO_Count;
>       Masked_Signals : in POSIX.Signal_Masking
>                      := POSIX.RTS_Signals);
> 
> I have searched the CLA but do not find any solutions. Can you please
> help?
> 
> Thank you.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada



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

* Re: How to convert record?
  2001-10-30 19:05 Beard, Frank
@ 2001-10-31  4:10 ` Adrian Hoe
  0 siblings, 0 replies; 24+ messages in thread
From: Adrian Hoe @ 2001-10-31  4:10 UTC (permalink / raw)


"Beard, Frank" <beardf@spawar.navy.mil> wrote in message news:<mailman.1004468879.10255.comp.lang.ada@ada.eu.org>...
> But using this method it doesn't matter whether it's packed
> or not, or whether it's 14 or 11 bytes.  the_Command'size
> will give you the size of the "object" being passed in, which
> will be translated to the equivalent size IO_Buffer.  So long 
> as the receiver is on the same platform (same hardware and
> same OS), or represents the data the same way, it will work.
> So, there is no need to "take care of" the_Command'Size.
> 
> Frank
> 


You are wrong. Try compile the following example and you will get 14
and 16 for each.

-------------------------------------------------------
with Ada.Text_Io;          use Ada.Text_Io;
with Ada.Integer_Text_Io;  use Ada.Integer_Text_Io;

procedure Test2 is
   type Rx_Header_Data is
      record
         Start_Byte   : Character;
         Splitter     : Character;
         Command_Byte : Character;
         Pad_Byte_1   : Character;
         Pad_Byte_2   : Character;
         Log_Num      : Long_Integer;
         End_Byte     : Character;
         LRC          : Character;
      end record;
--   pragma Pack (Rx_Header_Data);

   Command : Rx_Header_Data;

   procedure Print_Size (Command : in Rx_Header_Data) is
   begin
      New_Line;
      Put (Command'Size / 8);
   end Print_Size;

begin
   Command.Start_Byte := Character'Val (2);
   Put (Rx_Header_Data'Size / 8);
   New_Line;
   Put (Command'Size / 8);
   Print_Size (Command);
end Test2;
------------------------------------------------------------------

Uncomment the "pragma pack (Rx_Header_Data);", compile again and run
the program and you will get 11. 11 bytes is exactly the length of the
actual record.

Your method "could" lead to errors if the purpose is to interface to
external devices which require exactly 11 bytes and in the exact order
as defined.




> "Beard, Frank" <beardf@spawar.navy.mil> wrote in message
> news:<mailman.1004379548.31870.comp.lang.ada@ada.eu.org>...
> > One other thing you might want to consider:
> > 
> > 
> > procedure Write_ABCD (the_Command : Command) is
> > 
> > 
> > begin
> > 
> >    POSIX.IO.write (File_Descriptor, Buffer, Length);
> > 
> > end Write_ABCD;
> > 
> > 
> > I've used it on both Windows and a couple of Unix flavors without
> > any problems, but we were always communicating between homogenous 
> > platforms (same hardware, same OS).  If you are in a heterogeneous
> > environment, you have to use a different method, but you have the
> > same problem with Unchecked_Conversion.
> > 
> > I like it because it's fairly straight forward and it doesn't copy
> > data like Unchecked_Conversion, which is particularly good when
> > dealing with large buffers.
> > 
> > Frank
> 
> 
> Then you would have to take care of the_Command'Size. The 8-byte
> record will give you no problem at all. See my post thread "Size and
> Pack".




> > package ABCD is
> > 
> >    type Command is
> >       record
> >           A : Character;
> >           B : Character;
> >           C : Integer;
> >           D : Long_Integer;
> >       end record;    -- total 8 bytes;
> > 
> > ...
> > end ABCD;
> > 
> > 
> > POSIX.IO.write (File_Descriptor, Command, Length);
> > 
> > where 
> > 
> >    procedure Write
> >      (File           : in File_Descriptor;
> >       Buffer         : in IO_Buffer;
> >       Last           : out POSIX.IO_Count;
> >       Masked_Signals : in POSIX.Signal_Masking
> >                      := POSIX.RTS_Signals);
> > 
> > I have searched the CLA but do not find any solutions. Can you please
> > help?
> > 
> > Thank you.



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

* RE: How to convert record?
@ 2001-10-31 19:40 Beard, Frank
  2001-11-01  2:13 ` Adrian Hoe
  0 siblings, 1 reply; 24+ messages in thread
From: Beard, Frank @ 2001-10-31 19:40 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'


-----Original Message-----
From: byhoe@greenlime.com [mailto:byhoe@greenlime.com]

> You are wrong. Try compile the following example and you will get 14
> and 16 for each.

I'm not wrong.  Your posts on this thread said nothing about requiring
it to be 11 bytes.  You were asking how to convert it to a POSIX IO_Buffer.

What I was saying is, if ALL YOU WANT TO DO is send and receive it across
homogeneous platforms then you don't need to worry about the size.  Let
the system, via 'size of the object, do it for you.  The compiler will 
do what is more efficient for it's processing.  There's no reason to
work against it unless you have to.

If you are sending it to a device that requires it to be 11 bytes, then
that's a different issue.  And of course a rep spec and pragma pack are
going to make a difference in the size, but that wasn't the issue either.

Your thread on "Size and pack" only led me to believe you expected 11
bytes, but got 14.  NOT that it HAD to be 11.  Expecting one thing and
getting something else does not imply a requirement.

So, which is it?  Does it have to be 11, or are you just sending it to
an equivalent peer system.




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

* Re: How to convert record?
  2001-10-31 19:40 Beard, Frank
@ 2001-11-01  2:13 ` Adrian Hoe
  0 siblings, 0 replies; 24+ messages in thread
From: Adrian Hoe @ 2001-11-01  2:13 UTC (permalink / raw)


"Beard, Frank" <beardf@spawar.navy.mil> wrote in message news:<mailman.1004557367.26087.comp.lang.ada@ada.eu.org>...
> -----Original Message-----
> From: byhoe@greenlime.com [mailto:byhoe@greenlime.com]
> 
> > You are wrong. Try compile the following example and you will get 14
> > and 16 for each.
> 
> I'm not wrong.  Your posts on this thread said nothing about requiring
> it to be 11 bytes.  You were asking how to convert it to a POSIX IO_Buffer.

You are wrong in another way. :) Joseph asked to convert it to POSIX
IO_Buffer, not me, Adrian!

> What I was saying is, if ALL YOU WANT TO DO is send and receive it across
> homogeneous platforms then you don't need to worry about the size.  Let
> the system, via 'size of the object, do it for you.  The compiler will 
> do what is more efficient for it's processing.  There's no reason to
> work against it unless you have to.
> 
> If you are sending it to a device that requires it to be 11 bytes, then
> that's a different issue.  And of course a rep spec and pragma pack are
> going to make a difference in the size, but that wasn't the issue either.
> 
> Your thread on "Size and pack" only led me to believe you expected 11
> bytes, but got 14.  NOT that it HAD to be 11.  Expecting one thing and
> getting something else does not imply a requirement.
> 
> So, which is it?  Does it have to be 11, or are you just sending it to
> an equivalent peer system.

OK. My apologies to the wrong pointing. It's my fault but I knew
Joseph is working on a similar project like mine (we are colleagues :)
and my instinct immediately jumped to the conclusion without
thoroughly going through your post properly. Hey.. Take it easy, I am
not written in Ada! :,)



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

end of thread, other threads:[~2001-11-01  2:13 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-21  9:56 How to convert record? Joseph Kwan
2001-10-22 11:30 ` John McCabe
2001-10-25 11:40   ` Joseph Kwan
2001-10-25 12:38     ` sk
2001-10-25 17:12       ` Jeffrey L. Susanj
2001-10-25 12:48     ` David C. Hoos
2001-10-29  1:42       ` Joseph Kwan
2001-10-29 13:35         ` Marc A. Criley
2001-10-25 12:48     ` Marc A. Criley
2001-10-25 12:54       ` sk
2001-10-25 15:41         ` Ted Dennison
2001-10-26  3:21           ` Smark
2001-10-26  5:45             ` James Rogers
2001-10-26 17:46               ` Smark
2001-10-26 14:30             ` Ted Dennison
2001-10-26 17:42               ` Smark
2001-10-26 20:02                 ` Ted Dennison
2001-10-26 23:10             ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2001-10-29 18:16 Beard, Frank
2001-10-30  1:17 ` Adrian Hoe
2001-10-30 19:05 Beard, Frank
2001-10-31  4:10 ` Adrian Hoe
2001-10-31 19:40 Beard, Frank
2001-11-01  2:13 ` Adrian Hoe

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