use std::ops::Deref;
use crate::co;
use crate::decl::*;
use crate::kernel::privs::*;
use crate::ole::ffi;
use crate::prelude::*;
pub struct CoLockObjectExternalGuard<'a, T>
where
T: ole_IUnknown,
{
com_obj: &'a T,
}
impl<'a, T> Drop for CoLockObjectExternalGuard<'a, T>
where
T: ole_IUnknown,
{
fn drop(&mut self) {
unsafe {
ffi::CoLockObjectExternal(self.com_obj.ptr(), 0, 1); }
}
}
impl<'a, T> CoLockObjectExternalGuard<'a, T>
where
T: ole_IUnknown,
{
#[must_use]
pub const unsafe fn new(com_obj: &'a T) -> Self {
Self { com_obj }
}
}
pub struct CoTaskMemFreeGuard {
pmem: *mut std::ffi::c_void,
sz: usize,
}
impl Drop for CoTaskMemFreeGuard {
fn drop(&mut self) {
if !self.pmem.is_null() {
unsafe {
ffi::CoTaskMemFree(self.pmem);
}
}
}
}
impl CoTaskMemFreeGuard {
#[must_use]
pub const unsafe fn new(pmem: *mut std::ffi::c_void, sz: usize) -> Self {
Self { pmem, sz }
}
#[must_use]
pub fn leak(&mut self) -> (*mut std::ffi::c_void, usize) {
(
std::mem::replace(&mut self.pmem, std::ptr::null_mut()),
std::mem::replace(&mut self.sz, 0),
)
}
pub_fn_mem_block!();
}
pub struct CoUninitializeGuard {
hr: co::HRESULT,
}
impl Drop for CoUninitializeGuard {
fn drop(&mut self) {
unsafe {
ffi::CoUninitialize();
}
}
}
impl CoUninitializeGuard {
#[must_use]
pub const unsafe fn new(hr: co::HRESULT) -> Self {
Self { hr }
}
#[must_use]
pub const fn hr(&self) -> co::HRESULT {
self.hr
}
}
pub struct OleUninitializeGuard {}
impl Drop for OleUninitializeGuard {
fn drop(&mut self) {
unsafe {
ffi::OleUninitialize();
}
}
}
impl OleUninitializeGuard {
#[must_use]
pub const unsafe fn new() -> Self {
Self {}
}
}
pub struct ReleaseStgMediumGuard {
stgm: STGMEDIUM,
}
impl Drop for ReleaseStgMediumGuard {
fn drop(&mut self) {
unsafe {
ffi::ReleaseStgMedium(pvoid(&mut self.stgm));
}
}
}
impl Deref for ReleaseStgMediumGuard {
type Target = STGMEDIUM;
fn deref(&self) -> &Self::Target {
&self.stgm
}
}
impl ReleaseStgMediumGuard {
#[must_use]
pub const unsafe fn new(stgm: STGMEDIUM) -> Self {
Self { stgm }
}
}