gstreamer/subclass/
pad.rs
1use glib::{prelude::*, subclass::prelude::*, translate::*};
4
5use super::prelude::*;
6use crate::{ffi, Pad};
7
8pub trait PadImpl: GstObjectImpl + ObjectSubclass<Type: IsA<Pad>> {
9 fn linked(&self, peer: &Pad) {
10 self.parent_linked(peer)
11 }
12
13 fn unlinked(&self, peer: &Pad) {
14 self.parent_unlinked(peer)
15 }
16}
17
18pub trait PadImplExt: PadImpl {
19 fn parent_linked(&self, peer: &Pad) {
20 unsafe {
21 let data = Self::type_data();
22 let parent_class = data.as_ref().parent_class() as *mut ffi::GstPadClass;
23
24 (*parent_class)
25 .linked
26 .map(|f| {
27 f(
28 self.obj().unsafe_cast_ref::<Pad>().to_glib_none().0,
29 peer.to_glib_none().0,
30 )
31 })
32 .unwrap_or(())
33 }
34 }
35
36 fn parent_unlinked(&self, peer: &Pad) {
37 unsafe {
38 let data = Self::type_data();
39 let parent_class = data.as_ref().parent_class() as *mut ffi::GstPadClass;
40
41 (*parent_class)
42 .unlinked
43 .map(|f| {
44 f(
45 self.obj().unsafe_cast_ref::<Pad>().to_glib_none().0,
46 peer.to_glib_none().0,
47 )
48 })
49 .unwrap_or(())
50 }
51 }
52}
53
54impl<T: PadImpl> PadImplExt for T {}
55
56unsafe impl<T: PadImpl> IsSubclassable<T> for Pad {
57 fn class_init(klass: &mut glib::Class<Self>) {
58 Self::parent_class_init::<T>(klass);
59 let klass = klass.as_mut();
60 klass.linked = Some(pad_linked::<T>);
61 klass.unlinked = Some(pad_unlinked::<T>);
62 }
63}
64
65unsafe extern "C" fn pad_linked<T: PadImpl>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad) {
66 let instance = &*(ptr as *mut T::Instance);
67 let imp = instance.imp();
68
69 imp.linked(&from_glib_borrow(peer))
70}
71
72unsafe extern "C" fn pad_unlinked<T: PadImpl>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad) {
73 let instance = &*(ptr as *mut T::Instance);
74 let imp = instance.imp();
75
76 imp.unlinked(&from_glib_borrow(peer))
77}
78
79#[cfg(test)]
80mod tests {
81 use std::sync::atomic;
82
83 use super::*;
84 use crate::{prelude::*, PadDirection};
85
86 pub mod imp {
87 use super::*;
88
89 #[derive(Default)]
90 pub struct TestPad {
91 pub(super) linked: atomic::AtomicBool,
92 pub(super) unlinked: atomic::AtomicBool,
93 }
94
95 #[glib::object_subclass]
96 impl ObjectSubclass for TestPad {
97 const NAME: &'static str = "TestPad";
98 type Type = super::TestPad;
99 type ParentType = Pad;
100 }
101
102 impl ObjectImpl for TestPad {}
103
104 impl GstObjectImpl for TestPad {}
105
106 impl PadImpl for TestPad {
107 fn linked(&self, peer: &Pad) {
108 self.linked.store(true, atomic::Ordering::SeqCst);
109 self.parent_linked(peer)
110 }
111
112 fn unlinked(&self, peer: &Pad) {
113 self.unlinked.store(true, atomic::Ordering::SeqCst);
114 self.parent_unlinked(peer)
115 }
116 }
117 }
118
119 glib::wrapper! {
120 pub struct TestPad(ObjectSubclass<imp::TestPad>) @extends Pad, crate::Object;
121 }
122
123 impl TestPad {
124 pub fn new(name: &str, direction: PadDirection) -> Self {
125 glib::Object::builder()
126 .property("name", name)
127 .property("direction", direction)
128 .build()
129 }
130 }
131
132 #[test]
133 fn test_pad_subclass() {
134 crate::init().unwrap();
135
136 let pad = TestPad::new("test", PadDirection::Src);
137
138 assert_eq!(pad.name(), "test");
139
140 let otherpad = Pad::builder(PadDirection::Sink).name("other-test").build();
141 pad.link(&otherpad).unwrap();
142 pad.unlink(&otherpad).unwrap();
143
144 let imp = pad.imp();
145 assert!(imp.linked.load(atomic::Ordering::SeqCst));
146 assert!(imp.unlinked.load(atomic::Ordering::SeqCst));
147 }
148}