2

I am trying to create a simple nim wrapper around the Clever Audio Plugin c library.

In c there is an enum of format flags that can be activated using bitwise operations.

summary of the c code

#  definitions
enum clap_note_dialect {
   CLAP_NOTE_DIALECT_CLAP = 1 << 0,
   CLAP_NOTE_DIALECT_MIDI = 1 << 1,
   CLAP_NOTE_DIALECT_MIDI_MPE = 1 << 2,
   CLAP_NOTE_DIALECT_MIDI2 = 1 << 3,
};

typedef struct clap_note_port_info {
   ...
   uint32_t supported_dialects;   // bitfield, see clap_note_dialect
   ...
} clap_note_port_info_t;

#  implementation
info->supported_dialects =
      CLAP_NOTE_DIALECT_CLAP | CLAP_NOTE_DIALECT_MIDI_MPE | CLAP_NOTE_DIALECT_MIDI2;

using c2nim I get the following nim code:


type
  clap_note_dialect* = enum               
    CLAP_NOTE_DIALECT_CLAP = 1 shl 0,
    CLAP_NOTE_DIALECT_MIDI = 1 shl 1,
    CLAP_NOTE_DIALECT_MIDI_MPE = 1 shl 2,
    CLAP_NOTE_DIALECT_MIDI2 = 1 shl 3
  clap_note_port_info* {.bycopy.} = object
    ...
    supported_dialects*: uint32         ##  bitfield, see clap_note_dialect


# implementation:

info.supported_dialects = CLAP_NOTE_DIALECT_CLAP or CLAP_NOTE_DIALECT_MIDI_MPE or
      CLAP_NOTE_DIALECT_MIDI2

When compiling I get an mismatch error and message that "expression 'CLAP_NOTE_DIALECT_CLAP' is of type: clap_note_dialect"

How can I let nim know that my enum should be uint32 values?

Alex Gustafson
  • 198
  • 1
  • 8

2 Answers2

3

Note that you may also use an enum set in Nim when you have to wrap C enums that are used as ored bits. I did that in the GTK wrapper. You can find an example at the end of the the "Sets" section here: https://ssalewski.de/nimprogramming.html#_sets

But some care is necessary, so for plain and ugly wrappers, or unexperienced people, using distinct ints may be another solution.

Salewski
  • 111
  • 1
  • 2
  • can you explain it by an example? I am trying to use numbers `0,1,2,3` instead of `GtkPositionTypes` for orientation of notebook, so far no success. – Shoaib Mirzaei Sep 27 '22 at 15:21
0

This fix came from user Vindaar on the Nim #main discord channel:

"in order to or the enum values you'll want to wrap them in an ord, so:"

info.supported_dialects = ord(CLAP_NOTE_DIALECT_CLAP) or ord(CLAP_NOTE_DIALECT_MIDI_MPE) or ord(CLAP_NOTE_DIALECT_MIDI2)
Alex Gustafson
  • 198
  • 1
  • 8