1 /*
2 
3 Boost Software License - Version 1.0 - August 17th, 2003
4 
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 
27 */
28 module derelict.util.loader;
29 
30 private
31 {
32     import derelict.util.sharedlib;
33     import derelict.util.compat;
34 }
35 
36 class SharedLibLoader
37 {
38 public:
39     this(string winLibs, string nixLibs, string macLibs)
40     {
41         version(Windows)
42         {
43             _libNames = winLibs;
44         }
45         else version(OSX)
46         {
47             _libNames = macLibs;
48         }
49         else version(darwin)
50         {
51             _libNames = macLibs;
52         }
53         else
54         {
55             _libNames = nixLibs;
56         }
57 
58         _lib = new SharedLib();
59     }
60 
61     static void disableAutoUnload()
62     {
63         _manualUnload = true;
64     }
65 
66     static bool isAutoUnloadEnabled()
67     {
68         return (_manualUnload == false);
69     }
70 
71     void load()
72     {
73         load(_libNames);
74     }
75 
76     void load(string libNameString)
77     {
78         assert(libNameString !is null);
79 
80         string[] libNames = libNameString.splitStr(",");
81         foreach(ref string l; libNames)
82         {
83             l = l.stripWhiteSpace();
84         }
85 
86         load(libNames);
87     }
88 
89     void load(string[] libNames)
90     {
91         _lib.load(libNames);
92         loadSymbols();
93     }
94 
95     void unload()
96     {
97         _lib.unload();
98     }
99 
100     bool isLoaded()
101     {
102         return _lib.isLoaded;
103     }
104 
105 protected:
106     abstract void loadSymbols();
107 
108     void* loadSymbol(string name)
109     {
110         return _lib.loadSymbol(name);
111     }
112 
113     SharedLib lib()
114     {
115         return _lib;
116     }
117 
118     void bindFunc(void** ptr, string funcName, bool doThrow = true)
119     {
120         void* func = lib.loadSymbol(funcName, doThrow);
121         *ptr = func;
122     }
123 
124 private:
125     static bool _manualUnload;
126     string _libNames;
127     SharedLib _lib;
128 }
129 
130 /*
131 * These templates need to stick around a bit longer, until the macinit stuff
132 in Derelict SDL gets sorted
133 */
134 package struct Binder(T) {
135     void opCall(in char[] n, SharedLib lib) {
136         *fptr = lib.loadSymbol(n);
137     }
138 
139 
140     private {
141         void** fptr;
142     }
143 }
144 
145 
146 template bindFunc(T) {
147     Binder!(T) bindFunc(inout T a) {
148         Binder!(T) res;
149         res.fptr = cast(void**)&a;
150         return res;
151     }
152 }