gstreamer/subclass/
child_proxy.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{prelude::*, subclass::prelude::*, translate::*};
4
5use super::prelude::*;
6use crate::{ffi, ChildProxy};
7
8pub trait ChildProxyImpl: GstObjectImpl + ObjectSubclass<Type: IsA<ChildProxy>> {
9    fn child_by_name(&self, name: &str) -> Option<glib::Object> {
10        self.parent_child_by_name(name)
11    }
12
13    fn child_by_index(&self, index: u32) -> Option<glib::Object>;
14    fn children_count(&self) -> u32;
15
16    fn child_added(&self, child: &glib::Object, name: &str) {
17        self.parent_child_added(child, name);
18    }
19    fn child_removed(&self, child: &glib::Object, name: &str) {
20        self.parent_child_removed(child, name);
21    }
22}
23
24pub trait ChildProxyImplExt: ChildProxyImpl {
25    fn parent_child_by_name(&self, name: &str) -> Option<glib::Object> {
26        unsafe {
27            let type_data = Self::type_data();
28            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
29                as *const ffi::GstChildProxyInterface;
30
31            let func = (*parent_iface)
32                .get_child_by_name
33                .expect("no parent \"child_by_name\" implementation");
34            let ret = func(
35                self.obj().unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
36                name.to_glib_none().0,
37            );
38            from_glib_full(ret)
39        }
40    }
41
42    fn parent_child_by_index(&self, index: u32) -> Option<glib::Object> {
43        unsafe {
44            let type_data = Self::type_data();
45            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
46                as *const ffi::GstChildProxyInterface;
47
48            let func = (*parent_iface)
49                .get_child_by_index
50                .expect("no parent \"child_by_index\" implementation");
51            let ret = func(
52                self.obj().unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
53                index,
54            );
55            from_glib_full(ret)
56        }
57    }
58
59    fn parent_children_count(&self) -> u32 {
60        unsafe {
61            let type_data = Self::type_data();
62            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
63                as *const ffi::GstChildProxyInterface;
64
65            let func = (*parent_iface)
66                .get_children_count
67                .expect("no parent \"children_count\" implementation");
68            func(self.obj().unsafe_cast_ref::<ChildProxy>().to_glib_none().0)
69        }
70    }
71
72    fn parent_child_added(&self, child: &glib::Object, name: &str) {
73        unsafe {
74            let type_data = Self::type_data();
75            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
76                as *const ffi::GstChildProxyInterface;
77
78            if let Some(func) = (*parent_iface).child_added {
79                func(
80                    self.obj().unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
81                    child.to_glib_none().0,
82                    name.to_glib_none().0,
83                );
84            }
85        }
86    }
87
88    fn parent_child_removed(&self, child: &glib::Object, name: &str) {
89        unsafe {
90            let type_data = Self::type_data();
91            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
92                as *const ffi::GstChildProxyInterface;
93
94            if let Some(func) = (*parent_iface).child_removed {
95                func(
96                    self.obj().unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
97                    child.to_glib_none().0,
98                    name.to_glib_none().0,
99                );
100            }
101        }
102    }
103}
104
105impl<T: ChildProxyImpl> ChildProxyImplExt for T {}
106
107unsafe impl<T: ChildProxyImpl> IsImplementable<T> for ChildProxy {
108    fn interface_init(iface: &mut glib::Interface<Self>) {
109        let iface = iface.as_mut();
110
111        iface.get_child_by_name = Some(child_proxy_get_child_by_name::<T>);
112        iface.get_child_by_index = Some(child_proxy_get_child_by_index::<T>);
113        iface.get_children_count = Some(child_proxy_get_children_count::<T>);
114        iface.child_added = Some(child_proxy_child_added::<T>);
115        iface.child_removed = Some(child_proxy_child_removed::<T>);
116    }
117}
118
119unsafe extern "C" fn child_proxy_get_child_by_name<T: ChildProxyImpl>(
120    child_proxy: *mut ffi::GstChildProxy,
121    name: *const libc::c_char,
122) -> *mut glib::gobject_ffi::GObject {
123    let instance = &*(child_proxy as *mut T::Instance);
124    let imp = instance.imp();
125
126    imp.child_by_name(&glib::GString::from_glib_borrow(name))
127        .into_glib_ptr()
128}
129
130unsafe extern "C" fn child_proxy_get_child_by_index<T: ChildProxyImpl>(
131    child_proxy: *mut ffi::GstChildProxy,
132    index: u32,
133) -> *mut glib::gobject_ffi::GObject {
134    let instance = &*(child_proxy as *mut T::Instance);
135    let imp = instance.imp();
136
137    imp.child_by_index(index).into_glib_ptr()
138}
139
140unsafe extern "C" fn child_proxy_get_children_count<T: ChildProxyImpl>(
141    child_proxy: *mut ffi::GstChildProxy,
142) -> u32 {
143    let instance = &*(child_proxy as *mut T::Instance);
144    let imp = instance.imp();
145
146    imp.children_count()
147}
148
149unsafe extern "C" fn child_proxy_child_added<T: ChildProxyImpl>(
150    child_proxy: *mut ffi::GstChildProxy,
151    child: *mut glib::gobject_ffi::GObject,
152    name: *const libc::c_char,
153) {
154    let instance = &*(child_proxy as *mut T::Instance);
155    let imp = instance.imp();
156
157    imp.child_added(
158        &from_glib_borrow(child),
159        &glib::GString::from_glib_borrow(name),
160    )
161}
162
163unsafe extern "C" fn child_proxy_child_removed<T: ChildProxyImpl>(
164    child_proxy: *mut ffi::GstChildProxy,
165    child: *mut glib::gobject_ffi::GObject,
166    name: *const libc::c_char,
167) {
168    let instance = &*(child_proxy as *mut T::Instance);
169    let imp = instance.imp();
170
171    imp.child_removed(
172        &from_glib_borrow(child),
173        &glib::GString::from_glib_borrow(name),
174    )
175}