将C风格的回调设置功能绑定到D.

时间:2014-04-01 18:39:48

标签: c callback d glfw

我目前正处于绑定GLFW3库的过程中(由于各种原因我不使用Derelict)。除了以下内容之外,这很容易做到并且没有任何实际问题:

extern (C) {
    alias void(*GLFWwindowposfun)(GLFWwindow*, int, int);
    alias void(*GLFWwindowsizefun)(GLFWwindow*, int, int);
    alias void(*GLFWwindowclosefun)(GLFWwindow*);
    alias void(*GLFWwindowrefreshfun)(GLFWwindow*);
    alias void(*GLFWwindowfocusfun)(GLFWwindow*, int);
    alias void(*GLFWwindowiconifyfun)(GLFWwindow*, int);
    alias void(*GLFWframebuffersizefun)(GLFWwindow*, int, int);
}

以下是如何定义原始声明之一(在GLFW / GLFW3.h中):

typedef void (* GLFWwindowposfun)(GLFWwindow*,int,int);

这个的工作在编译的意义上已经足够了,但它会触发以下警告:

Source/MageLib/GLFW3.di(9): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers
Source/MageLib/GLFW3.di(10): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers
Source/MageLib/GLFW3.di(11): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers
Source/MageLib/GLFW3.di(12): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers
Source/MageLib/GLFW3.di(13): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers
Source/MageLib/GLFW3.di(14): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers
Source/MageLib/GLFW3.di(15): Deprecation: C-style function pointer and pointer to array syntax is deprecated. Use 'function' to declare function pointers

Googling的一些资源为我提供了以下资源:http://dlang.org/function.html#closures

我不知道如何应用这个,我已经尝试过这样做,但显然不起作用:

alias void function(GLFWwindow*, int, int);

这会产生以下错误:

Source/MageLib/GLFW3.di(9): Error: no identifier for declarator extern (C) void function(GLFWwindow*, int, int)

如何以正确的方式正确转换?

1 个答案:

答案 0 :(得分:3)

alias GLFWwindowposfun = void function(GLFWwindow *,int,int);