CTFE Overhaul (Was: RFC: Thrift project proposal (draft))

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Mar 29 09:44:04 PDT 2011


On 3/27/11, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> I remember a few months ago I've tried using CTFE and import
> expressions to load a .def file and generate at compile-time a runtime
> DLL loading mechanism in a class which would load a DLL file and
> create wrapper functions for DLL functions.

Found it. It doesn't actually load a .def file, and it wouldn't make
much sense since a def file doesn't have much except a list of symbol
names. It generates code that links function pointers to a DLL at
runtime, and creates wrapper functions which take care of calling the
C code.

First I'd create a struct with a list of function prototypes. Then I'd
just mixin() a string inside a class. The function that creates the
string to be mixed in first checks the return values of the function
prototypes, and based on that it can add code that throws on invalid
values.

Here's an example of a generated class at compile-time:
https://gist.github.com/892698
or if that doesn't display right: http://dl.dropbox.com/u/9218759/result.d

Of course much more could be done here. The generated functions could
take strings instead of char pointers and call toStringz on them when
calling a function pointer. And we could use ref instead of pointers
for other parameters.


More information about the Digitalmars-d mailing list