1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Take a look at the license at the top of the repository in the LICENSE file.

use super::prelude::*;
use glib::prelude::*;
use glib::subclass::prelude::*;
use glib::translate::*;

use crate::ChildProxy;

pub trait ChildProxyImpl: GstObjectImpl + Send + Sync {
    fn child_by_name(&self, object: &Self::Type, name: &str) -> Option<glib::Object> {
        self.parent_child_by_name(object, name)
    }

    fn child_by_index(&self, object: &Self::Type, index: u32) -> Option<glib::Object>;
    fn children_count(&self, object: &Self::Type) -> u32;

    fn child_added(&self, object: &Self::Type, child: &glib::Object, name: &str) {
        self.parent_child_added(object, child, name);
    }
    fn child_removed(&self, object: &Self::Type, child: &glib::Object, name: &str) {
        self.parent_child_removed(object, child, name);
    }
}

pub trait ChildProxyImplExt: ObjectSubclass {
    fn parent_child_by_name(&self, object: &Self::Type, name: &str) -> Option<glib::Object>;

    fn parent_child_by_index(&self, object: &Self::Type, index: u32) -> Option<glib::Object>;
    fn parent_children_count(&self, object: &Self::Type) -> u32;

    fn parent_child_added(&self, _object: &Self::Type, _child: &glib::Object, _name: &str);
    fn parent_child_removed(&self, _object: &Self::Type, _child: &glib::Object, _name: &str);
}

impl<T: ChildProxyImpl> ChildProxyImplExt for T {
    fn parent_child_by_name(&self, object: &Self::Type, name: &str) -> Option<glib::Object> {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
                as *const ffi::GstChildProxyInterface;

            let func = (*parent_iface)
                .get_child_by_name
                .expect("no parent \"child_by_name\" implementation");
            let ret = func(
                object.unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
                name.to_glib_none().0,
            );
            from_glib_full(ret)
        }
    }

    fn parent_child_by_index(&self, object: &Self::Type, index: u32) -> Option<glib::Object> {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
                as *const ffi::GstChildProxyInterface;

            let func = (*parent_iface)
                .get_child_by_index
                .expect("no parent \"child_by_index\" implementation");
            let ret = func(
                object.unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
                index,
            );
            from_glib_full(ret)
        }
    }

    fn parent_children_count(&self, object: &Self::Type) -> u32 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
                as *const ffi::GstChildProxyInterface;

            let func = (*parent_iface)
                .get_children_count
                .expect("no parent \"children_count\" implementation");
            func(object.unsafe_cast_ref::<ChildProxy>().to_glib_none().0)
        }
    }

    fn parent_child_added(&self, object: &Self::Type, child: &glib::Object, name: &str) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
                as *const ffi::GstChildProxyInterface;

            if let Some(func) = (*parent_iface).child_added {
                func(
                    object.unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
                    child.to_glib_none().0,
                    name.to_glib_none().0,
                );
            }
        }
    }

    fn parent_child_removed(&self, object: &Self::Type, child: &glib::Object, name: &str) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<ChildProxy>()
                as *const ffi::GstChildProxyInterface;

            if let Some(func) = (*parent_iface).child_removed {
                func(
                    object.unsafe_cast_ref::<ChildProxy>().to_glib_none().0,
                    child.to_glib_none().0,
                    name.to_glib_none().0,
                );
            }
        }
    }
}

unsafe impl<T: ChildProxyImpl> IsImplementable<T> for ChildProxy {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        iface.get_child_by_name = Some(child_proxy_get_child_by_name::<T>);
        iface.get_child_by_index = Some(child_proxy_get_child_by_index::<T>);
        iface.get_children_count = Some(child_proxy_get_children_count::<T>);
        iface.child_added = Some(child_proxy_child_added::<T>);
        iface.child_removed = Some(child_proxy_child_removed::<T>);
    }
}

unsafe extern "C" fn child_proxy_get_child_by_name<T: ChildProxyImpl>(
    child_proxy: *mut ffi::GstChildProxy,
    name: *const libc::c_char,
) -> *mut glib::gobject_ffi::GObject {
    let instance = &*(child_proxy as *mut T::Instance);
    let imp = instance.imp();

    imp.child_by_name(
        from_glib_borrow::<_, ChildProxy>(child_proxy).unsafe_cast_ref(),
        &glib::GString::from_glib_borrow(name),
    )
    .to_glib_full()
}

unsafe extern "C" fn child_proxy_get_child_by_index<T: ChildProxyImpl>(
    child_proxy: *mut ffi::GstChildProxy,
    index: u32,
) -> *mut glib::gobject_ffi::GObject {
    let instance = &*(child_proxy as *mut T::Instance);
    let imp = instance.imp();

    imp.child_by_index(
        from_glib_borrow::<_, ChildProxy>(child_proxy).unsafe_cast_ref(),
        index,
    )
    .to_glib_full()
}

unsafe extern "C" fn child_proxy_get_children_count<T: ChildProxyImpl>(
    child_proxy: *mut ffi::GstChildProxy,
) -> u32 {
    let instance = &*(child_proxy as *mut T::Instance);
    let imp = instance.imp();

    imp.children_count(from_glib_borrow::<_, ChildProxy>(child_proxy).unsafe_cast_ref())
}

unsafe extern "C" fn child_proxy_child_added<T: ChildProxyImpl>(
    child_proxy: *mut ffi::GstChildProxy,
    child: *mut glib::gobject_ffi::GObject,
    name: *const libc::c_char,
) {
    let instance = &*(child_proxy as *mut T::Instance);
    let imp = instance.imp();

    imp.child_added(
        from_glib_borrow::<_, ChildProxy>(child_proxy).unsafe_cast_ref(),
        &from_glib_borrow(child),
        &glib::GString::from_glib_borrow(name),
    )
}

unsafe extern "C" fn child_proxy_child_removed<T: ChildProxyImpl>(
    child_proxy: *mut ffi::GstChildProxy,
    child: *mut glib::gobject_ffi::GObject,
    name: *const libc::c_char,
) {
    let instance = &*(child_proxy as *mut T::Instance);
    let imp = instance.imp();

    imp.child_removed(
        from_glib_borrow::<_, ChildProxy>(child_proxy).unsafe_cast_ref(),
        &from_glib_borrow(child),
        &glib::GString::from_glib_borrow(name),
    )
}