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             "libnewton.so"
76         ];
77     }
78     else static assert(0, "bindbc-newton is not yet supported on this platform.");
79 
80     NewtonSupport ret;
81     foreach(name; libNames)
82     {
83         ret = loadNewton(name.ptr);
84         if (ret != NewtonSupport.noLibrary)
85             break;
86     }
87     return ret;
88 }
89 
90 NewtonSupport loadNewton(const(char)* libName)
91 {    
92     lib = load(libName);
93     if(lib == invalidHandle)
94     {
95         return NewtonSupport.noLibrary;
96     }
97 
98     auto errCount = errorCount();
99     loadedVersion = NewtonSupport.badLibrary;
100     
101     import std.algorithm.searching: startsWith;
102     static foreach(m; __traits(allMembers, bindbc.newton.funcs))
103     {
104         static if (m.startsWith("da_"))
105             lib.bindSymbol(
106                 cast(void**)&__traits(getMember, bindbc.newton.funcs, m[3..$]),
107                 __traits(getMember, bindbc.newton.funcs, m[3..$]).stringof);
108     }
109     
110     loadedVersion = NewtonSupport.newton314;
111 
112     if (errorCount() != errCount)
113         return NewtonSupport.badLibrary;
114 
115     return loadedVersion;
116 }