You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Even though Nim does have enum types, these are not always the best choicewhen passing values on to a native C API. Also, bitflags are regurlarly usedin C APIs, but in Nim we would rather use theset type for that purpose.
This nimble package provides helpers to bridge enums, semi-enums and bitflagsfrom C to Nim.
Enums as distinct Types
In many C APIs, especially those that are tied to the operating system, a groupof constants is not implemented as an enum type, but rather as a series of#define constants. Often this is done, to provide a clear definition of thebit length for a parameter, since the bit-length of an enum type in C can beambigious depending on the compiler, target platform and other factors.
Unfortunenately, this habit makes it harder to translate C headers to equivalentNim bindings.
TheimplementDistictEnum macro can be placed in front of a block that declaresvalues for a distinct type, and will automatically implement the equalityoperator, and if desired, stringify and parse procs.
If we have a C API that returns aCOLOUR value, and we wanted to do somethingdepending on the value returned, we would now have to implement the==operator for the distinctColour type in Nim. Sure, the implementation istrivial, but it still has to be done, if we do not want to cast everyColourvalue to anint32 everytime we want to do something useful with it.
For that purpose, this package provides theimplementDistinctEnum macro.
Note that theconst block is now nested inside theimplementDistinctEnummacro invocation. The macro peeks into the const block and finds all identifierdefinitions in the block. The identifiers that are found are used by the macroas thelist of known values for theColour type. Then the macro generatesthe simple trivial implementations for these procs:
Note that it uses thelist of known values for theColour type to implementthe stringify and parse procs. The parse procs use case-insensitiveunicode comparison with the identifiers of the known values.
Bitflags as distinct types
The Nim Manual states thatset types should be used when implementing flagtypes. However, theset type is not compatible with bitflags that areregurlarly used in C, andset types in Nim only support bitlength of up to16-bits.
With theimplementDistinctFlags macro, you can easily define a bitflags typeas a distinct numeric type, and the macro will provide all the procs you wouldexpect from a regular Nimset type.
The==,contains,+,*,-,<=,<,incl,excl procs providetheBitflags8 with all the procs that you would get ifBitflags8 was asettype.
Stringify for Bitflags returns a comma-separated list of all set flags in thespecified value, if there are bits set that have no known value associated withthem, the remainder is shown as its decimal representation.
For both theimplementDistinctEnum and theimplementDistinctFlags macros,there are overloads that accept a static bool argument, to indicate whether themacro should generate the stringify and parse procs. Since these methods carrythe string literals in the resulting code and thus also into the output binary,generating these string procs can significantly increase the output binary size.For example, the Windows SDK has several thousand lines of code where it definesHRESULT values that represent various error conditions. Producing a binarywith the names of all these constants inside it can easily increase the binarysize with approximately 7 MiB.
The AST block containing the known values for the distinct type that is passedin as the last argument for theimplementDistinctEnum and theimplementDistinctFlags macros can contain multipleconst sections. However,ALL identifiers declared in that blockMUST have the same type as theone specified for the macro.
Passinglet orvar sections instead ofconst sections for the known valuesto theimplementDistinctEnum and theimplementDistinctFlags macros ispossible, but not necessarily recommended.
About
Nimble package for supporting and simplifying import of symbols from C into Nim