Compile time mapping

Bastiaan Veelo Bastiaan at Veelo.net
Sun May 12 18:54:44 UTC 2019


On Sunday, 12 May 2019 at 18:47:20 UTC, Bogdan wrote:
> On Sunday, 12 May 2019 at 17:53:56 UTC, Bastiaan Veelo wrote:
>> If I understand your question correctly, you have two enums of 
>> equal length, and you want to convert members across enums 
>> according to their position, right?
>
> My question was very vague, sorry about that.
>
> In my use case I'd like to map SDL2 keyboard scan codes to my 
> own game input keyboard codes. The two enums would look 
> something like this:
>
> ```
> enum SDL_Scancode
> {
>     SDL_SCANCODE_UNKNOWN = 0,
>     SDL_SCANCODE_A = 4,
>     SDL_SCANCODE_B = 5,
>     SDL_SCANCODE_C = 6,
>     SDL_SCANCODE_D = 7,
> }
>
> enum MY_Scancode
> {
>   KEY_A,
>   KEY_B,
>   KEY_C,
>   KEY_D,
> }
> ```
>
> The two enums are not of equal length, so in the end I just 
> decided to create an immutable array of type My_Scancode[] 
> where the index is an SDL_Scancode and the value is the 
> corresponding MY_Scancode enum member. I'm ok with using some 
> memory for this, as long as it's as fast as possible.

If the only difference is the extra _UNKNOWN member, you can 
still use the static foreach approach. Just make it a 
non-template function and rip out the checks, and add a +1 in the 
right place.


More information about the Digitalmars-d-learn mailing list