Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-09

C Enum vs. Integer

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Jonas Maebe
to Johann Glaser
Johann Glaser wrote 5 lines
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

Vinzent Höfler
to Jonas Maebe
Jonas Maebe 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
patspiper
to Johann Glaser: Hi! I'm working on a header translation for LibUSB and its fork libusbx. They define…
Johann Glaser wrote 19 lines
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

Guillermo Martínez Jiménez
Show quoted text 9 lines
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

Jonas Maebe
to Guillermo Martínez Jiménez

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

Show quoted text 13 lines
> 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

Johann Glaser
to Jonas Maebe

Hi!

Jonas Maebe wrote 27 lines
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

patspiper
to Johann Glaser
Johann Glaser wrote 5 lines
@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