From 68d971d4c2d7dbfcda4eedaa75e59cc78e2b60f8 Mon Sep 17 00:00:00 2001 From: dushibaiyu Date: Mon, 26 Sep 2016 16:29:43 +0800 Subject: [PATCH 1/3] add bindFunc template --- source/derelict/util/loader.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/derelict/util/loader.d b/source/derelict/util/loader.d index e3bf854..73ba2b6 100644 --- a/source/derelict/util/loader.d +++ b/source/derelict/util/loader.d @@ -342,9 +342,16 @@ abstract class SharedLibLoader { Throws: SymbolLoadException if doThrow is true and a the symbol specified by funcName is missing from the shared library. +/ - final void bindFunc(void** ptr, string funcName, bool doThrow = true) { + final void bindFunc(void** ptr, string funcName, bool doThrow = true) { void* func = loadSymbol(funcName, doThrow); *ptr = func; } + + import std.traits; + final void bindFunc(TFUN)(ref TFUN fun, string funcName, bool doThrow = true) if(isFunctionPointer!(TFUN)) + { + void* func = loadSymbol(funcName, doThrow); + fun = cast(TFUN)func; + } } } \ No newline at end of file From 5012287d0099597dd69a7cc8823dc116e6e7ae5d Mon Sep 17 00:00:00 2001 From: dushibaiyu Date: Mon, 26 Sep 2016 16:35:55 +0800 Subject: [PATCH 2/3] fix codestyle and add docs --- source/derelict/util/loader.d | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/source/derelict/util/loader.d b/source/derelict/util/loader.d index 73ba2b6..c62d955 100644 --- a/source/derelict/util/loader.d +++ b/source/derelict/util/loader.d @@ -342,16 +342,30 @@ abstract class SharedLibLoader { Throws: SymbolLoadException if doThrow is true and a the symbol specified by funcName is missing from the shared library. +/ - final void bindFunc(void** ptr, string funcName, bool doThrow = true) { + final void bindFunc(void** ptr, string funcName, bool doThrow = true) { void* func = loadSymbol(funcName, doThrow); *ptr = func; } - import std.traits; - final void bindFunc(TFUN)(ref TFUN fun, string funcName, bool doThrow = true) if(isFunctionPointer!(TFUN)) - { - void* func = loadSymbol(funcName, doThrow); - fun = cast(TFUN)func; - } + /++ + Subclasses can use this to bind a function pointer to a symbol in the + shared library. + + Params: + fun = function pointer that will be used as the bind + point. + funcName = The name of the symbol to be bound. + doThrow = If true, a SymbolLoadException will be thrown if the symbol + is missing. If false, no exception will be thrown and the + ptr parameter will be set to null. + Throws: SymbolLoadException if doThrow is true and a the symbol + specified by funcName is missing from the shared library. + +/ + import std.traits; + final void bindFunc(TFUN)(ref TFUN fun, string funcName, bool doThrow = true) + if(isFunctionPointer!(TFUN)){ + void* func = loadSymbol(funcName, doThrow); + fun = cast(TFUN)func; + } } -} \ No newline at end of file +} From dee3caccef1533fbe2aa1bd0a63489e1f62a3d48 Mon Sep 17 00:00:00 2001 From: dushibaiyu Date: Mon, 26 Sep 2016 20:39:35 +0800 Subject: [PATCH 3/3] add buildhelper --- source/derelict/util/buildhelper.d | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 source/derelict/util/buildhelper.d diff --git a/source/derelict/util/buildhelper.d b/source/derelict/util/buildhelper.d new file mode 100644 index 0000000..d438bdc --- /dev/null +++ b/source/derelict/util/buildhelper.d @@ -0,0 +1,28 @@ +module derelict.util.buildhelper; + +import std.traits; + +template BuildLoadSymbols(alias T, bool doThrow = false) +{ + string _buildLoadSymbols(alias T, bool doThrow = false)() + { + static if(doThrow) + enum dthrow = "true"; + else + enum dthrow = "false"; + + string bindlist = "\n{"; + foreach(mem; __traits(derivedMembers, T)) + { + static if( isFunctionPointer!(__traits(getMember, T, mem)) /*&& !is(typeof(__traits(getMember, T, mem)) == immutable)*/) + { + bindlist ~= "\tbindFunc(" ~ mem ~ ", \"" ~ mem ~ "\", " ~ dthrow ~ ");\n"; + } + } + bindlist ~= "}"; + return bindlist; + } + + enum BuildLoadSymbols = _buildLoadSymbols!(T, doThrow)(); +} +