// Copyright (C) 2016-2017 Sebastian Dröge // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use std::os::raw::c_void; use std::{fmt, ops, borrow, ptr}; use std::marker::PhantomData; use utils::*; #[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct GstRc { obj: T, } impl GstRc { unsafe fn new(obj: T, owned: bool) -> Self { extern "C" { fn gst_mini_object_ref(obj: *mut c_void) -> *mut c_void; } assert!(!obj.as_ptr().is_null()); if !owned { gst_mini_object_ref(obj.as_ptr()); } GstRc { obj: obj } } pub unsafe fn new_from_owned_ptr(ptr: *mut c_void) -> Self { Self::new(T::new_from_ptr(ptr), true) } pub unsafe fn new_from_unowned_ptr(ptr: *mut c_void) -> Self { Self::new(T::new_from_ptr(ptr), false) } pub fn make_mut(&mut self) -> &mut T { extern "C" { fn gst_mini_object_make_writable(obj: *mut c_void) -> *mut c_void; } unsafe { let ptr = self.obj.as_ptr(); if self.is_writable() { return &mut self.obj; } self.obj.replace_ptr(gst_mini_object_make_writable(ptr)); 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 { extern "C" { fn gst_mini_object_copy(obj: *const c_void) -> *mut c_void; } unsafe { GstRc::new_from_owned_ptr(gst_mini_object_copy(self.obj.as_ptr())) } } fn is_writable(&self) -> bool { extern "C" { fn gst_mini_object_is_writable(obj: *mut c_void) -> GBoolean; } unsafe { gst_mini_object_is_writable(self.as_ptr()).to_bool() } } pub unsafe fn into_ptr(mut self) -> *mut c_void { self.obj.swap_ptr(ptr::null_mut()) } } impl ops::Deref for GstRc { type Target = T; fn deref(&self) -> &T { &self.obj } } impl AsRef for GstRc { fn as_ref(&self) -> &T { &self.obj } } impl borrow::Borrow for GstRc { fn borrow(&self) -> &T { &self.obj } } impl Clone for GstRc { fn clone(&self) -> GstRc { unsafe { GstRc::new_from_unowned_ptr(self.obj.as_ptr()) } } } impl Drop for GstRc { fn drop(&mut self) { extern "C" { fn gst_mini_object_unref(obj: *mut c_void) -> *mut c_void; } unsafe { if !self.obj.as_ptr().is_null() { gst_mini_object_unref(self.obj.as_ptr()); } } } } unsafe impl Sync for GstRc {} unsafe impl Send for GstRc {} impl fmt::Display for GstRc { 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 { unsafe fn as_ptr(&self) -> *mut c_void; unsafe fn replace_ptr(&mut self, ptr: *mut c_void); unsafe fn swap_ptr(&mut self, new_ptr: *mut c_void) -> *mut c_void { let ptr = self.as_ptr(); self.replace_ptr(new_ptr); ptr } unsafe fn new_from_ptr(ptr: *mut c_void) -> Self; } impl<'a, T: MiniObject> From<&'a T> for GstRc { fn from(f: &'a T) -> GstRc { unsafe { GstRc::new_from_unowned_ptr(f.as_ptr()) } } } impl<'a, T: MiniObject> From<&'a mut T> for GstRc { fn from(f: &'a mut T) -> GstRc { unsafe { GstRc::new_from_unowned_ptr(f.as_ptr()) } } } #[repr(C)] pub struct GstRefPtr(*mut c_void); #[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct GstRef<'a, T: MiniObject> { obj: T, #[allow(dead_code)] phantom: PhantomData<&'a GstRefPtr>, } impl<'a, T: MiniObject> GstRef<'a, T> { pub unsafe fn new(ptr: &'a GstRefPtr) -> GstRef<'a, T> { GstRef { obj: T::new_from_ptr(ptr.0), 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 { extern "C" { fn gst_mini_object_copy(obj: *const c_void) -> *mut c_void; } unsafe { GstRc::new_from_owned_ptr(gst_mini_object_copy(self.obj.as_ptr())) } } fn is_writable(&self) -> bool { extern "C" { fn gst_mini_object_is_writable(obj: *mut c_void) -> GBoolean; } unsafe { gst_mini_object_is_writable(self.as_ptr()).to_bool() } } pub unsafe fn into_ptr(mut self) -> *mut c_void { 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 for GstRef<'a, T> { fn as_ref(&self) -> &T { &self.obj } } impl<'a, T: MiniObject> borrow::Borrow 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) } }