1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use crate::translate::*;
4 use crate::Value;
5 use std::cmp::Ordering;
6 use std::ops;
7 use std::slice;
8 
9 wrapper! {
10     #[derive(Debug)]
11     #[doc(alias = "GValueArray")]
12     pub struct ValueArray(Boxed<gobject_ffi::GValueArray>);
13 
14     match fn {
15         copy => |ptr| gobject_ffi::g_value_array_copy(mut_override(ptr)),
16         free => |ptr| gobject_ffi::g_value_array_free(ptr),
17         type_ => || gobject_ffi::g_value_array_get_type(),
18     }
19 }
20 
21 impl ValueArray {
22     #[doc(alias = "g_value_array_new")]
new(n_prealloced: u32) -> ValueArray23     pub fn new(n_prealloced: u32) -> ValueArray {
24         unsafe { from_glib_full(gobject_ffi::g_value_array_new(n_prealloced)) }
25     }
26 
27     #[doc(alias = "g_value_array_append")]
append(&mut self, value: &Value)28     pub fn append(&mut self, value: &Value) {
29         let value = value.to_glib_none();
30         unsafe {
31             gobject_ffi::g_value_array_append(self.to_glib_none_mut().0, value.0);
32         }
33     }
34 
is_empty(&self) -> bool35     pub fn is_empty(&self) -> bool {
36         self.len() == 0
37     }
38 
len(&self) -> usize39     pub fn len(&self) -> usize {
40         let value = self.to_glib_none();
41         value.1.n_values as usize
42     }
43 
44     #[doc(alias = "get_nth")]
45     #[doc(alias = "g_value_array_get_nth")]
nth(&self, index_: u32) -> Option<Value>46     pub fn nth(&self, index_: u32) -> Option<Value> {
47         unsafe {
48             from_glib_none(gobject_ffi::g_value_array_get_nth(
49                 mut_override(self.to_glib_none().0),
50                 index_,
51             ))
52         }
53     }
54 
55     #[doc(alias = "g_value_array_insert")]
insert(&mut self, index_: u32, value: &Value)56     pub fn insert(&mut self, index_: u32, value: &Value) {
57         let value = value.to_glib_none();
58         unsafe {
59             gobject_ffi::g_value_array_insert(self.to_glib_none_mut().0, index_, value.0);
60         }
61     }
62 
63     #[doc(alias = "g_value_array_prepend")]
prepend(&mut self, value: &Value)64     pub fn prepend(&mut self, value: &Value) {
65         let value = value.to_glib_none();
66         unsafe {
67             gobject_ffi::g_value_array_prepend(self.to_glib_none_mut().0, value.0);
68         }
69     }
70 
71     #[doc(alias = "g_value_array_remove")]
remove(&mut self, index_: u32)72     pub fn remove(&mut self, index_: u32) {
73         unsafe {
74             gobject_ffi::g_value_array_remove(self.to_glib_none_mut().0, index_);
75         }
76     }
77 
78     #[doc(alias = "g_value_array_sort_with_data")]
sort_with_data<F: FnMut(&Value, &Value) -> Ordering>(&mut self, compare_func: F)79     pub fn sort_with_data<F: FnMut(&Value, &Value) -> Ordering>(&mut self, compare_func: F) {
80         unsafe extern "C" fn compare_func_trampoline(
81             a: ffi::gconstpointer,
82             b: ffi::gconstpointer,
83             func: ffi::gpointer,
84         ) -> i32 {
85             let func = func as *mut &mut (dyn FnMut(&Value, &Value) -> Ordering);
86 
87             let a = &*(a as *const Value);
88             let b = &*(b as *const Value);
89 
90             match (*func)(a, b) {
91                 Ordering::Less => -1,
92                 Ordering::Equal => 0,
93                 Ordering::Greater => 1,
94             }
95         }
96         unsafe {
97             let mut func = compare_func;
98             let func_obj: &mut (dyn FnMut(&Value, &Value) -> Ordering) = &mut func;
99             let func_ptr =
100                 &func_obj as *const &mut (dyn FnMut(&Value, &Value) -> Ordering) as ffi::gpointer;
101 
102             gobject_ffi::g_value_array_sort_with_data(
103                 self.to_glib_none_mut().0,
104                 Some(compare_func_trampoline),
105                 func_ptr,
106             );
107         }
108     }
109 }
110 
111 impl ops::Deref for ValueArray {
112     type Target = [Value];
113 
deref(&self) -> &[Value]114     fn deref(&self) -> &[Value] {
115         unsafe {
116             slice::from_raw_parts(
117                 (*self.to_glib_none().0).values as *const Value,
118                 (*self.to_glib_none().0).n_values as usize,
119             )
120         }
121     }
122 }
123 
124 impl ops::DerefMut for ValueArray {
deref_mut(&mut self) -> &mut [Value]125     fn deref_mut(&mut self) -> &mut [Value] {
126         unsafe {
127             slice::from_raw_parts_mut(
128                 (*self.to_glib_none().0).values as *mut Value,
129                 (*self.to_glib_none().0).n_values as usize,
130             )
131         }
132     }
133 }
134