Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions source/derelict/util/buildhelper.d
Original file line number Diff line number Diff line change
@@ -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)();
}

23 changes: 22 additions & 1 deletion source/derelict/util/loader.d
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,26 @@ abstract class SharedLibLoader {
void* func = loadSymbol(funcName, doThrow);
*ptr = 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;
}
}
}
}