rust_macios/core_graphics/
cg_function.rs

1use libc::{c_uint, c_void, size_t};
2
3use crate::{core_foundation::CFTypeID, object};
4
5use super::CGFloat;
6
7/// A general facility for defining and using callback functions.
8pub type CGFunctionRef = CGFunction;
9
10object! {
11    /// A general facility for defining and using callback functions.
12    unsafe pub  struct CGFunction;
13}
14
15impl CGFunction {
16    /// Creates a Core Graphics function.
17    ///
18    /// # Parameters
19    ///
20    /// * `info`: A pointer to user-defined storage for data that you want to pass to your callbacks. You need to make sure that the data persists for as long as it’s needed, which can be beyond the scope in which the Core Graphics function is used.
21    /// * `domain_dimension`: The number of inputs.
22    /// * `domain`: An array of (2*domainDimension) floats used to specify the valid intervals of input values. For each k from 0 to (domainDimension - 1), `domain[2*k]` must be less than or equal to `domain[2*k+1]`, and the kth input value will be clipped to lie in the interval `domain[2*k]` ≤ `input[k]` ≤ `domain[2*k+1]`. If this parameter is NULL, then the input values are not clipped.
23    /// * `range_dimension`: The number of outputs.
24    /// * `range`: An array of (2*rangeDimension) floats that specifies the valid intervals of output values. For each k from 0 to (rangeDimension - 1), `range[2*k]` must be less than or equal to `range[2*k+1]`, and the kth output value will be clipped to lie in the interval `range[2*k]` ≤ `output[k]` `≤ range[2*k+1]`. If this parameter is NULL, then the output values are not clipped.
25    /// * `callbacks`: A pointer to a callback function table. This table should contain pointers to the callbacks you provide to implement the semantics of this Core Graphics function. Core Graphics makes a copy of your table, so, for example, you could safely pass in a pointer to a structure on the stack.
26    ///
27    /// # Returns
28    ///
29    /// The new Core Graphics function. You are responsible for releasing this object using `release`.
30    ///
31    /// # Safety
32    ///
33    /// The `callbacks` parameter must be a valid pointer to a CGFunctionCallbacks structure.
34    pub unsafe fn create(
35        info: *mut c_void,
36        domain_dimension: size_t,
37        domain: *const CGFloat,
38        range_dimension: size_t,
39        range: *const size_t,
40        callbacks: *const CGFunctionCallbacks,
41    ) -> CGFunctionRef {
42        CGFunctionCreate(
43            info,
44            domain_dimension,
45            domain,
46            range_dimension,
47            range,
48            callbacks,
49        )
50    }
51
52    /// Decrements the retain count of a function object.
53    ///
54    /// # Parameters
55    ///
56    /// * `function`: The function object to release.
57    ///
58    /// # Safety
59    ///
60    /// It does not cause an error if the function parameter is null.
61    pub unsafe fn release(function: CGFunctionRef) {
62        CGFunctionRelease(function);
63    }
64
65    /// Increments the retain count of a function object.
66    ///
67    /// # Parameters
68    ///
69    /// * `function`: The function object to retain.
70    ///
71    /// # Returns
72    ///
73    /// The function object passed in.
74    ///
75    /// # Safety
76    ///
77    /// It does not cause an error if the function parameter is null.
78    pub unsafe fn retain(function: CGFunctionRef) -> CGFunctionRef {
79        CGFunctionRetain(function)
80    }
81
82    /// Returns the type identifier for Core Graphics function objects.
83    ///
84    /// # Returns
85    ///
86    /// The type identifier for Core Graphics function objects.
87    ///
88    /// # Safety
89    ///
90    /// This function is unsafe because it does not ensure that the returned value is a valid type identifier.
91    pub unsafe fn type_id() -> CFTypeID {
92        CGFunctionGetTypeID()
93    }
94}
95
96extern "C" {
97    fn CGFunctionCreate(
98        info: *mut c_void,
99        domainDimension: size_t,
100        domain: *const CGFloat,
101        rangeDimension: size_t,
102        range: *const size_t,
103        callbacks: *const CGFunctionCallbacks,
104    ) -> CGFunctionRef;
105
106    fn CGFunctionRelease(function: CGFunctionRef);
107
108    fn CGFunctionRetain(function: CGFunctionRef) -> CGFunctionRef;
109
110    fn CGFunctionGetTypeID() -> CFTypeID;
111}
112
113/// Performs custom operations on the supplied input data to produce output data.
114pub type CGFunctionEvaluateCallback =
115    extern "C" fn(info: *mut c_void, in_values: *const CGFloat, out_values: *mut CGFloat);
116
117/// Performs custom clean-up tasks when Core Graphics deallocates a CGFunctionRef object.
118pub type CGFunctionReleaseInfoCallback = extern "C" fn(info: *mut c_void);
119
120/// A structure that contains callbacks needed by a CGFunctionRef object.
121#[derive(Debug)]
122pub struct CGFunctionCallbacks {
123    /// The structure version number. For this structure,the version should be 0.
124    pub version: c_uint,
125    /// The callback that evaluates the function.
126    pub evaluate: *mut CGFunctionEvaluateCallback,
127    /// If non-NULL,the callback used to release the info parameterpassed to CGFunctionCreate.
128    pub release_info: *mut CGFunctionReleaseInfoCallback,
129}
130
131#[cfg(test)]
132mod tests {
133    use std::ptr;
134
135    use super::*;
136
137    #[test]
138    fn test_create() {
139        unsafe {
140            let callbacks = CGFunctionCallbacks {
141                version: 0,
142                evaluate: ptr::null_mut(),
143                release_info: ptr::null_mut(),
144            };
145
146            let function =
147                CGFunction::create(ptr::null_mut(), 0, ptr::null(), 0, ptr::null(), &callbacks);
148
149            let id = CGFunction::type_id();
150
151            assert!(id != 0);
152
153            CGFunction::release(function);
154        }
155    }
156}