On Saturday, 23 January 2016 at 19:42:29 UTC, Johan Engelen wrote:
Hi all,
While trying to interface C++ and D, I have to new a few D
objects in C++ code. I am doing this using a D function: "XXX
createXXX(...) { return new XXX(...); }".
I am sure there must be some great way to automatically
generate these creator functions, but I don't know how to do it.
In the C++-header I will write manually:
XXX* createXXX(int a, int b);
XXX* createXXX(bool flag);
In D source:
extern (C++) class XXX {
this(int a, int b) { /+...+/ }
this(bool flag) { /+...+/ }
}
// Somehow define these guys automatically,
"genCreateCtors!(XXX)" ?
XXX createXXX(int a, int b) { return new XXX(a, b); }
XXX createXXX(bool flag) { return new XXX(flag); }
Thanks a lot!
Johan
Wow! There are lots of XXX there.
Anyway, I did a similar thing to yours for automatic attribute
definition before. Three things:
1. Template
2. Mixin
3. Compile time function
You define a compile time function which generates a string that
is valid D code.
You define template that takes some parameters (Your XXX values),
and calls the function to merge them.
In your class, you use mixin and template to generate the string
and inject the generated code.
Not that complex once you do it.
Try to understand this code.
http://david.rothlis.net/d/templates/
Its in there.