CTFE and -betterC
Xavier Bigand
flamaros.xavier at gmail.com
Tue Mar 13 23:00:19 UTC 2018
As I am trying to do a dll that acts exactly like one written in C, I am trying to compile my code with the -betterC option. So I would not need the DllMain function.
I am not sure that I use the best syntax for my CTFE function to be able to make it works with the option -betterC and to maintain it after.
In particular I have following issues (my code is at the end of message) :
* startsWith function doesn't compile with -betterC
* can't put static before the first foreach
* don't really now how to factorize small expressions (oglFunctionName , oglFunctionName,...)
* how to make the code I generate less polluted by conditions and iterations? Certainly by naming some previously computed parts with alias?
* after that how to see the generated result and debug?
Thank you in advance for any help.
module api_entry;
import std.stdio : writeln;
import std.algorithm.searching;
import missing_ogl;
import std.traits;
import std.meta;
static string implementFunctionsOf(string Module, bool removeARB = false)()
{
import std.traits;
import std.regex;
import std.conv;
mixin("import " ~ Module ~ ";");
string res;
res ~= "extern (C) {\n";
foreach (name; __traits(allMembers, mixin(Module)))
{
static if (name.startsWith("da_")
&& mixin("isCallable!" ~ name))
{
alias derelict_oglFunctionName = Alias!(name[3..$]);
alias oglFunctionName = derelict_oglFunctionName;
alias returnType = Alias!(ReturnType!(mixin(name)).stringof);
alias parametersType = Alias!(Parameters!(mixin(name)).stringof);
static if (removeARB && name.endsWith("ARB"))
oglFunctionName = oglFunctionName[0..$ - 3];
res ~=
"export\n" ~
returnType ~ "\n" ~
oglFunctionName ~
parametersType ~ "\n" ~
"{\n" ~
" writeln(\"" ~ oglFunctionName ~ " is not specialized\");\n";
// Forward the call to the driver (with arguments and return the
// value of the forward directly)
res ~= "import " ~ Module ~ ";";
// For a reason I do not understand the compiler can not
// compile with returnType
static if (ReturnType!(mixin(name)).stringof == "int function()")
res ~=
" alias extern (C) " ~ returnType ~ " returnType;\n" ~
" return cast(returnType) ";
else if (returnType != "void")
res ~=
" return ";
res ~=
" " ~ Module ~ "." ~ derelict_oglFunctionName ~ "(";
foreach (i, parameter; Parameters!(mixin(name)))
{
if (i > 0)
res ~= ", ";
// We use the default parameter name variable "_param_x" where x
// is the index of the parameter starting from 0
res ~= "_param_" ~ to!string(i);
}
res ~=
");";
res ~=
"}\n";
}
}
res ~=
"}\n";
return res;
}
mixin(implementFunctionsOf!("derelict.opengl3.functions"));
mixin(implementFunctionsOf!("derelict.opengl3.deprecatedFunctions"));
More information about the Digitalmars-d-learn
mailing list