2017-02-16 15:52:27 +00:00
|
|
|
// Copyright (C) 2016-2017 Sebastian Dröge <sebastian@centricular.com>
|
2017-02-03 14:34:17 +00:00
|
|
|
//
|
2017-02-16 15:52:27 +00:00
|
|
|
// 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.
|
2017-02-03 14:34:17 +00:00
|
|
|
|
|
|
|
use std::{fmt, ops, borrow, ptr};
|
|
|
|
use std::marker::PhantomData;
|
2017-04-09 21:29:07 +00:00
|
|
|
|
|
|
|
use glib;
|
|
|
|
use gst;
|
2017-02-03 14:34:17 +00:00
|
|
|
|
|
|
|
#[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct GstRc<T: MiniObject> {
|
|
|
|
obj: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: MiniObject> GstRc<T> {
|
|
|
|
unsafe fn new(obj: T, owned: bool) -> Self {
|
|
|
|
assert!(!obj.as_ptr().is_null());
|
|
|
|
|
|
|
|
if !owned {
|
2017-04-09 21:29:07 +00:00
|
|
|
gst::gst_mini_object_ref(obj.as_ptr() as *mut gst::GstMiniObject);
|
2017-02-03 14:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstRc { obj: obj }
|
|
|
|
}
|
|
|
|
|
2017-04-09 21:29:07 +00:00
|
|
|
pub unsafe fn new_from_owned_ptr(ptr: *mut T::PtrType) -> Self {
|
2017-02-03 14:34:17 +00:00
|
|
|
Self::new(T::new_from_ptr(ptr), true)
|
|
|
|
}
|
|
|
|
|
2017-04-09 21:29:07 +00:00
|
|
|
pub unsafe fn new_from_unowned_ptr(ptr: *mut T::PtrType) -> Self {
|
2017-02-03 14:34:17 +00:00
|
|
|
Self::new(T::new_from_ptr(ptr), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_mut(&mut self) -> &mut T {
|
|
|
|
unsafe {
|
|
|
|
let ptr = self.obj.as_ptr();
|
|
|
|
|
|
|
|
if self.is_writable() {
|
|
|
|
return &mut self.obj;
|
|
|
|
}
|
|
|
|
|
2017-04-12 13:46:11 +00:00
|
|
|
self.obj
|
|
|
|
.replace_ptr(gst::gst_mini_object_make_writable(ptr as *mut gst::GstMiniObject) as
|
|
|
|
*mut T::PtrType);
|
2017-02-03 14:34:17 +00:00
|
|
|
assert!(self.is_writable());
|
|
|
|
|
|
|
|
&mut self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut(&mut self) -> Option<&mut T> {
|
|
|
|
if self.is_writable() {
|
|
|
|
Some(&mut self.obj)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy(&self) -> Self {
|
2017-04-09 21:29:07 +00:00
|
|
|
unsafe {
|
|
|
|
GstRc::new_from_owned_ptr(gst::gst_mini_object_copy(self.obj.as_ptr() as
|
|
|
|
*const gst::GstMiniObject) as
|
|
|
|
*mut T::PtrType)
|
2017-02-03 14:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:52:38 +00:00
|
|
|
fn is_writable(&self) -> bool {
|
2017-04-09 21:29:07 +00:00
|
|
|
(unsafe {
|
|
|
|
gst::gst_mini_object_is_writable(self.as_ptr() as *const gst::GstMiniObject)
|
|
|
|
} == glib::GTRUE)
|
2017-04-05 20:52:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 21:29:07 +00:00
|
|
|
pub unsafe fn into_ptr(mut self) -> *mut T::PtrType {
|
2017-02-03 14:34:17 +00:00
|
|
|
self.obj.swap_ptr(ptr::null_mut())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: MiniObject> ops::Deref for GstRc<T> {
|
|
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: MiniObject> AsRef<T> for GstRc<T> {
|
|
|
|
fn as_ref(&self) -> &T {
|
|
|
|
&self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: MiniObject> borrow::Borrow<T> for GstRc<T> {
|
|
|
|
fn borrow(&self) -> &T {
|
|
|
|
&self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: MiniObject> Clone for GstRc<T> {
|
|
|
|
fn clone(&self) -> GstRc<T> {
|
|
|
|
unsafe { GstRc::new_from_unowned_ptr(self.obj.as_ptr()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: MiniObject> Drop for GstRc<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
if !self.obj.as_ptr().is_null() {
|
2017-04-09 21:29:07 +00:00
|
|
|
gst::gst_mini_object_unref(self.obj.as_ptr() as *mut gst::GstMiniObject);
|
2017-02-03 14:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: MiniObject + Sync> Sync for GstRc<T> {}
|
|
|
|
unsafe impl<T: MiniObject + Send> Send for GstRc<T> {}
|
|
|
|
|
|
|
|
impl<T: MiniObject + fmt::Display> fmt::Display for GstRc<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.obj.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Reference counting must not happen in here
|
|
|
|
pub unsafe trait MiniObject {
|
2017-04-09 21:29:07 +00:00
|
|
|
type PtrType;
|
|
|
|
|
|
|
|
unsafe fn as_ptr(&self) -> *mut Self::PtrType;
|
|
|
|
unsafe fn replace_ptr(&mut self, ptr: *mut Self::PtrType);
|
|
|
|
unsafe fn swap_ptr(&mut self, new_ptr: *mut Self::PtrType) -> *mut Self::PtrType {
|
2017-02-03 14:34:17 +00:00
|
|
|
let ptr = self.as_ptr();
|
|
|
|
self.replace_ptr(new_ptr);
|
|
|
|
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
|
2017-04-09 21:29:07 +00:00
|
|
|
unsafe fn new_from_ptr(ptr: *mut Self::PtrType) -> Self;
|
2017-02-03 14:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject> From<&'a T> for GstRc<T> {
|
|
|
|
fn from(f: &'a T) -> GstRc<T> {
|
|
|
|
unsafe { GstRc::new_from_unowned_ptr(f.as_ptr()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject> From<&'a mut T> for GstRc<T> {
|
|
|
|
fn from(f: &'a mut T) -> GstRc<T> {
|
|
|
|
unsafe { GstRc::new_from_unowned_ptr(f.as_ptr()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
2017-04-09 21:29:07 +00:00
|
|
|
pub struct GstRef<'a, T: 'a + MiniObject> {
|
2017-02-03 14:34:17 +00:00
|
|
|
obj: T,
|
|
|
|
#[allow(dead_code)]
|
2017-04-17 08:29:28 +00:00
|
|
|
phantom: PhantomData<&'a T>,
|
2017-02-03 14:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject> GstRef<'a, T> {
|
2017-04-17 08:29:28 +00:00
|
|
|
pub unsafe fn new(ptr: *mut T::PtrType) -> GstRef<'a, T> {
|
2017-02-03 14:34:17 +00:00
|
|
|
GstRef {
|
2017-04-17 08:29:28 +00:00
|
|
|
obj: T::new_from_ptr(ptr),
|
2017-02-03 14:34:17 +00:00
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut(&mut self) -> Option<&mut T> {
|
|
|
|
if self.is_writable() {
|
|
|
|
Some(&mut self.obj)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy(&self) -> GstRc<T> {
|
2017-04-09 21:29:07 +00:00
|
|
|
unsafe {
|
|
|
|
GstRc::new_from_owned_ptr(gst::gst_mini_object_copy(self.obj.as_ptr() as
|
|
|
|
*const gst::GstMiniObject) as
|
|
|
|
*mut T::PtrType)
|
2017-02-03 14:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:52:38 +00:00
|
|
|
fn is_writable(&self) -> bool {
|
2017-04-09 21:29:07 +00:00
|
|
|
(unsafe {
|
|
|
|
gst::gst_mini_object_is_writable(self.as_ptr() as *const gst::GstMiniObject)
|
|
|
|
} == glib::GTRUE)
|
2017-04-05 20:52:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 21:29:07 +00:00
|
|
|
pub unsafe fn into_ptr(mut self) -> *mut T::PtrType {
|
2017-02-03 14:34:17 +00:00
|
|
|
self.obj.swap_ptr(ptr::null_mut())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject> ops::Deref for GstRef<'a, T> {
|
|
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject> AsRef<T> for GstRef<'a, T> {
|
|
|
|
fn as_ref(&self) -> &T {
|
|
|
|
&self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject> borrow::Borrow<T> for GstRef<'a, T> {
|
|
|
|
fn borrow(&self) -> &T {
|
|
|
|
&self.obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: MiniObject + fmt::Display> fmt::Display for GstRef<'a, T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.obj.fmt(f)
|
|
|
|
}
|
|
|
|
}
|