D 中具有可变数量字符串参数的 Mixin?
我正在为现有的 C 库开发一些 D 绑定,并且我有一堆函数定义以及它们的一堆绑定。例如:
// Functions
void function(int) funcA;
long function() funcB;
bool function(bool) funcC;
char function(string) funcD;
// etc...
// Bindings
if(!presentInLibrary("func")) return false;
if(!bindFunction(funcA, "funcA")) return false;
if(!bindFunction(funcB, "funcB")) return false;
if(!bindFunction(funcC, "funcC")) return false;
if(!bindFunction(funcD, "funcD")) return false;
// etc...
该模型与 Derelict 处理 OpenGL 扩展加载的方式非常相似。然而,这似乎有很多多余的打字。我真的很想要一种方法来表达上面的“绑定”部分,例如:
BINDGROUP("func", "funcA", "funcB", "funcC", "funcD", ...); // Name of function group, then variable list of function names.
这是可以用 mixins 来完成的事情吗?
I'm working on some D bindings for an existing C library, and I have a bunch of function definitions, and a bunch of bindings for them. For example:
// Functions
void function(int) funcA;
long function() funcB;
bool function(bool) funcC;
char function(string) funcD;
// etc...
// Bindings
if(!presentInLibrary("func")) return false;
if(!bindFunction(funcA, "funcA")) return false;
if(!bindFunction(funcB, "funcB")) return false;
if(!bindFunction(funcC, "funcC")) return false;
if(!bindFunction(funcD, "funcD")) return false;
// etc...
This model is very similar to how Derelict handles OpenGL extension loading. However, this seems like a lot of redundant typing. I'd really like a way to express the "binding" portion above as something like:
BINDGROUP("func", "funcA", "funcB", "funcC", "funcD", ...); // Name of function group, then variable list of function names.
Is this something that can be done with mixins?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在进行动态加载时使用了这个,虽然它不能回答你的问题,但你可以调整它:
这里
bindFunctions("a", "b", "c")
返回一个字符串看起来像:其中
lib.getSymbol()
从dl_open()
返回指针等。希望这会有所帮助。I used this when I was doing dynamic loading, while it doesn't answer your question you may be able to adapt it:
Here
bindFunctions("a", "b", "c")
returns a string that looks something like:Where
lib.getSymbol()
returns a pointer fromdl_open()
etc. Hope this helps.我想你的意思是字符串混合?您可以直接使用 D 的 vararg 语法:
I assume you meant string mixins? You can just make straight-forward use of D's vararg syntax:
我相信这就是您正在寻找的
我在这里使用递归模板声明,您也可以使用 foreach 循环来执行此操作
I believe this is what you're looking for
I'm using recursive template declaration here, you could also do this with foreach loops