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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,74b958f114ec4924 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feed.tweaknews.nl!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Type convertion References: <47d6ae9b$1@news.broadpark.no> Date: Tue, 11 Mar 2008 21:45:03 +0100 Message-ID: <878x0pt2gg.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:EHxrSbT3rTVNN1DGvyQnKxunE8o= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tele2 X-Trace: DXC=RfC2^FaCRB:M_SUSjh0Wk=6`Y6aWje^Y:[fF7l3eiTE Ed Falis writes: > On Tue, 11 Mar 2008 12:10:35 -0400, news.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: typedef enum { ab, cd, ef, gh, ij } type_t; void foo (type_t param); 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; If, however, the C part doesn't have an enum but something like: #define AB 2 #define CD 5 #define EF 8 #define GH 9 #define IJ 42 void foo (int param); then you can write: 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; HTH -- Ludovic Brenta.