使用 PltScheme FFI 中的 _bitmask

发布于 2024-08-14 22:24:18 字数 368 浏览 2 评论 0原文

这是 plt-scheme 包装库的一部分:

(define InputMask

  (_bitmask '(NoEventMask =            #x00000000

            KeyPressMask =             #x00000001

            KeyReleaseMask =           #x00000002

            ...

            OwnerGrabButtonMask =      #x01000000)

      _long))

问题是我无法弄清楚如何访问位掩码(或枚举)中的字段。例如,如何获取 KeyPressMask 值?

This is a part of a plt-scheme wrapper library:

(define InputMask

  (_bitmask '(NoEventMask =            #x00000000

            KeyPressMask =             #x00000001

            KeyReleaseMask =           #x00000002

            ...

            OwnerGrabButtonMask =      #x01000000)

      _long))

The thing is I cant figure out how to access fields in a bitmask (or enum for that matter). How can I get KeyPressMask value for example?

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

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

发布评论

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

评论(1

最后的乘客 2024-08-21 22:24:18

你不知道。添加 ctypes 很容易:要创建一个新的 ctype,您需要提供一个现有的 ctype 来构建,以及两个函数——一个将任何内容转换为现有类型,另一个将另一种方式转换。

现在, _bitmask 类型就是这样做的 - 它构建在 _int 上(但在您的情况下,它是在 _long 上),并且两个翻译函数将符号列表转换为整数,并将整数转换为符号列表。一旦使用了这样的类型,您就不需要知道 KeyPressMask 的值 - 您只需知道可以将 '(KeyPressMask) 作为 传递InputMask 输入到外部函数,并将其转换为适当的数字;并且您还知道,当您从具有 InputMask 输出的函数获取结果值时,它将是可能包含 KeyPressMask 的符号列表。最重要的是,在方案方面,您不处理数字——只处理符号列表。

如果您确实由于某些不明原因需要访问这些值,那么您可以通过其他方式构建自己的ctype - 使用make-ctype(正如我上面概述的那样)应该很容易。

You don't. Adding ctypes is easy: to make a new ctype, you need to provide an existing ctype to build on, and two functions -- one to translate whatever to the existing type, and one to translate the other way.

Now, the _bitmask type does just that -- it builds on _int (but in your case, it's on _long), and the two translation functions translate a list of symbols to an integer, and an integer to a list of symbols. Once such a type is used, you don't need to know the value of KeyPressMask -- you just know that you can pass '(KeyPressMask) as an InputMask input to the foreign function, and that will be translated to the appropriate number; and you also know that when you get the result value from a function that has an InputMask output, then it will be a list of symbols that might contain KeyPressMask. The bottom line is that on the Scheme side you don't deal with numbers -- only with symbol lists.

If you do need to access these values for some obscure reason, then you can build your own ctype in some other way -- using make-ctype (as I outlined above) should be very easy.

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