1 // Licensed under the Apache License, Version 2.0
2 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4 // All files in the project carrying such notice may not be copied, modified, or distributed
5 // except according to those terms.
6 use std::fmt::{Debug, Error as FmtError, Formatter};
7 use std::mem::forget;
8 use std::ops::Deref;
9 use std::ptr::{NonNull, null_mut};
10 use winapi::Interface;
11 use winapi::um::unknwnbase::IUnknown;
12 
13 // ComPtr to wrap COM interfaces sanely
14 #[repr(transparent)]
15 pub struct ComPtr<T>(NonNull<T>);
16 impl<T> ComPtr<T> {
17     /// Creates a `ComPtr` to wrap a raw pointer.
18     /// It takes ownership over the pointer which means it does __not__ call `AddRef`.
19     /// `T` __must__ be a COM interface that inherits from `IUnknown`.
from_raw(ptr: *mut T) -> ComPtr<T> where T: Interface20     pub unsafe fn from_raw(ptr: *mut T) -> ComPtr<T> where T: Interface {
21         ComPtr(NonNull::new(ptr).expect("ptr should not be null"))
22     }
23     /// Casts up the inheritance chain
up<U>(self) -> ComPtr<U> where T: Deref<Target=U>, U: Interface24     pub fn up<U>(self) -> ComPtr<U> where T: Deref<Target=U>, U: Interface {
25         unsafe { ComPtr::from_raw(self.into_raw() as *mut U) }
26     }
27     /// Extracts the raw pointer.
28     /// You are now responsible for releasing it yourself.
into_raw(self) -> *mut T29     pub fn into_raw(self) -> *mut T {
30         let p = self.0.as_ptr();
31         forget(self);
32         p
33     }
34     /// For internal use only.
as_unknown(&self) -> &IUnknown35     fn as_unknown(&self) -> &IUnknown {
36         unsafe { &*(self.as_raw() as *mut IUnknown) }
37     }
38     /// Performs QueryInterface fun.
cast<U>(&self) -> Result<ComPtr<U>, i32> where U: Interface39     pub fn cast<U>(&self) -> Result<ComPtr<U>, i32> where U: Interface {
40         let mut obj = null_mut();
41         let err = unsafe { self.as_unknown().QueryInterface(&U::uuidof(), &mut obj) };
42         if err < 0 { return Err(err); }
43         Ok(unsafe { ComPtr::from_raw(obj as *mut U) })
44     }
45     /// Obtains the raw pointer without transferring ownership.
46     /// Do __not__ release this pointer because it is still owned by the `ComPtr`.
as_raw(&self) -> *mut T47     pub fn as_raw(&self) -> *mut T {
48         self.0.as_ptr()
49     }
50 }
51 impl<T> Deref for ComPtr<T> {
52     type Target = T;
deref(&self) -> &T53     fn deref(&self) -> &T {
54         unsafe { &*self.as_raw() }
55     }
56 }
57 impl<T> Clone for ComPtr<T> where T: Interface {
clone(&self) -> Self58     fn clone(&self) -> Self {
59         unsafe {
60             self.as_unknown().AddRef();
61             ComPtr::from_raw(self.as_raw())
62         }
63     }
64 }
65 impl<T> Debug for ComPtr<T> {
fmt(&self, f: &mut Formatter) -> Result<(), FmtError>66     fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
67         write!(f, "{:?}", self.0)
68     }
69 }
70 impl<T> Drop for ComPtr<T> {
drop(&mut self)71     fn drop(&mut self) {
72         unsafe { self.as_unknown().Release(); }
73     }
74 }
75 impl<T> PartialEq<ComPtr<T>> for ComPtr<T> where T: Interface {
eq(&self, other: &ComPtr<T>) -> bool76     fn eq(&self, other: &ComPtr<T>) -> bool {
77         self.0 == other.0
78     }
79 }
80