1
0
Fork 0

Add VST3 smart pointers for regular objects

This commit is contained in:
Robbert van der Helm 2022-02-16 20:23:22 +01:00
parent 7fb1547f95
commit 95f0183d27

View file

@ -1,4 +1,6 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::ops::Deref;
use vst3_sys::{interfaces::IUnknown, ComInterface};
use crate::wrapper::util::hash_param_id; use crate::wrapper::util::hash_param_id;
@ -33,7 +35,14 @@ pub struct VstPtr<T: vst3_sys::ComInterface + ?Sized> {
ptr: vst3_sys::VstPtr<T>, ptr: vst3_sys::VstPtr<T>,
} }
impl<T: vst3_sys::ComInterface + ?Sized> std::ops::Deref for VstPtr<T> { /// The same as [VstPtr] with shared semnatics, but for objects we defined ourself since VstPtr only
/// works for interfaces.
#[repr(transparent)]
pub struct ObjectPtr<T: IUnknown> {
ptr: *const T,
}
impl<T: ComInterface + ?Sized> Deref for VstPtr<T> {
type Target = vst3_sys::VstPtr<T>; type Target = vst3_sys::VstPtr<T>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -41,13 +50,38 @@ impl<T: vst3_sys::ComInterface + ?Sized> std::ops::Deref for VstPtr<T> {
} }
} }
impl<T: IUnknown> Deref for ObjectPtr<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*self.ptr }
}
}
impl<T: vst3_sys::ComInterface + ?Sized> From<vst3_sys::VstPtr<T>> for VstPtr<T> { impl<T: vst3_sys::ComInterface + ?Sized> From<vst3_sys::VstPtr<T>> for VstPtr<T> {
fn from(ptr: vst3_sys::VstPtr<T>) -> Self { fn from(ptr: vst3_sys::VstPtr<T>) -> Self {
Self { ptr } Self { ptr }
} }
} }
impl<T: IUnknown> Drop for ObjectPtr<T> {
fn drop(&mut self) {
unsafe { (*self).release() };
}
}
/// SAFETY: Sharing these pointers across thread is s safe as they have internal atomic reference /// SAFETY: Sharing these pointers across thread is s safe as they have internal atomic reference
/// counting, so as long as a `VstPtr<T>` handle exists the object will stay alive. /// counting, so as long as a `VstPtr<T>` handle exists the object will stay alive.
unsafe impl<T: vst3_sys::ComInterface + ?Sized> Send for VstPtr<T> {} unsafe impl<T: ComInterface + ?Sized> Send for VstPtr<T> {}
unsafe impl<T: vst3_sys::ComInterface + ?Sized> Sync for VstPtr<T> {} unsafe impl<T: ComInterface + ?Sized> Sync for VstPtr<T> {}
unsafe impl<T: IUnknown> Send for ObjectPtr<T> {}
unsafe impl<T: IUnknown> Sync for ObjectPtr<T> {}
impl<T: IUnknown> ObjectPtr<T> {
/// Create a smart pointer for an existing reference counted object.
pub fn new(obj: &T) -> Self {
unsafe { obj.add_ref() };
Self { ptr: obj }
}
}