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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: Victor Porton Newsgroups: comp.lang.ada Subject: Re: 'Size hack for enumerated types Date: Mon, 07 Jul 2014 02:01:40 +0300 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: BISA/P8IHJbAM3ms471zeQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Complaints-To: abuse@aioe.org User-Agent: KNode/4.12.4 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:20755 Date: 2014-07-07T02:01:40+03:00 List-Id: Simon Wright wrote: > Victor Porton writes: > >> -- rdf-auxilary.ads >> with Interfaces.C; >> >> package RDF.Auxilary is >> >> generic >> type Enum_Type is range <>; >> package Convert_Enum is >> function To_C(Argument: Enum_Type) return Interfaces.C.int; >> function From_C(Argument: Interfaces.C.int) return Enum_Type; >> end; >> >> end RDF.Auxilary; > > If you want Enum_Type to be an enumeration, you should use a formal > discrete type definition[1]: > > type Enum_Type is (<>); The same error with this. BTW, remind me what is the difference of: - type Enum_Type is range <>; - type Enum_Type is (<>); ? > But I don't see what you're trying to achieve that can't easily be done > using simple language facilities: > > with Ada.Text_IO; use Ada.Text_IO; > procedure Enums is > type Flag_Type is (Libxml_Error_Save, > Libxml_Structured_Error_Save, > URI_Interning, > WWW_Skip_Init_Finish) with Convention => C; > for Flag_Type use (Libxml_Error_Save => 1, > Libxml_Structured_Error_Save => 2, > URI_Interning => 3, > WWW_Skip_Init_Finish => 4); > function Increment (F : Flag_Type) return Flag_Type > with Import, Convention => C, External_Name => "increment"; > begin > Put_Line ("incremented " > & Libxml_Structured_Error_Save'Img > & " is " > & Increment (Libxml_Structured_Error_Save)'Img); > end Enums; I see no reason for Increment function to work well with Convention => C, it seems that you are lucky with your compiler. After all Flag_Type and enum flag_type may be of different size! > and, in increment.c, > > #include > > typedef enum {LIBXML_ERROR_SAVE = 1, > LIBXML_STRUCTURED_ERROR_SAVE, > URI_INTERNING, > WWW_SKIP_INIT_FINISH} flag_type; > > flag_type increment(flag_type f) { > flag_type result = f; > ++result; > printf("increment received %d, returning %d\n", f, result); > return result; > } > > with > > $ gcc -c increment.c > $ gnatmake enums.adb -largs increment.o > $ ./enums > increment received 2, returning 3 > incremented LIBXML_STRUCTURED_ERROR_SAVE is URI_INTERNING > > [1] http://www.ada-auth.org/standards/12rm/html/RM-12-5-2.html#p2 -- Victor Porton - http://portonvictor.org