Remove some boilerplate

This commit is contained in:
Sebastian Dröge 2017-09-25 15:29:39 +03:00
parent f322395918
commit b25e612458
5 changed files with 115 additions and 135 deletions

View file

@ -115,59 +115,41 @@ unsafe impl BaseSinkClass<RsBaseSink> for gst_base_ffi::GstBaseSinkClass {}
unsafe impl BaseSinkClass<RsBaseSink> for RsBaseSinkClass {} unsafe impl BaseSinkClass<RsBaseSink> for RsBaseSinkClass {}
unsafe impl ElementClass<RsBaseSink> for gst_base_ffi::GstBaseSinkClass {} unsafe impl ElementClass<RsBaseSink> for gst_base_ffi::GstBaseSinkClass {}
unsafe impl ElementClass<RsBaseSink> for RsBaseSinkClass {} unsafe impl ElementClass<RsBaseSink> for RsBaseSinkClass {}
unsafe impl ObjectClass for gst_base_ffi::GstBaseSinkClass {}
// FIXME: Boilerplate #[macro_export]
impl BaseSinkImpl for Box<BaseSinkImpl> { macro_rules! box_base_sink_impl(
($name:ident) => {
box_element_impl!($name);
impl BaseSinkImpl for Box<$name> {
fn start(&self, element: &gst_base::BaseSink) -> bool { fn start(&self, element: &gst_base::BaseSink) -> bool {
let imp: &BaseSinkImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.start(element) imp.start(element)
} }
fn stop(&self, element: &gst_base::BaseSink) -> bool { fn stop(&self, element: &gst_base::BaseSink) -> bool {
let imp: &BaseSinkImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.stop(element) imp.stop(element)
} }
fn render(&self, element: &gst_base::BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn { fn render(&self, element: &gst_base::BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn {
let imp: &BaseSinkImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.render(element, buffer) imp.render(element, buffer)
} }
fn query(&self, element: &gst_base::BaseSink, query: &mut gst::QueryRef) -> bool { fn query(&self, element: &gst_base::BaseSink, query: &mut gst::QueryRef) -> bool {
let imp: &BaseSinkImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.query(element, query) imp.query(element, query)
} }
fn event(&self, element: &gst_base::BaseSink, event: &gst::Event) -> bool { fn event(&self, element: &gst_base::BaseSink, event: &gst::Event) -> bool {
let imp: &BaseSinkImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.event(element, event) imp.event(element, event)
} }
}
// FIXME: Boilerplate
impl ElementImpl for Box<BaseSinkImpl> {
fn change_state(
&self,
element: &gst::Element,
transition: gst::StateChange,
) -> gst::StateChangeReturn {
let imp: &BaseSinkImpl = self.as_ref();
imp.change_state(element, transition)
} }
} };
);
// FIXME: Boilerplate box_base_sink_impl!(BaseSinkImpl);
impl ObjectImpl for Box<BaseSinkImpl> {
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
let imp: &BaseSinkImpl = self.as_ref();
imp.set_property(obj, id, value);
}
fn get_property(&self, obj: &glib::Object, id: u32) -> Result<glib::Value, ()> {
let imp: &BaseSinkImpl = self.as_ref();
imp.get_property(obj, id)
}
}
impl ObjectType for RsBaseSink { impl ObjectType for RsBaseSink {
const NAME: &'static str = "RsBaseSink"; const NAME: &'static str = "RsBaseSink";

View file

@ -151,27 +151,30 @@ unsafe impl BaseSrcClass<RsBaseSrc> for gst_base_ffi::GstBaseSrcClass {}
unsafe impl BaseSrcClass<RsBaseSrc> for RsBaseSrcClass {} unsafe impl BaseSrcClass<RsBaseSrc> for RsBaseSrcClass {}
unsafe impl ElementClass<RsBaseSrc> for gst_base_ffi::GstBaseSrcClass {} unsafe impl ElementClass<RsBaseSrc> for gst_base_ffi::GstBaseSrcClass {}
unsafe impl ElementClass<RsBaseSrc> for RsBaseSrcClass {} unsafe impl ElementClass<RsBaseSrc> for RsBaseSrcClass {}
unsafe impl ObjectClass for gst_base_ffi::GstBaseSrcClass {}
// FIXME: Boilerplate #[macro_export]
impl BaseSrcImpl for Box<BaseSrcImpl> { macro_rules! box_base_src_impl(
($name:ident) => {
box_element_impl!($name);
impl BaseSrcImpl for Box<$name> {
fn start(&self, element: &gst_base::BaseSrc) -> bool { fn start(&self, element: &gst_base::BaseSrc) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.start(element) imp.start(element)
} }
fn stop(&self, element: &gst_base::BaseSrc) -> bool { fn stop(&self, element: &gst_base::BaseSrc) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.stop(element) imp.stop(element)
} }
fn is_seekable(&self, element: &gst_base::BaseSrc) -> bool { fn is_seekable(&self, element: &gst_base::BaseSrc) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.is_seekable(element) imp.is_seekable(element)
} }
fn get_size(&self, element: &gst_base::BaseSrc) -> Option<u64> { fn get_size(&self, element: &gst_base::BaseSrc) -> Option<u64> {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.get_size(element) imp.get_size(element)
} }
@ -182,49 +185,27 @@ impl BaseSrcImpl for Box<BaseSrcImpl> {
length: u32, length: u32,
buffer: &mut gst::BufferRef, buffer: &mut gst::BufferRef,
) -> gst::FlowReturn { ) -> gst::FlowReturn {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.fill(element, offset, length, buffer) imp.fill(element, offset, length, buffer)
} }
fn do_seek(&self, element: &gst_base::BaseSrc, segment: &mut gst::Segment) -> bool { fn do_seek(&self, element: &gst_base::BaseSrc, segment: &mut gst::Segment) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.do_seek(element, segment) imp.do_seek(element, segment)
} }
fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool { fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.query(element, query) imp.query(element, query)
} }
fn event(&self, element: &gst_base::BaseSrc, event: &gst::Event) -> bool { fn event(&self, element: &gst_base::BaseSrc, event: &gst::Event) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.event(element, event) imp.event(element, event)
} }
}
// FIXME: Boilerplate
impl ElementImpl for Box<BaseSrcImpl> {
fn change_state(
&self,
element: &gst::Element,
transition: gst::StateChange,
) -> gst::StateChangeReturn {
let imp: &BaseSrcImpl = self.as_ref();
imp.change_state(element, transition)
} }
} };
);
// FIXME: Boilerplate box_base_src_impl!(BaseSrcImpl);
impl ObjectImpl for Box<BaseSrcImpl> {
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
let imp: &BaseSrcImpl = self.as_ref();
imp.set_property(obj, id, value);
}
fn get_property(&self, obj: &glib::Object, id: u32) -> Result<glib::Value, ()> {
let imp: &BaseSrcImpl = self.as_ref();
imp.get_property(obj, id)
}
}
impl ObjectType for RsBaseSrc { impl ObjectType for RsBaseSrc {
const NAME: &'static str = "RsBaseSrc"; const NAME: &'static str = "RsBaseSrc";

View file

@ -118,31 +118,26 @@ pub type RsElementClass = ClassStruct<RsElement>;
// FIXME: Boilerplate // FIXME: Boilerplate
unsafe impl ElementClass<RsElement> for RsElementClass {} unsafe impl ElementClass<RsElement> for RsElementClass {}
unsafe impl ElementClass<RsElement> for gst_ffi::GstElementClass {} unsafe impl ElementClass<RsElement> for gst_ffi::GstElementClass {}
unsafe impl ObjectClass for gst_ffi::GstElementClass {}
// FIXME: Boilerplate #[macro_export]
impl ObjectImpl for Box<ElementImpl> { macro_rules! box_element_impl(
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) { ($name:ident) => {
let imp: &ElementImpl = self.as_ref(); box_object_impl!($name);
imp.set_property(obj, id, value);
}
fn get_property(&self, obj: &glib::Object, id: u32) -> Result<glib::Value, ()> { impl ElementImpl for Box<$name> {
let imp: &ElementImpl = self.as_ref();
imp.get_property(obj, id)
}
}
impl ElementImpl for Box<ElementImpl> {
fn change_state( fn change_state(
&self, &self,
element: &gst::Element, element: &gst::Element,
transition: gst::StateChange, transition: gst::StateChange,
) -> gst::StateChangeReturn { ) -> gst::StateChangeReturn {
let imp: &ElementImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.change_state(element, transition) imp.change_state(element, transition)
} }
} }
};
);
box_element_impl!(ElementImpl);
impl ObjectType for RsElement { impl ObjectType for RsElement {
const NAME: &'static str = "RsElement"; const NAME: &'static str = "RsElement";

View file

@ -72,8 +72,13 @@ pub mod sink;
pub mod demuxer; pub mod demuxer;
pub mod bytes; pub mod bytes;
#[macro_use]
pub mod object; pub mod object;
#[macro_use]
pub mod element; pub mod element;
#[macro_use]
pub mod base_src; pub mod base_src;
#[macro_use]
pub mod base_sink; pub mod base_sink;
#[macro_use]
pub mod uri_handler; pub mod uri_handler;

View file

@ -36,6 +36,23 @@ pub trait ObjectImpl: Send + Sync + 'static {
} }
} }
#[macro_export]
macro_rules! box_object_impl(
($name:ident) => {
impl ObjectImpl for Box<$name> {
fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
let imp: &$name = self.as_ref();
imp.set_property(obj, id, value);
}
fn get_property(&self, obj: &glib::Object, id: u32) -> Result<glib::Value, ()> {
let imp: &$name = self.as_ref();
imp.get_property(obj, id)
}
}
};
);
pub trait ImplTypeStatic<T: ObjectType>: Send + Sync + 'static { pub trait ImplTypeStatic<T: ObjectType>: Send + Sync + 'static {
fn get_name(&self) -> &str; fn get_name(&self) -> &str;
fn new(&self, &T::RsType) -> T::ImplType; fn new(&self, &T::RsType) -> T::ImplType;