Proof of Concept: Binding to and extending C++ objects via a metric fuckton of templates
Don
nospam at nospam.com
Thu Dec 10 01:59:37 PST 2009
downs wrote:
> I've just committed a module to tools (tools.cpp, http://dsource.org/projects/scrapple/Trunk/tools/tools/cpp.d ) that should enable linking and using any C++ library with D programs without creating a C wrapper for it.
>
> The code has been tested with a simple multiple-inheritance sample: http://paste.dprogramming.com/dpv8hpjp
>
> The D code to bind to this is http://paste.dprogramming.com/dphlv1ot . It requires a symlink to the tools folder in its directory.
>
> Caveats:
>
> - D1
> - Only works on g++4's "newapi" layout and mangling (but should be reasonably portable).
At http://www.agner.org/optimize/, you can find a document
(calling_conventions.pdf) which gives full details of the C++ name
mangling schemes for various compilers.
> Writing this also gave us what I believe to be a fair contender to Most Horrifying Statement of D Ever Written:
>
> fndefs ~= `mixin("extern(C) `~t~` "~`~mname~`~"`~fnparams~`; ");`~'\n'~
> `mixin("`~fn~`("~refToParamList("Params!(typeof(&"~`~mname~`~"))", isRef!(typeof(mixin("&"~`~mname~`))))~") {
> return "~`~mname~`~"("~refToValueList(isRef!(typeof(mixin("&"~`~mname~`))))~");
> }
> "); `;
Ugh, that's unreadable.
I found that with a little function called string enquote(string s),
which changes s into "s", this kind of stuff looks much less terrible.
In particular you get rid of the nasty mix of ` and ".
fndefs ~= "mixin(" ~ enquote("extern(C)" ~ t ~ " ")
~ "~" ~ mname
~ "~" ~ enquote(fnparams ~ ";")
~ ");"
~ "mixin(" ~ ....
// etc
More information about the Digitalmars-d
mailing list