1 /*
2 Boost Software License - Version 1.0 - August 17th, 2003
3 
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 
11 The copyright notices in the Software and this entire statement, including
12 the above license grant, this restriction and the following disclaimer,
13 must be included in all copies of the Software, in whole or in part, and
14 all derivative works of the Software, unless such copies or derivative
15 works are solely in the form of machine-executable object code generated by
16 a source language processor.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 */
26 module bindbc.newton.binddynamic;
27 
28 import bindbc.loader;
29 import bindbc.newton.types;
30 import bindbc.newton.funcs;
31 
32 enum NewtonSupport {
33     noLibrary,
34     badLibrary,
35     newton314
36 }
37 
38 private
39 {
40     SharedLib lib;
41     NewtonSupport loadedVersion;
42 }
43 
44 void unloadNewton()
45 {
46     if (lib != invalidHandle)
47     {
48         lib.unload();
49     }
50 }
51 
52 NewtonSupport loadedNewtonVersion() { return loadedVersion; }
53 bool isNewtonLoaded() { return lib != invalidHandle; }
54 
55 NewtonSupport loadNewton()
56 {
57     version(Windows)
58     {
59         const(char)[][1] libNames =
60         [
61             "newton.dll"
62         ];
63     }
64     else version(OSX)
65     {
66         const(char)[][1] libNames =
67         [
68             "/usr/local/lib/libnewton.dylib"
69         ];
70     }
71     else version(Posix)
72     {
73         const(char)[][1] libNames =
74         [
75             "/usr/local/lib/libnewton.so",
76             "libnewton.so"
77         ];
78     }
79     else static assert(0, "bindbc-newton is not yet supported on this platform.");
80 
81     NewtonSupport ret;
82     foreach(name; libNames)
83     {
84         ret = loadNewton(name.ptr);
85         if (ret != NewtonSupport.noLibrary)
86             break;
87     }
88     return ret;
89 }
90 
91 NewtonSupport loadNewton(const(char)* libName)
92 {    
93     lib = load(libName);
94     if(lib == invalidHandle)
95     {
96         return NewtonSupport.noLibrary;
97     }
98 
99     auto errCount = errorCount();
100     loadedVersion = NewtonSupport.badLibrary;
101     
102     import std.algorithm.searching: startsWith;
103     static foreach(m; __traits(allMembers, bindbc.newton.funcs))
104     {
105         static if (m.startsWith("da_"))
106             lib.bindSymbol(
107                 cast(void**)&__traits(getMember, bindbc.newton.funcs, m[3..$]),
108                 __traits(getMember, bindbc.newton.funcs, m[3..$]).stringof);
109     }
110     
111     loadedVersion = NewtonSupport.newton314;
112 
113     if (errorCount() != errCount)
114         return NewtonSupport.badLibrary;
115 
116     return loadedVersion;
117 }