dx_core/
unknown.rs

1/*
2 * Copyright 2015 Joshua R. Rodgers
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #![macro_use]
17
18use std::ptr::null_mut;
19use iid::HasIID;
20use com_ref::ComRef;
21use com_object::ComObject;
22
23#[repr(C)]
24pub struct UnknownVtable {
25    query_interface: extern "stdcall" fn(*const Unknown, *const ::iid::IID, *mut *const Unknown) -> i32,
26    add_ref: extern "stdcall" fn(*const Unknown) -> u32,
27    release: extern "stdcall" fn(*const Unknown) -> u32
28}
29
30#[repr(C)]
31pub struct Unknown {
32    vtable: *const UnknownVtable
33}
34
35impl_iid! { Unknown : 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }
36impl_com_object! { Unknown : UnknownVtable }
37unsafe impl Send for Unknown {}
38
39pub trait IUnknown {
40    fn query_interface<T>(&self) -> ::com::HResult<ComRef<T>> where T: IUnknown + HasIID;
41
42    #[doc(hidden)]
43    fn add_ref(&self) -> u32;
44
45    #[doc(hidden)]
46    fn release(&self) -> u32;
47}
48
49impl<TComObject: ComObject<UnknownVtable>> IUnknown for TComObject {
50    fn query_interface<T>(&self) -> ::com::HResult<ComRef<T>> where T: IUnknown + HasIID {
51        let mut interface: *const Unknown = null_mut();
52        let result = (self.vtable().query_interface)(self.this(), <T as HasIID>::iid(), &mut interface);
53
54        wrap_result! { result => ComRef::new(interface as *const T) }
55    }
56
57    fn add_ref(&self) -> u32 {
58        (self.vtable().add_ref)(self.this())
59    }
60
61    fn release(&self) -> u32 {
62        (self.vtable().release)(self.this())
63    }
64}