tm_rs/
the_truth.rs

1use crate::api::{Api, ApiWithCtx, ApiWithCtxMut};
2use std::ffi::c_void;
3use tm_sys::ffi::{
4    tm_the_truth_api, tm_the_truth_o, tm_the_truth_object_o, tm_the_truth_property_definition_t,
5    tm_tt_id_t, TM_THE_TRUTH_API_NAME,
6};
7
8#[derive(Copy, Clone)]
9pub struct TheTruthApi {
10    api: *mut tm_the_truth_api,
11}
12
13unsafe impl Send for TheTruthApi {}
14unsafe impl Sync for TheTruthApi {}
15
16impl Api for TheTruthApi {
17    type CType = *mut tm_the_truth_api;
18    const NAME: &'static [u8] = TM_THE_TRUTH_API_NAME;
19
20    #[inline]
21    fn new(api: *mut c_void) -> Self {
22        Self {
23            api: api as Self::CType,
24        }
25    }
26}
27
28#[derive(Copy, Clone)]
29pub struct TheTruthApiInstance {
30    pub api: *mut tm_the_truth_api,
31    pub ctx: *const tm_the_truth_o,
32}
33
34#[derive(Copy, Clone)]
35pub struct TheTruthApiInstanceMut {
36    pub api: *mut tm_the_truth_api,
37    pub ctx: *mut tm_the_truth_o,
38}
39
40impl ApiWithCtx for TheTruthApi {
41    type Ctx = tm_the_truth_o;
42    type ApiInstance = TheTruthApiInstance;
43
44    #[inline]
45    fn wrap(self, ctx: *const Self::Ctx) -> Self::ApiInstance {
46        TheTruthApiInstance { api: self.api, ctx }
47    }
48}
49
50impl ApiWithCtxMut for TheTruthApi {
51    type ApiInstanceMut = TheTruthApiInstanceMut;
52
53    #[inline]
54    fn wrap_mut(self, ctx: *mut Self::Ctx) -> Self::ApiInstanceMut {
55        TheTruthApiInstanceMut { api: self.api, ctx }
56    }
57}
58
59impl TheTruthApiInstance {
60    pub fn read(&self, id: tm_tt_id_t) -> *const tm_the_truth_object_o {
61        unsafe { (*self.api).read.unwrap()(self.ctx, id) }
62    }
63
64    #[inline]
65    #[allow(clippy::not_unsafe_ptr_arg_deref)]
66    pub fn get_f32(&self, tto: *const tm_the_truth_object_o, property: u32) -> f32 {
67        assert!(!tto.is_null());
68        unsafe { (*self.api).get_float.unwrap()(self.ctx, tto, property) }
69    }
70
71    #[inline]
72    #[allow(clippy::not_unsafe_ptr_arg_deref)]
73    pub fn get_f64(&self, tto: *const tm_the_truth_object_o, property: u32) -> f64 {
74        assert!(!tto.is_null());
75        unsafe { (*self.api).get_double.unwrap()(self.ctx, tto, property) }
76    }
77
78    #[inline]
79    #[allow(clippy::not_unsafe_ptr_arg_deref)]
80    pub fn get_u32(&self, tto: *const tm_the_truth_object_o, property: u32) -> u32 {
81        assert!(!tto.is_null());
82        unsafe { (*self.api).get_uint32_t.unwrap()(self.ctx, tto, property) }
83    }
84
85    #[inline]
86    #[allow(clippy::not_unsafe_ptr_arg_deref)]
87    pub fn get_u64(&self, tto: *const tm_the_truth_object_o, property: u32) -> u64 {
88        assert!(!tto.is_null());
89        unsafe { (*self.api).get_uint64_t.unwrap()(self.ctx, tto, property) }
90    }
91
92    #[inline]
93    #[allow(clippy::not_unsafe_ptr_arg_deref)]
94    pub fn get_bool(&self, tto: *const tm_the_truth_object_o, property: u32) -> bool {
95        assert!(!tto.is_null());
96        unsafe { (*self.api).get_bool.unwrap()(self.ctx, tto, property) }
97    }
98}
99
100impl TheTruthApiInstanceMut {
101    #[inline]
102    pub fn create_object_type(
103        &mut self,
104        name: &[u8],
105        properties: &[tm_the_truth_property_definition_t],
106    ) -> u64 {
107        unsafe {
108            (*self.api).create_object_type.unwrap()(
109                self.ctx,
110                name.as_ptr() as *const ::std::os::raw::c_char,
111                properties.as_ptr(),
112                properties.len() as u32,
113            )
114        }
115    }
116}