NIM中的枚举C库包装纸

发布于 2025-02-09 13:19:47 字数 1323 浏览 3 评论 0 原文

我正在尝试在聪明的音频插件C库周围创建一个简单的NIM包装器。

在C中,有一个格式标志的枚举,可以使用位操作激活。

使用C2NIM的C代码摘要

#  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;

我将获得以下NIM代码:


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

编译时,我会收到一个不匹配错误和消息,即“ clap_note_dialect_clap'expression'clap_note_dialect'表达式“ clap_note_dialect_clap”是:clap_note_dialect“

我如何让nim知道我的枚举应该是uint32值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2025-02-16 13:19:47

请注意,当您必须包装用作ORED位的C枚举时,您也可以使用NIM中的枚举集。我在GTK包装纸中做到了。您可以在此处的“集合”部分的末尾找到一个示例:

但是需要进行一些注意,因此对于普通和丑陋的包装纸或不经验的人来说,使用独特的INT可能是另一个解决方案。

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.

笑咖 2025-02-16 13:19:47

此修复程序来自NIM #Main Discord频道上的用户Vindaar:

“为了或要将其包装在ORD中的枚举值,所以:”

info.supported_dialects = ord(CLAP_NOTE_DIALECT_CLAP) or ord(CLAP_NOTE_DIALECT_MIDI_MPE) or ord(CLAP_NOTE_DIALECT_MIDI2)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文