C Enum vs. Integer

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

C Enum vs. Integer

Johann Glaser
Hi!

I'm working on a header translation for LibUSB and its fork libusbx.
They define several data types as C enums (for examples, see
http://libusbx.sourceforge.net/api-1.0/group__desc.html ).

They excessively use direct assignment of enum values, and even use one
value multiple times (e.g. LIBUSB_CLASS_PTP = 6, LIBUSB_CLASS_IMAGE =
6).

Can I translate this 1:1, i.e., define an Enum in Pascal and just ignore
the compiler warnings about reused values?

Or should I rather define all these types as Integer and define
constants for all values?

One more question: In some C structs and function parameters and return
values these enums are used too. How can I assure that the same memory
allocation is used?

Thanks
  Hansi


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

Jonas Maebe-2

On 12 Sep 2012, at 19:11, Johann Glaser wrote:

> Can I translate this 1:1, i.e., define an Enum in Pascal and just ignore
> the compiler warnings about reused values?
>
> Or should I rather define all these types as Integer and define
> constants for all values?

It depends on whether you need arithmetic or not. If you do, use integer constants, otherwise you can use an enum.

> One more question: In some C structs and function parameters and return
> values these enums are used too. How can I assure that the same memory
> allocation is used?

Include an appropriate {$packenum xxx} directive in your source code. There is no {$packenum c} (I'm not even sure whether the C standard says anything about the size of enums), so you'll have to check the size of those enumeration types in C yourself.


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

Vinzent Höfler
On Wed, 12 Sep 2012 20:44:21 +0200, Jonas Maebe  
<[hidden email]> wrote:

> Include an appropriate {$packenum xxx} directive in your source code.  
> There is no {$packenum c} (I'm not even sure whether the C standard says  
> anything about the size of enums),

Yes, it does say something about it:

|Each enumerated type shall be compatible with char, a signed integer type,
|or an unsigned integer type. The choice of type is implementation-defined
|but shall be capable of representing the values of all the members of the
|enumeration.
   --  ISO/IEC 9899:1999, 6.7.2.2 (4)

It says it's implementation defined. :D


Vinzent.

--
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents.
     -- Nathaniel Borenstein
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

patspiper
In reply to this post by Johann Glaser
On 12/09/12 20:11, Johann Glaser wrote:

> Hi!
>
> I'm working on a header translation for LibUSB and its fork libusbx.
> They define several data types as C enums (for examples, see
> http://libusbx.sourceforge.net/api-1.0/group__desc.html ).
>
> They excessively use direct assignment of enum values, and even use one
> value multiple times (e.g. LIBUSB_CLASS_PTP = 6, LIBUSB_CLASS_IMAGE =
> 6).
>
> Can I translate this 1:1, i.e., define an Enum in Pascal and just ignore
> the compiler warnings about reused values?
>
> Or should I rather define all these types as Integer and define
> constants for all values?
>
> One more question: In some C structs and function parameters and return
> values these enums are used too. How can I assure that the same memory
> allocation is used?
A translation of libusb 0.1.12 is available in the fpc contributed units
(http://www.freepascal.org/contrib/contribs.html). Maybe you can use
that as a reference.

Stephano
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

Guillermo Martínez Jiménez-2
In reply to this post by Johann Glaser
> On 12 Sep 2012, at 19:11, Johann Glaser wrote:
>
>> Can I translate this 1:1, i.e., define an Enum in Pascal and just ignore
>> the compiler warnings about reused values?
>>
>> Or should I rather define all these types as Integer and define
>> constants for all values?
>
> It depends on whether you need arithmetic or not. If you do, use integer constants, otherwise you can use an enum.

Actually you cannot.  FPC optimizes enumerations in some cases (i.e.
in Delphi mode) but GCC uses 32bit for enumerations alwais.

Anyway you can still use enums, but telling to FPC that you want to
set its size by directives $PACKENUM and $ENUMSIZE.

Guillermo "Ñuño" Martínez
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

Jonas Maebe-2

Guillermo Martínez Jiménez wrote on Thu, 13 Sep 2012:

>> On 12 Sep 2012, at 19:11, Johann Glaser wrote:
>>
>>> Can I translate this 1:1, i.e., define an Enum in Pascal and just ignore
>>> the compiler warnings about reused values?
>>>
>>> Or should I rather define all these types as Integer and define
>>> constants for all values?
>>
>> It depends on whether you need arithmetic or not. If you do, use
>> integer constants, otherwise you can use an enum.
>
> Actually you cannot.  FPC optimizes enumerations in some cases (i.e.
> in Delphi mode) but GCC uses 32bit for enumerations alwais.

The size of an enum in gcc depends on whether or not the -fshort-enums
command line option is used, and on the number of bits required to
represent its upper bound value.

> Anyway you can still use enums, but telling to FPC that you want to
> set its size by directives $PACKENUM and $ENUMSIZE.

It's $PACKENUM *or* $ENUMSIZE (they affect the same compiler setting)

Anwyay, yes, that's also what I said the part of my original mail that
you cut away.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

Johann Glaser
Hi!

Am Donnerstag, den 13.09.2012, 15:45 +0200 schrieb Jonas Maebe:

> Guillermo Martínez Jiménez wrote on Thu, 13 Sep 2012:
>
> >> On 12 Sep 2012, at 19:11, Johann Glaser wrote:
> >>
> >>> Can I translate this 1:1, i.e., define an Enum in Pascal and just ignore
> >>> the compiler warnings about reused values?
> >>>
> >>> Or should I rather define all these types as Integer and define
> >>> constants for all values?
> >>
> >> It depends on whether you need arithmetic or not. If you do, use  
> >> integer constants, otherwise you can use an enum.
> >
> > Actually you cannot.  FPC optimizes enumerations in some cases (i.e.
> > in Delphi mode) but GCC uses 32bit for enumerations alwais.
>
> The size of an enum in gcc depends on whether or not the -fshort-enums  
> command line option is used, and on the number of bits required to  
> represent its upper bound value.
>
> > Anyway you can still use enums, but telling to FPC that you want to
> > set its size by directives $PACKENUM and $ENUMSIZE.
>
> It's $PACKENUM *or* $ENUMSIZE (they affect the same compiler setting)
>
> Anwyay, yes, that's also what I said the part of my original mail that  
> you cut away.

Thank you all for your valuable information!

I found that libusb doesn't use the enum types anywhere but use uint8_t
or int instead. So I use constants for everything now.

Only one enum is used (libusb_transfer_status) in two structs, so I
replaced it as it was an int.

@Stephano: Thanks for the link with contributed units. Actually, one of
the USB units is from me, but a bit outdated. When my libusb 1.0
translation (including an OOP wrapper) is done I'll update
http://wiki.freepascal.org/Hardware_Access#libusb and the contributed
units page again.

Thanks
  Hansi


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: C Enum vs. Integer

patspiper
On 13/09/12 22:39, Johann Glaser wrote:
> @Stephano: Thanks for the link with contributed units. Actually, one
> of the USB units is from me, but a bit outdated. When my libusb 1.0
> translation (including an OOP wrapper) is done I'll update
> http://wiki.freepascal.org/Hardware_Access#libusb and the contributed
> units page again.

Pls do as it is a very useful translation. an OOP wrapper would be even
better. Can you email me the updated unit (which is outdated)?

Keep up the good work!

Stephano
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal