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