gstreamer/subclass/
pad.rs1use glib::{prelude::*, subclass::prelude::*, translate::*};
4
5use super::prelude::*;
6use crate::{Pad, ffi};
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 unsafe {
67 let instance = &*(ptr as *mut T::Instance);
68 let imp = instance.imp();
69
70 imp.linked(&from_glib_borrow(peer))
71 }
72}
73
74unsafe extern "C" fn pad_unlinked<T: PadImpl>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad) {
75 unsafe {
76 let instance = &*(ptr as *mut T::Instance);
77 let imp = instance.imp();
78
79 imp.unlinked(&from_glib_borrow(peer))
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use std::sync::atomic;
86
87 use super::*;
88 use crate::{PadDirection, prelude::*};
89
90 pub mod imp {
91 use super::*;
92
93 #[derive(Default)]
94 pub struct TestPad {
95 pub(super) linked: atomic::AtomicBool,
96 pub(super) unlinked: atomic::AtomicBool,
97 }
98
99 #[glib::object_subclass]
100 impl ObjectSubclass for TestPad {
101 const NAME: &'static str = "TestPad";
102 type Type = super::TestPad;
103 type ParentType = Pad;
104 }
105
106 impl ObjectImpl for TestPad {}
107
108 impl GstObjectImpl for TestPad {}
109
110 impl PadImpl for TestPad {
111 fn linked(&self, peer: &Pad) {
112 self.linked.store(true, atomic::Ordering::SeqCst);
113 self.parent_linked(peer)
114 }
115
116 fn unlinked(&self, peer: &Pad) {
117 self.unlinked.store(true, atomic::Ordering::SeqCst);
118 self.parent_unlinked(peer)
119 }
120 }
121 }
122
123 glib::wrapper! {
124 pub struct TestPad(ObjectSubclass<imp::TestPad>) @extends Pad, crate::Object;
125 }
126
127 impl TestPad {
128 pub fn new(name: &str, direction: PadDirection) -> Self {
129 glib::Object::builder()
130 .property("name", name)
131 .property("direction", direction)
132 .build()
133 }
134 }
135
136 #[test]
137 fn test_pad_subclass() {
138 crate::init().unwrap();
139
140 let pad = TestPad::new("test", PadDirection::Src);
141
142 assert_eq!(pad.name(), "test");
143
144 let otherpad = Pad::builder(PadDirection::Sink).name("other-test").build();
145 pad.link(&otherpad).unwrap();
146 pad.unlink(&otherpad).unwrap();
147
148 let imp = pad.imp();
149 assert!(imp.linked.load(atomic::Ordering::SeqCst));
150 assert!(imp.unlinked.load(atomic::Ordering::SeqCst));
151 }
152}