use std::ops::{Deref, DerefMut};
use std::ptr;
use winapi::um::unknwnbase::IUnknown;
use winapi::shared::guiddef::REFIID;
use winapi::shared::winerror::{S_OK, E_NOINTERFACE};
#[derive(Debug)]
pub struct ComPtr<T> {
ptr: *mut T,
}
impl<T> ComPtr<T> {
pub fn new() -> Self {
ComPtr { ptr: ptr::null_mut() }
}
pub fn from_ptr(ptr: *mut T) -> Self {
unsafe {
if !ptr.is_null() {
(*(ptr as *mut IUnknown)).AddRef();
}
}
ComPtr { ptr: ptr }
}
pub unsafe fn already_addrefed(ptr: *mut T) -> Self {
ComPtr { ptr: ptr }
}
pub unsafe fn getter_addrefs<Q>(&mut self) -> *mut *mut Q {
self.release();
return &mut self.ptr as *mut *mut _ as *mut *mut Q;
}
pub fn as_ptr(&self) -> *mut T {
self.ptr
}
pub fn query_interface<Q>(&self, iid: REFIID) -> Option<ComPtr<Q>> {
if self.ptr.is_null() {
return None;
}
unsafe {
let mut p = ComPtr::<Q>::new();
let hr = (*(self.ptr as *mut IUnknown)).QueryInterface(iid, p.getter_addrefs());
if hr == S_OK {
return Some(p);
}
assert!(hr == E_NOINTERFACE);
return None;
}
}
pub fn addref(&self) {
unsafe {
assert!(!self.ptr.is_null());
(*(self.ptr as *mut IUnknown)).AddRef();
}
}
pub unsafe fn release(&self) {
if !self.ptr.is_null() {
(*(self.ptr as *mut IUnknown)).Release();
}
}
pub fn forget(&mut self) -> *mut T {
let ptr = self.ptr;
self.ptr = ptr::null_mut();
ptr
}
pub fn is_null(&self) -> bool {
self.ptr.is_null()
}
}
impl<T> Clone for ComPtr<T> {
fn clone(&self) -> Self {
if !self.ptr.is_null() {
self.addref();
}
ComPtr { ptr: self.ptr }
}
}
impl<T> Deref for ComPtr<T> {
type Target = T;
fn deref(&self) -> &T {
assert!(!self.ptr.is_null());
unsafe { &mut *self.ptr }
}
}
impl<T> DerefMut for ComPtr<T> {
fn deref_mut(&mut self) -> &mut T {
assert!(!self.ptr.is_null());
unsafe { &mut *self.ptr }
}
}
impl<T> PartialEq for ComPtr<T> {
fn eq(&self, other: &ComPtr<T>) -> bool {
self.ptr == other.ptr
}
}
impl<T> Drop for ComPtr<T> {
fn drop(&mut self) {
unsafe {
self.release();
}
}
}
unsafe impl<T> Send for ComPtr<T> {}