From: Simon Wright <simon@pushface.org>
Subject: Re: 'Size hack for enumerated types
Date: Sun, 06 Jul 2014 21:59:28 +0100
Date: 2014-07-06T21:59:28+01:00 [thread overview]
Message-ID: <ly38eefbkf.fsf@pushface.org> (raw)
In-Reply-To: lp9tj0$6ii$1@speranza.aioe.org
Victor Porton <porton@narod.ru> 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 (<>);
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;
and, in increment.c,
#include <stdio.h>
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
next prev parent reply other threads:[~2014-07-06 20:59 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-05 20:32 'Size hack for enumerated types Victor Porton
2014-07-05 21:47 ` Simon Wright
2014-07-05 22:11 ` Victor Porton
2014-07-05 22:18 ` Victor Porton
2014-07-05 22:23 ` Victor Porton
2014-07-06 16:25 ` Victor Porton
2014-07-06 20:59 ` Simon Wright [this message]
2014-07-06 23:01 ` Victor Porton
2014-07-06 23:30 ` Jeffrey Carter
2014-07-07 16:00 ` Victor Porton
2014-07-07 17:12 ` Simon Wright
2014-07-07 20:23 ` Victor Porton
2014-07-08 7:04 ` Simon Wright
2014-07-08 10:17 ` sbelmont700
2014-07-08 14:53 ` Dan'l Miller
2014-07-08 20:56 ` Randy Brukardt
2014-07-08 22:26 ` Dan'l Miller
2014-07-08 23:18 ` Jeffrey Carter
2014-07-08 9:43 ` AdaMagica
2014-07-08 13:52 ` Victor Porton
2014-07-08 15:02 ` Simon Wright
2014-07-07 7:45 ` Simon Wright
2014-07-06 7:22 ` Simon Wright
2014-07-06 13:21 ` sbelmont700
2014-07-06 16:16 ` Victor Porton
2014-07-06 17:52 ` sbelmont700
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox