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