comp.lang.ada
 help / color / mirror / Atom feed
* Type convertion
@ 2008-03-11 16:10 news.broadpark.no
  2008-03-11 16:59 ` Ed Falis
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: news.broadpark.no @ 2008-03-11 16:10 UTC (permalink / raw)


I have a simple question. I'm working on an Ada package I have received.

- Is it possible to covert a Boolean to Integer in an easy way
- Is it possible to covert an enum to Integer in an easy way

In the second question i typically have something like this:

   type My_Type is  (AB, CD, EF, GH, IJ);

Now, I have a declaration:

   MyTest : My_Type;

The result is stored in MyTest. I'm writing an interface to a C based 
program and I need to convert it to Integer. I'm new to Ada, so any hint 
would help.

Eirik




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

* Re: Type convertion
  2008-03-11 16:10 Type convertion news.broadpark.no
@ 2008-03-11 16:59 ` Ed Falis
  2008-03-11 20:45   ` Ludovic Brenta
  2008-03-11 17:39 ` Jeffrey R. Carter
  2008-03-11 17:49 ` Stuart
  2 siblings, 1 reply; 12+ messages in thread
From: Ed Falis @ 2008-03-11 16:59 UTC (permalink / raw)


On Tue, 11 Mar 2008 12:10:35 -0400, news.broadpark.no  
<etjensen@broadpark.no> wrote:

> I have a simple question. I'm working on an Ada package I have received.
>
> - Is it possible to covert a Boolean to Integer in an easy way
> - Is it possible to covert an enum to Integer in an easy way
>
> In the second question i typically have something like this:
>
>    type My_Type is  (AB, CD, EF, GH, IJ);
>
> Now, I have a declaration:
>
>    MyTest : My_Type;
>
> The result is stored in MyTest. I'm writing an interface to a C based  
> program and I need to convert it to Integer. I'm new to Ada, so any hint  
> would help.
>
> Eirik
>

I expect:

pragma Convention (C, My_Type);

would do the trick for you.



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

* Re: Type convertion
  2008-03-11 16:10 Type convertion news.broadpark.no
  2008-03-11 16:59 ` Ed Falis
@ 2008-03-11 17:39 ` Jeffrey R. Carter
  2008-03-11 20:53   ` news.broadpark.no
  2008-03-11 17:49 ` Stuart
  2 siblings, 1 reply; 12+ messages in thread
From: Jeffrey R. Carter @ 2008-03-11 17:39 UTC (permalink / raw)


news.broadpark.no wrote:
> 
> - Is it possible to covert a Boolean to Integer in an easy way
> - Is it possible to covert an enum to Integer in an easy way

Take a look at the 'Pos attribute, and its inverse, 'Val.

-- 
Jeff Carter
"C's solution to this [variable-sized array parameters] has real
problems, and people who are complaining about safety definitely
have a point."
Dennis Ritchie
25



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

* Re: Type convertion
  2008-03-11 16:10 Type convertion news.broadpark.no
  2008-03-11 16:59 ` Ed Falis
  2008-03-11 17:39 ` Jeffrey R. Carter
@ 2008-03-11 17:49 ` Stuart
  2008-03-11 20:56   ` news.broadpark.no
  2 siblings, 1 reply; 12+ messages in thread
From: Stuart @ 2008-03-11 17:49 UTC (permalink / raw)


"news.broadpark.no" <etjensen@broadpark.no> wrote in message 
news:47d6ae9b$1@news.broadpark.no...
>I have a simple question. I'm working on an Ada package I have received.
>
> - Is it possible to covert a Boolean to Integer in an easy way
> - Is it possible to covert an enum to Integer in an easy way

Yes - but it depends on what integers you want.

First, try checking up on the 'pos attribute for enumerated types - ARM 
3.5.5

> In the second question i typically have something like this:
>
>   type My_Type is  (AB, CD, EF, GH, IJ);
>
> Now, I have a declaration:
>
>   MyTest : My_Type;
>
> The result is stored in MyTest. I'm writing an interface to a C based 
> program and I need to convert it to Integer. I'm new to Ada, so any hint 
> would help.

If you are interfacing to a C program you perhaps should look at ARM Annex 
B.  For the interface you should use one of the Integer_n/Unsigned_n types.

If you simply want the mapping
    AB => 0
    CD => 1
    EF => 2
    GH => 3
    IJ => 4

you could use
    My_Type'pos(MyTest)

Another way is to declare an array of Interfaces.Integer_n  indexed by 
My_Type which contains the values you want for each value of My_Type.

You could use representation clauses for My_Type and Unchecked_Conversions - 
but this is probably not a good idea.

HTH

-- 
Stuart

If you simply want 





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

* Re: Type convertion
  2008-03-11 16:59 ` Ed Falis
@ 2008-03-11 20:45   ` Ludovic Brenta
  2008-03-12 20:36     ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: Ludovic Brenta @ 2008-03-11 20:45 UTC (permalink / raw)


Ed Falis writes:
> On Tue, 11 Mar 2008 12:10:35 -0400, news.broadpark.no
> <etjensen@broadpark.no> wrote:
>
>> I have a simple question. I'm working on an Ada package I have received.
>>
>> - Is it possible to covert a Boolean to Integer in an easy way
>> - Is it possible to covert an enum to Integer in an easy way
>>
>> In the second question i typically have something like this:
>>
>>    type My_Type is  (AB, CD, EF, GH, IJ);
>>
>> Now, I have a declaration:
>>
>>    MyTest : My_Type;
>>
>> The result is stored in MyTest. I'm writing an interface to a C
>> based program and I need to convert it to Integer. I'm new to Ada,
>> so any hint  would help.
>>
>> Eirik
>>
>
> I expect:
>
> pragma Convention (C, My_Type);
>
> would do the trick for you.

Yes, and to elaborate on that, the interfacing below should Do The
Right Thing(tm) automatically:

<code language="C">
typedef enum { ab, cd, ef, gh, ij } type_t;

void foo (type_t param);
</code>

<code language="Ada">
package P is
   type My_Type is  (AB, CD, EF, GH, IJ);
   pragma Convention (C, My_Type);

   procedure Foo (Param : in My_Type);
   pragma Import (C, Foo, "foo");
end P;
</code>


If, however, the C part doesn't have an enum but something like:

<code language="C">
#define AB 2
#define CD 5
#define EF 8
#define GH 9
#define IJ 42

void foo (int param);
</code>

then you can write:

<code language="Ada">
package P is
   type My_Type is  (AB, CD, EF, GH, IJ);
   pragma Convention (C, My_Type);
   for My_Type use (AB => 2,
                    CD => 5,
                    EF => 8,
                    GH => 9,
                    IJ => 42);

   procedure Foo (Param : in My_Type);
   pragma Import (C, Foo, "foo");
end P;
</code>

HTH

-- 
Ludovic Brenta.



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

* Re: Type convertion
  2008-03-11 17:39 ` Jeffrey R. Carter
@ 2008-03-11 20:53   ` news.broadpark.no
  2008-03-11 21:40     ` Ludovic Brenta
  0 siblings, 1 reply; 12+ messages in thread
From: news.broadpark.no @ 2008-03-11 20:53 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> wrote in message 
news:XszBj.71283$yE1.45295@attbi_s21...
> news.broadpark.no wrote:
>>
>> - Is it possible to covert a Boolean to Integer in an easy way
>> - Is it possible to covert an enum to Integer in an easy way
>
> Take a look at the 'Pos attribute, and its inverse, 'Val.
>

Works fine. Thank you!

Eirik 




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

* Re: Type convertion
  2008-03-11 17:49 ` Stuart
@ 2008-03-11 20:56   ` news.broadpark.no
  0 siblings, 0 replies; 12+ messages in thread
From: news.broadpark.no @ 2008-03-11 20:56 UTC (permalink / raw)


"Stuart" <stuart@0.0> wrote in message 
news:47d6c231$1_1@glkas0286.greenlnk.net...
> "news.broadpark.no" <etjensen@broadpark.no> wrote in message 
> news:47d6ae9b$1@news.broadpark.no...
>>I have a simple question. I'm working on an Ada package I have received.
>>
>> - Is it possible to covert a Boolean to Integer in an easy way
>> - Is it possible to covert an enum to Integer in an easy way
>
> Yes - but it depends on what integers you want.
>
> First, try checking up on the 'pos attribute for enumerated types - ARM 
> 3.5.5
>
>> In the second question i typically have something like this:
>>
>>   type My_Type is  (AB, CD, EF, GH, IJ);
>>
>> Now, I have a declaration:
>>
>>   MyTest : My_Type;
>>
>> The result is stored in MyTest. I'm writing an interface to a C based 
>> program and I need to convert it to Integer. I'm new to Ada, so any hint 
>> would help.
>
> If you are interfacing to a C program you perhaps should look at ARM Annex 
> B.  For the interface you should use one of the Integer_n/Unsigned_n 
> types.
>
> If you simply want the mapping
>    AB => 0
>    CD => 1
>    EF => 2
>    GH => 3
>    IJ => 4
>
> you could use
>    My_Type'pos(MyTest)
>
> Another way is to declare an array of Interfaces.Integer_n  indexed by 
> My_Type which contains the values you want for each value of My_Type.
>
> You could use representation clauses for My_Type and 
> Unchecked_Conversions - but this is probably not a good idea.

My_Type'pos and My_Type'val did the job. Thank's!

Eirik




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

* Re: Type convertion
  2008-03-11 20:53   ` news.broadpark.no
@ 2008-03-11 21:40     ` Ludovic Brenta
  0 siblings, 0 replies; 12+ messages in thread
From: Ludovic Brenta @ 2008-03-11 21:40 UTC (permalink / raw)


"news.broadpark.no" <etjensen@broadpark.no> writes:

> "Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> wrote in message
> news:XszBj.71283$yE1.45295@attbi_s21...
>> news.broadpark.no wrote:
>>>
>>> - Is it possible to covert a Boolean to Integer in an easy way
>>> - Is it possible to covert an enum to Integer in an easy way
>>
>> Take a look at the 'Pos attribute, and its inverse, 'Val.
>>
>
> Works fine. Thank you!

I don't think this is the best solution.  You don't really want to
"convert to an int"; you want to "interface with C".  The solution I
gave earlier expresses that more cleanly.

-- 
Ludovic Brenta.




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

* Re: Type convertion
  2008-03-11 20:45   ` Ludovic Brenta
@ 2008-03-12 20:36     ` Simon Wright
  2008-03-12 20:45       ` Ludovic Brenta
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2008-03-12 20:36 UTC (permalink / raw)


Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> typedef enum { ab, cd, ef, gh, ij } type_t;

Is sizeof(type_t) here defined to be the same as sizeof(int)? (it was,
in a little experiment).

--S



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

* Re: Type convertion
  2008-03-12 20:36     ` Simon Wright
@ 2008-03-12 20:45       ` Ludovic Brenta
  2008-03-13  8:33         ` Maciej Sobczak
  0 siblings, 1 reply; 12+ messages in thread
From: Ludovic Brenta @ 2008-03-12 20:45 UTC (permalink / raw)


Simon Wright writes:
> Ludovic Brenta writes:
>
>> typedef enum { ab, cd, ef, gh, ij } type_t;
>
> Is sizeof(type_t) here defined to be the same as sizeof(int)? (it was,
> in a little experiment).

Yes.  My C is getting rustier by the day but I seem to remember that
the language standard does require enums to have the same size as int.

-- 
Ludovic Brenta.



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

* Re: Type convertion
  2008-03-12 20:45       ` Ludovic Brenta
@ 2008-03-13  8:33         ` Maciej Sobczak
  2008-03-24  2:14           ` David Thompson
  0 siblings, 1 reply; 12+ messages in thread
From: Maciej Sobczak @ 2008-03-13  8:33 UTC (permalink / raw)


On 12 Mar, 21:45, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:

> Yes.  My C is getting rustier by the day but I seem to remember that
> the language standard does require enums to have the same size as int.

No. The underlying type for enum has to be big enough to fit all
defined values. It can be equivalent to int even for small sets, but
does not have to be - in other words, it does not have to be bigger
than necessary.

And it can be larger that int for enums that don't fit in int.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



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

* Re: Type convertion
  2008-03-13  8:33         ` Maciej Sobczak
@ 2008-03-24  2:14           ` David Thompson
  0 siblings, 0 replies; 12+ messages in thread
From: David Thompson @ 2008-03-24  2:14 UTC (permalink / raw)


On Thu, 13 Mar 2008 01:33:46 -0700 (PDT), Maciej Sobczak
<see.my.homepage@gmail.com> wrote:

> On 12 Mar, 21:45, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
> 
> > Yes.  My C is getting rustier by the day but I seem to remember that
> > the language standard does require enums to have the same size as int.
> 
To be pedantic, it's the 'width' (number of value bits) that matters,
not the size. C allows any integer type's representation to have
padding bits, so two types might have the same memory size but
different ranges, or even different sizes but the same range. Sane
implementors use this freedom only when necessary.

> No. The underlying type for enum has to be big enough to fit all
> defined values. It can be equivalent to int even for small sets, but
> does not have to be - in other words, it does not have to be bigger
> than necessary.
> 
For the enum _type_, right. As a convenient example, gcc has an option
-fshort-enums which does this. (To a first approximation, for all X
gcc has an option for X.) But the enum _constants_ are exactly 'int',
and (thus) must be in the range of int. (Yes, this is ugly.)

> And it can be larger that int for enums that don't fit in int.

Not in C. In _C++_ enum types can have values that exceed int but fit
in long, and the 'implementing' type can thus be anything up to long.
(When C++ adds C99's long long >=64b, I expect this will extend.)
And in C++ enum constants do have the 'correct' (enum) type.

- formerly david.thompson1 || achar(64) || worldnet.att.net



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

end of thread, other threads:[~2008-03-24  2:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-11 16:10 Type convertion news.broadpark.no
2008-03-11 16:59 ` Ed Falis
2008-03-11 20:45   ` Ludovic Brenta
2008-03-12 20:36     ` Simon Wright
2008-03-12 20:45       ` Ludovic Brenta
2008-03-13  8:33         ` Maciej Sobczak
2008-03-24  2:14           ` David Thompson
2008-03-11 17:39 ` Jeffrey R. Carter
2008-03-11 20:53   ` news.broadpark.no
2008-03-11 21:40     ` Ludovic Brenta
2008-03-11 17:49 ` Stuart
2008-03-11 20:56   ` news.broadpark.no

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