[fpc-devel] C flags, enums, sets, and the "Pascal way"
Alcom
alcombiz at gmail.com
Sun Apr 3 15:04:08 CEST 2016
Yes, h2pas essentially generates my second example.
My question has more to do with using predefined enum constants as bitmasks
in sets, instead of the default behaviour (which is to use the enum
constant as the bit position instead of a bitmask).
My previous suggestion {$EnumConstantsUseLiteral} makes no sense as a
compiler mode flag; a better choice would be something like
{$UseEnumConstantsAsBitmasksInSets}, or something even longer if possible.
: -)
On Sat, Apr 2, 2016 at 2:06 AM, thaddy at thaddy.com <thaddy at thaddy.com> wrote:
> Well,
>
> h2pas gives you:
> {$packrecords c}
>
> type
> VkFlags = uint32_t;
>
> VkSparseImageFormatFlagBits =
> (VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT := $00000001,
> VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT := $00000002,
> VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT := $00000004
> );
>
> VkSparseImageFormatFlags = VkFlags;
>
>
>
> On 01-Apr-16 10:01 PM, Alcom wrote:
>
>
> I'm working on some translated C headers for Vulkan for use in FPC.
>
> In the vulkan.h header file, we have the following:
>
> typedef uint32_t VkFlags;
> typedef enum VkSparseImageFormatFlagBits {
> VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = 0x00000001,
> VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = 0x00000002,
> VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = 0x00000004,
> } VkSparseImageFormatFlagBits;
> typedef VkFlags VkSparseImageFormatFlags;
>
> For code maintenance purposes, it is desirable to have a header
> translation that is as close as possible to the source. A reasonable
> translation to Pascal of this might be:
>
> TVkFlags = Cardinal;
> TVkSparseImageFormatFlagBits =
> (
> VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001,
> VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002,
> VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004
> );
> TVkSparseImageFormatFlags = TVkFlags;
>
>
> However, this solution has only the *illusion* of being strongly typed --
> nothing about which the compiler is aware links TVkImageCreateFlags with
> the TVkImageCreateFlagBits.
>
> A seemingly better solution (albeit incorrect) would be the following:
>
> {$packset 4}
> TVkSparseImageFormatFlagBits =
> (
> VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT = $00000001,
> VK_SPARSE_IMAGE_FORMAT_ALIGNED_MIP_SIZE_BIT = $00000002,
> VK_SPARSE_IMAGE_FORMAT_NONSTANDARD_BLOCK_SIZE_BIT = $00000004
> );
> TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits;
>
> This will not give the expected results, because FPC interprets the enum
> constant as an exponent (or bit position, if you prefer).
>
>
>
> My question is this: Is there currently a modifier for the set declaration
> that instructs the compiler to accept the enum values as literal constants,
> instead of interpreting them as exponents?
>
> e.g. something like this:
> {$EnumConstantsUseLiteral}
> --or--
> TVkSparseImageFormatFlags = literalset of TVkSparseImageFormatFlagBits;
> --or--
> TVkSparseImageFormatFlags = set of TVkSparseImageFormatFlagBits;
> literal;
> --or--
> TVkSparseImageFormatFlags = set of literal
> TVkSparseImageFormatFlagBits;
>
>
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.orghttp://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
>
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160403/1cbff5f0/attachment.html>
More information about the fpc-devel
mailing list