This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.d
executable file
·60 lines (52 loc) · 1.73 KB
/
main.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
static import portaudio;
import std.meta;
import std.stdio;
import std.traits;
import core.sys.windows.windows;
struct Tuple(_FuncType, string _Name) {
alias FuncType = _FuncType;
enum Name = _Name;
}
/* Get the function pointer type of an actual function */
template FuncType(alias symbol) {
ReturnType!symbol function(Parameters!symbol) func;
alias FuncType = SetFunctionAttributes!(typeof(func), functionLinkage!symbol,
functionAttributes!(typeof(func)));
}
/* Get a sequence of (Function type, Name) belonging to the provided module */
template GetFunctionList(alias Module) {
alias GetFunctionList = AliasSeq!();
static foreach (idx, member; __traits(allMembers, Module)) {
static if (isFunction!(__traits(getMember, Module, member))) {
GetFunctionList = AliasSeq!(GetFunctionList,
Tuple!(FuncType!(__traits(getMember, Module, member)), member));
}
}
}
/* Generate dynamic bindings for all functions in Module and load SharedLib */
class Dynamic(alias Module, string SharedLib)
{
/* Load the shared library */
static HANDLE dll;
static this() {
dll = LoadLibraryA(SharedLib);
!dll && assert(0);
}
/* Declare the function pointers */
static foreach (Tup; GetFunctionList!Module) {
mixin("Tup.FuncType " ~ Tup.Name ~ ";");
}
/* Load the function pointers */
this()
{
static foreach (Tup; GetFunctionList!Module) {
*(cast(void**)&__traits(getMember, this, Tup.Name))
= cast(void*)GetProcAddress(dll, Tup.Name);
}
}
}
void main() {
// easy!
auto dynamic = new Dynamic!(portaudio, "portaudio_x64.dll");
printf("Version info %s\n", dynamic.Pa_GetVersionText());
}