comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Type convertion
Date: Tue, 11 Mar 2008 21:45:03 +0100
Date: 2008-03-11T21:45:03+01:00	[thread overview]
Message-ID: <878x0pt2gg.fsf@ludovic-brenta.org> (raw)
In-Reply-To: op.t7u2txlz5afhvo@naropa

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.



  reply	other threads:[~2008-03-11 20:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-11 16:10 Type convertion news.broadpark.no
2008-03-11 16:59 ` Ed Falis
2008-03-11 20:45   ` Ludovic Brenta [this message]
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
replies disabled

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