1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::Api;
extern crate dlopen;
use dlopen::wrapper::{Container, WrapperApi};
use std::{env, path::Path};

pub type ApiImpl = DynamicApi;

#[derive(WrapperApi)]
struct CApi {
    DlVDBGetFileBBox:
        unsafe extern "C" fn(filename: *const ::std::os::raw::c_char, bbox: *mut f64) -> bool,
    DlVDBGetGridNames: unsafe extern "C" fn(
        filename: *const ::std::os::raw::c_char,
        num_grids: *mut ::std::os::raw::c_int,
        grid_names: *mut *const *const ::std::os::raw::c_char,
    ) -> bool,
    DlVDBFreeGridNames:
        unsafe extern "C" fn(grid_names: *const *const ::std::os::raw::c_char) -> bool,
}

pub struct DynamicApi {
    api: Container<CApi>,
}

impl DynamicApi {
    // macOS implementation
    #[inline]
    pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
        match unsafe { Container::load("/Applications/3Delight/lib/lib3delight.dylib") }
            .or_else(|_| unsafe { Container::load("lib3delight.dylib") })
            .or_else(|_| match env::var("DELIGHT") {
                Err(e) => Err(Box::new(e) as Box<dyn std::error::Error>),
                Ok(delight) => unsafe {
                    Container::load(Path::new(&delight).join("lib").join("lib3delight.dylib"))
                }
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error>),
            }) {
            Err(e) => Err(e),
            Ok(api) => Ok(DynamicApi { api }),
        }
    }
}

impl Api for DynamicApi {
    #[inline]
    unsafe fn DlVDBGetFileBBox(
        &self,
        filename: *const ::std::os::raw::c_char,
        bbox: *mut f64,
    ) -> bool {
        self.api.DlVDBGetFileBBox(filename, bbox)
    }

    #[inline]
    unsafe fn DlVDBGetGridNames(
        &self,
        filename: *const ::std::os::raw::c_char,
        num_grids: *mut ::std::os::raw::c_int,
        grid_names: *mut *const *const ::std::os::raw::c_char,
    ) -> bool {
        self.api.DlVDBGetGridNames(filename, num_grids, grid_names)
    }

    #[inline]
    unsafe fn DlVDBFreeGridNames(&self, grid_names: *const *const ::std::os::raw::c_char) {
        self.api.DlVDBFreeGridNames(grid_names);
    }
}