| 1 |
<html lang="en"> |
|---|
| 2 |
<head> |
|---|
| 3 |
<title>Selective Symbol Loading</title> |
|---|
| 4 |
<link rel="stylesheet" type="text/css" href="styles.css"> |
|---|
| 5 |
</head> |
|---|
| 6 |
<body> |
|---|
| 7 |
<h2>Selective Symbol Loading</h2> |
|---|
| 8 |
There may be times when you want to provide backwards compatibility with an |
|---|
| 9 |
older version of a library, or when you know that a particular build of a library |
|---|
| 10 |
you are using is missing some functions that a Derelict package loads. Derelict, |
|---|
| 11 |
by default, throws a <tt>SymbolLoadException</tt> when a shared library symbol |
|---|
| 12 |
cannot be found. In order to allow the client to bypass this default behavior, |
|---|
| 13 |
the <a href="util.html">DerelictUtil</a> package provides a callback mechanism |
|---|
| 14 |
that allows you to selectively throw exceptions when symbols are missing. |
|---|
| 15 |
|
|---|
| 16 |
<h3>MissingSymbolCallback</h3> |
|---|
| 17 |
The <tt>derelict.util.exception</tt> module defines a type <tt>MissingSymbolCallback</tt>. |
|---|
| 18 |
The type definition looks like this: |
|---|
| 19 |
|
|---|
| 20 |
<pre> |
|---|
| 21 |
alias bool function(string libName, string symbolName) MissingSymbolCallback; |
|---|
| 22 |
</pre> |
|---|
| 23 |
|
|---|
| 24 |
<p> |
|---|
| 25 |
When a symbol fails to load, Derelict first checks if a <tt>MissingSymbolCallback</tt> |
|---|
| 26 |
has been set. If not, a <tt>SymbolLoadException</tt> is thrown. But, if a callback |
|---|
| 27 |
has been set, then the callback is called with the name of the library |
|---|
| 28 |
and the name of the missing symbol as arguments. The callback should return <tt>true</tt> if |
|---|
| 29 |
loading should continue and <tt>false</tt> if a <tt>SymbolLoadException</tt> should be thrown. |
|---|
| 30 |
Callbacks can be set using the function <tt>Derelict_SetMissingSymbolCallback</tt>. |
|---|
| 31 |
</p> |
|---|
| 32 |
<p> |
|---|
| 33 |
Following is a complete example of using this feature. Older versions of SDL |
|---|
| 34 |
did not include functions to test for CPU capabilities. |
|---|
| 35 |
<a href="sdl.html">DerelictSDL</a> always attempts to load those functions since |
|---|
| 36 |
they are present in the latest versions. It is possible to load older versions |
|---|
| 37 |
of the SDL shared library by ignoring the CPU related functions. This example |
|---|
| 38 |
demonstrates how to do that. |
|---|
| 39 |
</p> |
|---|
| 40 |
|
|---|
| 41 |
<pre> |
|---|
| 42 |
import derelict.util.exception; // MissingSymbolCallback interface declared here |
|---|
| 43 |
import derelict.sdl.sdl; |
|---|
| 44 |
|
|---|
| 45 |
import std.string; |
|---|
| 46 |
|
|---|
| 47 |
bool SDLMissingSymbolCallback(char[] libName, char[] procName) |
|---|
| 48 |
{ |
|---|
| 49 |
// there are 8 functions in SDL's CPU interface - test for them all. |
|---|
| 50 |
// If the procName matches any one of them, return true to ignore the missing |
|---|
| 51 |
// function. |
|---|
| 52 |
if( procName.cmp("SDL_HasRDTSC") == 0 || |
|---|
| 53 |
procName.cmp("SDL_HasMMX") == 0 || |
|---|
| 54 |
procName.cmp("SDL_HasMMXExt") == 0 || |
|---|
| 55 |
procName.cmp("SDL_Has3DNow") == 0 || |
|---|
| 56 |
procName.cmp("SDL_Has3DNowExt") == 0 || |
|---|
| 57 |
procName.cmp("SDL_HasSSE") == 0 || |
|---|
| 58 |
procName.cmp("SDL_HasSSE2") == 0 || |
|---|
| 59 |
procName.cmp("SDL_HasAltiVec") == 0) |
|---|
| 60 |
return true; // ignore the error and throw no exception |
|---|
| 61 |
|
|---|
| 62 |
// a function other than one of those above failed to load - return false |
|---|
| 63 |
// to indicate that an exception should be thrown. |
|---|
| 64 |
return false; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
void main() |
|---|
| 68 |
{ |
|---|
| 69 |
// the callback must be set before loading the library(ies) you want the callback to handle |
|---|
| 70 |
Derelict_SetMissingProcCallback(&myMissingProcCallback); |
|---|
| 71 |
DerelictSDL.load(); |
|---|
| 72 |
} |
|---|
| 73 |
</pre> |
|---|
| 74 |
|
|---|
| 75 |
<p> |
|---|
| 76 |
It is important to note that the callback must be set before loading the library |
|---|
| 77 |
you are interested in filtering. You can implement one callback for multiple libraries if you like, |
|---|
| 78 |
and filter them based upon the <tt>libName</tt> parameter. Or you can define multiple callbacks to |
|---|
| 79 |
handle one library each - as long as you call <tt>Derelict_SetMissingSymbolCallback</tt> with the |
|---|
| 80 |
appropriate callback argument before loading each library. |
|---|
| 81 |
</p> |
|---|
| 82 |
<p> |
|---|
| 83 |
<span class="important">This is an optional feature and is not required to use |
|---|
| 84 |
Derelict</span>. It is provided to give an even greater degree of flexibility |
|---|
| 85 |
and control to those who need it. |
|---|
| 86 |
</p> |
|---|
| 87 |
|
|---|
| 88 |
</body> |
|---|
| 89 |
</html> |
|---|