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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::api::{Api, ApiWithCtx, ApiWithCtxMut};
use std::ffi::c_void;
use tm_sys::ffi::{
    tm_the_truth_api, tm_the_truth_o, tm_the_truth_object_o, tm_the_truth_property_definition_t,
    tm_tt_id_t, TM_THE_TRUTH_API_NAME,
};

#[derive(Copy, Clone)]
pub struct TheTruthApi {
    api: *mut tm_the_truth_api,
}

unsafe impl Send for TheTruthApi {}
unsafe impl Sync for TheTruthApi {}

impl Api for TheTruthApi {
    type CType = *mut tm_the_truth_api;
    const NAME: &'static [u8] = TM_THE_TRUTH_API_NAME;

    #[inline]
    fn new(api: *mut c_void) -> Self {
        Self {
            api: api as Self::CType,
        }
    }
}

#[derive(Copy, Clone)]
pub struct TheTruthApiInstance {
    pub api: *mut tm_the_truth_api,
    pub ctx: *const tm_the_truth_o,
}

#[derive(Copy, Clone)]
pub struct TheTruthApiInstanceMut {
    pub api: *mut tm_the_truth_api,
    pub ctx: *mut tm_the_truth_o,
}

impl ApiWithCtx for TheTruthApi {
    type Ctx = tm_the_truth_o;
    type ApiInstance = TheTruthApiInstance;

    #[inline]
    fn wrap(self, ctx: *const Self::Ctx) -> Self::ApiInstance {
        TheTruthApiInstance { api: self.api, ctx }
    }
}

impl ApiWithCtxMut for TheTruthApi {
    type ApiInstanceMut = TheTruthApiInstanceMut;

    #[inline]
    fn wrap_mut(self, ctx: *mut Self::Ctx) -> Self::ApiInstanceMut {
        TheTruthApiInstanceMut { api: self.api, ctx }
    }
}

impl TheTruthApiInstance {
    pub fn read(&self, id: tm_tt_id_t) -> *const tm_the_truth_object_o {
        unsafe { (*self.api).read.unwrap()(self.ctx, id) }
    }

    #[inline]
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn get_f32(&self, tto: *const tm_the_truth_object_o, property: u32) -> f32 {
        assert!(!tto.is_null());
        unsafe { (*self.api).get_float.unwrap()(self.ctx, tto, property) }
    }

    #[inline]
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn get_f64(&self, tto: *const tm_the_truth_object_o, property: u32) -> f64 {
        assert!(!tto.is_null());
        unsafe { (*self.api).get_double.unwrap()(self.ctx, tto, property) }
    }

    #[inline]
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn get_u32(&self, tto: *const tm_the_truth_object_o, property: u32) -> u32 {
        assert!(!tto.is_null());
        unsafe { (*self.api).get_uint32_t.unwrap()(self.ctx, tto, property) }
    }

    #[inline]
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn get_u64(&self, tto: *const tm_the_truth_object_o, property: u32) -> u64 {
        assert!(!tto.is_null());
        unsafe { (*self.api).get_uint64_t.unwrap()(self.ctx, tto, property) }
    }

    #[inline]
    #[allow(clippy::not_unsafe_ptr_arg_deref)]
    pub fn get_bool(&self, tto: *const tm_the_truth_object_o, property: u32) -> bool {
        assert!(!tto.is_null());
        unsafe { (*self.api).get_bool.unwrap()(self.ctx, tto, property) }
    }
}

impl TheTruthApiInstanceMut {
    #[inline]
    pub fn create_object_type(
        &mut self,
        name: &[u8],
        properties: &[tm_the_truth_property_definition_t],
    ) -> u64 {
        unsafe {
            (*self.api).create_object_type.unwrap()(
                self.ctx,
                name.as_ptr() as *const ::std::os::raw::c_char,
                properties.as_ptr(),
                properties.len() as u32,
            )
        }
    }
}