Port Pad subclassing

This commit is contained in:
Sebastian Dröge 2018-11-20 14:38:20 +02:00
parent 97211e869b
commit ec2a0635ca
6 changed files with 119 additions and 2 deletions

View file

@ -577,6 +577,7 @@ trait_name = "GstObjectExt"
[[object]] [[object]]
name = "Gst.Pad" name = "Gst.Pad"
subclassing = true
status = "generate" status = "generate"
[[object.function]] [[object.function]]
name = "link_maybe_ghosting" name = "link_maybe_ghosting"

View file

@ -8,6 +8,7 @@ use Event;
use EventType; use EventType;
use FlowReturn; use FlowReturn;
use Object; use Object;
use PadClass;
use PadDirection; use PadDirection;
use PadLinkCheck; use PadLinkCheck;
use PadLinkReturn; use PadLinkReturn;
@ -34,7 +35,7 @@ use std::mem::transmute;
use std::ptr; use std::ptr;
glib_wrapper! { glib_wrapper! {
pub struct Pad(Object<ffi::GstPad, ffi::GstPadClass>): Object; pub struct Pad(Object<ffi::GstPad, ffi::GstPadClass, PadClass>): Object;
match fn { match fn {
get_type => || ffi::gst_pad_get_type(), get_type => || ffi::gst_pad_get_type(),

View file

@ -184,6 +184,7 @@ mod gobject;
mod iterator; mod iterator;
mod object; mod object;
mod pad; mod pad;
pub use pad::PadClass;
mod parse_context; mod parse_context;
mod proxy_pad; mod proxy_pad;
mod tag_setter; mod tag_setter;

View file

@ -26,6 +26,7 @@ use StaticPadTemplate;
use std::cell::RefCell; use std::cell::RefCell;
use std::mem; use std::mem;
use std::mem::transmute; use std::mem::transmute;
use std::ops;
use std::ptr; use std::ptr;
use glib; use glib;
@ -34,7 +35,7 @@ use glib::translate::{
ToGlibPtr, ToGlibPtr,
}; };
use glib::Object; use glib::Object;
use glib::{IsA, StaticType}; use glib::{IsA, IsClassFor, StaticType};
use glib_ffi; use glib_ffi;
use glib_ffi::gpointer; use glib_ffi::gpointer;
@ -912,6 +913,30 @@ impl<O: IsA<Pad>> PadExtManual for O {
} }
} }
#[repr(C)]
pub struct PadClass(ffi::GstPadClass);
unsafe impl IsClassFor for PadClass {
type Instance = Pad;
}
unsafe impl Send for PadClass {}
unsafe impl Sync for PadClass {}
impl ops::Deref for PadClass {
type Target = glib::ObjectClass;
fn deref(&self) -> &Self::Target {
self.upcast_ref()
}
}
impl ops::DerefMut for PadClass {
fn deref_mut(&mut self) -> &mut Self::Target {
self.upcast_ref_mut()
}
}
unsafe extern "C" fn trampoline_pad_probe( unsafe extern "C" fn trampoline_pad_probe(
pad: *mut ffi::GstPad, pad: *mut ffi::GstPad,
info: *mut ffi::GstPadProbeInfo, info: *mut ffi::GstPadProbeInfo,

View file

@ -16,6 +16,7 @@ pub mod plugin;
pub mod bin; pub mod bin;
pub mod child_proxy; pub mod child_proxy;
pub mod element; pub mod element;
pub mod pad;
pub mod pipeline; pub mod pipeline;
pub mod uri_handler; pub mod uri_handler;
@ -23,6 +24,7 @@ pub mod prelude {
pub use super::bin::BinImpl; pub use super::bin::BinImpl;
pub use super::child_proxy::ChildProxyImpl; pub use super::child_proxy::ChildProxyImpl;
pub use super::element::{ElementClassSubclassExt, ElementImpl, ElementImplExt}; pub use super::element::{ElementClassSubclassExt, ElementImpl, ElementImplExt};
pub use super::pad::PadImpl;
pub use super::pipeline::PipelineImpl; pub use super::pipeline::PipelineImpl;
pub use super::uri_handler::URIHandlerImpl; pub use super::uri_handler::URIHandlerImpl;
pub use super::PanicPoison; pub use super::PanicPoison;

View file

@ -0,0 +1,87 @@
// Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use ffi;
use glib;
use glib::translate::*;
use glib::subclass::prelude::*;
use Pad;
use PadClass;
pub trait PadImpl: ObjectImpl + Send + Sync + 'static {
fn linked(&self, pad: &Pad, peer: &Pad) {
self.parent_linked(pad, peer)
}
fn unlinked(&self, pad: &Pad, peer: &Pad) {
self.parent_unlinked(pad, peer)
}
fn parent_linked(&self, pad: &Pad, peer: &Pad) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstPadClass;
(*parent_class)
.linked
.map(|f| f(pad.to_glib_none().0, peer.to_glib_none().0))
.unwrap_or(())
}
}
fn parent_unlinked(&self, pad: &Pad, peer: &Pad) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstPadClass;
(*parent_class)
.unlinked
.map(|f| f(pad.to_glib_none().0, peer.to_glib_none().0))
.unwrap_or(())
}
}
}
unsafe impl<T: ObjectSubclass + PadImpl> IsSubclassable<T> for PadClass {
fn override_vfuncs(&mut self) {
<glib::ObjectClass as IsSubclassable<T>>::override_vfuncs(self);
unsafe {
let klass = &mut *(self as *const Self as *mut ffi::GstPadClass);
klass.linked = Some(pad_linked::<T>);
klass.unlinked = Some(pad_unlinked::<T>);
}
}
}
unsafe extern "C" fn pad_linked<T: ObjectSubclass>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad)
where
T: PadImpl,
{
glib_floating_reference_guard!(ptr);
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Pad = from_glib_borrow(ptr);
imp.linked(&wrap, &from_glib_borrow(peer))
}
unsafe extern "C" fn pad_unlinked<T: ObjectSubclass>(ptr: *mut ffi::GstPad, peer: *mut ffi::GstPad)
where
T: PadImpl,
{
glib_floating_reference_guard!(ptr);
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Pad = from_glib_borrow(ptr);
imp.unlinked(&wrap, &from_glib_borrow(peer))
}