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(
fn start(&self, element: &gst_base::BaseSink) -> bool { ($name:ident) => {
let imp: &BaseSinkImpl = self.as_ref(); box_element_impl!($name);
imp.start(element)
}
fn stop(&self, element: &gst_base::BaseSink) -> bool { impl BaseSinkImpl for Box<$name> {
let imp: &BaseSinkImpl = self.as_ref(); fn start(&self, element: &gst_base::BaseSink) -> bool {
imp.stop(element) let imp: &$name = self.as_ref();
} imp.start(element)
}
fn render(&self, element: &gst_base::BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn { fn stop(&self, element: &gst_base::BaseSink) -> bool {
let imp: &BaseSinkImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.render(element, buffer) imp.stop(element)
} }
fn query(&self, element: &gst_base::BaseSink, query: &mut gst::QueryRef) -> bool { 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.query(element, query) imp.render(element, buffer)
} }
fn event(&self, element: &gst_base::BaseSink, event: &gst::Event) -> bool {
let imp: &BaseSinkImpl = self.as_ref();
imp.event(element, event)
}
}
// FIXME: Boilerplate fn query(&self, element: &gst_base::BaseSink, query: &mut gst::QueryRef) -> bool {
impl ElementImpl for Box<BaseSinkImpl> { let imp: &$name = self.as_ref();
fn change_state( imp.query(element, query)
&self, }
element: &gst::Element, fn event(&self, element: &gst_base::BaseSink, event: &gst::Event) -> bool {
transition: gst::StateChange, let imp: &$name = self.as_ref();
) -> gst::StateChangeReturn { imp.event(element, event)
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,80 +151,61 @@ 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(
fn start(&self, element: &gst_base::BaseSrc) -> bool { ($name:ident) => {
let imp: &BaseSrcImpl = self.as_ref(); box_element_impl!($name);
imp.start(element)
}
fn stop(&self, element: &gst_base::BaseSrc) -> bool { impl BaseSrcImpl for Box<$name> {
let imp: &BaseSrcImpl = self.as_ref(); fn start(&self, element: &gst_base::BaseSrc) -> bool {
imp.stop(element) let imp: &$name = self.as_ref();
} imp.start(element)
}
fn is_seekable(&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.is_seekable(element) imp.stop(element)
} }
fn get_size(&self, element: &gst_base::BaseSrc) -> Option<u64> { fn is_seekable(&self, element: &gst_base::BaseSrc) -> bool {
let imp: &BaseSrcImpl = self.as_ref(); let imp: &$name = self.as_ref();
imp.get_size(element) imp.is_seekable(element)
} }
fn fill( fn get_size(&self, element: &gst_base::BaseSrc) -> Option<u64> {
&self, let imp: &$name = self.as_ref();
element: &gst_base::BaseSrc, imp.get_size(element)
offset: u64, }
length: u32,
buffer: &mut gst::BufferRef,
) -> gst::FlowReturn {
let imp: &BaseSrcImpl = self.as_ref();
imp.fill(element, offset, length, buffer)
}
fn do_seek(&self, element: &gst_base::BaseSrc, segment: &mut gst::Segment) -> bool { fn fill(
let imp: &BaseSrcImpl = self.as_ref(); &self,
imp.do_seek(element, segment) element: &gst_base::BaseSrc,
} offset: u64,
length: u32,
buffer: &mut gst::BufferRef,
) -> gst::FlowReturn {
let imp: &$name = self.as_ref();
imp.fill(element, offset, length, buffer)
}
fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> 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.query(element, query) imp.do_seek(element, segment)
} }
fn event(&self, element: &gst_base::BaseSrc, event: &gst::Event) -> bool {
let imp: &BaseSrcImpl = self.as_ref();
imp.event(element, event)
}
}
// FIXME: Boilerplate fn query(&self, element: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool {
impl ElementImpl for Box<BaseSrcImpl> { let imp: &$name = self.as_ref();
fn change_state( imp.query(element, query)
&self, }
element: &gst::Element, fn event(&self, element: &gst_base::BaseSrc, event: &gst::Event) -> bool {
transition: gst::StateChange, let imp: &$name = self.as_ref();
) -> gst::StateChangeReturn { imp.event(element, event)
let imp: &BaseSrcImpl = self.as_ref(); }
imp.change_state(element, transition) }
} };
} );
box_base_src_impl!(BaseSrcImpl);
// FIXME: Boilerplate
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(); fn change_state(
imp.get_property(obj, id) &self,
} element: &gst::Element,
} transition: gst::StateChange,
) -> gst::StateChangeReturn {
let imp: &$name = self.as_ref();
imp.change_state(element, transition)
}
}
};
);
impl ElementImpl for Box<ElementImpl> { box_element_impl!(ElementImpl);
fn change_state(
&self,
element: &gst::Element,
transition: gst::StateChange,
) -> gst::StateChangeReturn {
let imp: &ElementImpl = self.as_ref();
imp.change_state(element, transition)
}
}
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;