/** * This file has no copyright assigned and is placed in the Public Domain. * This file is part of the mingw-w64 runtime package. * No warranty is given; refer to the file DISCLAIMER.PD within this package. */ #ifndef _WRL_CLIENT_H_ #define _WRL_CLIENT_H_ #include #include /* #include */ #include /* #include */ #include namespace Microsoft { namespace WRL { namespace Details { template class ComPtrRefBase { protected: T* ptr_; public: typedef typename T::InterfaceType InterfaceType; #ifndef __WRL_CLASSIC_COM__ operator IInspectable**() const throw() { static_assert(__is_base_of(IInspectable, InterfaceType), "Invalid cast"); return reinterpret_cast(ptr_->ReleaseAndGetAddressOf()); } #endif operator IUnknown**() const throw() { static_assert(__is_base_of(IUnknown, InterfaceType), "Invalid cast"); return reinterpret_cast(ptr_->ReleaseAndGetAddressOf()); } }; template class ComPtrRef : public Details::ComPtrRefBase { public: ComPtrRef(T *ptr) throw() { ComPtrRefBase::ptr_ = ptr; } operator void**() const throw() { return reinterpret_cast(ComPtrRefBase::ptr_->ReleaseAndGetAddressOf()); } operator T*() throw() { *ComPtrRefBase::ptr_ = nullptr; return ComPtrRefBase::ptr_; } operator typename ComPtrRefBase::InterfaceType**() throw() { return ComPtrRefBase::ptr_->ReleaseAndGetAddressOf(); } typename ComPtrRefBase::InterfaceType *operator*() throw() { return ComPtrRefBase::ptr_->Get(); } typename ComPtrRefBase::InterfaceType *const *GetAddressOf() const throw() { return ComPtrRefBase::ptr_->GetAddressOf(); } typename ComPtrRefBase::InterfaceType **ReleaseAndGetAddressOf() throw() { return ComPtrRefBase::ptr_->ReleaseAndGetAddressOf(); } }; } template class ComPtr { public: typedef T InterfaceType; ComPtr() throw() : ptr_(nullptr) {} ComPtr(decltype(nullptr)) throw() : ptr_(nullptr) {} template ComPtr(U *other) throw() : ptr_(other) { InternalAddRef(); } ComPtr(const ComPtr &other) throw() : ptr_(other.ptr_) { InternalAddRef(); } template ComPtr(const ComPtr &other) throw() : ptr_(other.Get()) { InternalAddRef(); } ComPtr(ComPtr &&other) throw() : ptr_(nullptr) { if(this != reinterpret_cast(&reinterpret_cast(other))) Swap(other); } template ComPtr(ComPtr&& other) throw() : ptr_(other.Detach()) {} ~ComPtr() throw() { InternalRelease(); } ComPtr &operator=(decltype(nullptr)) throw() { InternalRelease(); return *this; } ComPtr &operator=(InterfaceType *other) throw() { if (ptr_ != other) { InternalRelease(); ptr_ = other; InternalAddRef(); } return *this; } template ComPtr &operator=(U *other) throw() { if (ptr_ != other) { InternalRelease(); ptr_ = other; InternalAddRef(); } return *this; } ComPtr& operator=(const ComPtr &other) throw() { if (ptr_ != other.ptr_) ComPtr(other).Swap(*this); return *this; } template ComPtr &operator=(const ComPtr &other) throw() { ComPtr(other).Swap(*this); return *this; } ComPtr& operator=(ComPtr &&other) throw() { ComPtr(other).Swap(*this); return *this; } template ComPtr& operator=(ComPtr &&other) throw() { ComPtr(other).Swap(*this); return *this; } void Swap(ComPtr &&r) throw() { InterfaceType *tmp = ptr_; ptr_ = r.ptr_; r.ptr_ = tmp; } void Swap(ComPtr &r) throw() { InterfaceType *tmp = ptr_; ptr_ = r.ptr_; r.ptr_ = tmp; } operator Details::BoolType() const throw() { return Get() != nullptr ? &Details::BoolStruct::Member : nullptr; } InterfaceType *Get() const throw() { return ptr_; } InterfaceType *operator->() const throw() { return ptr_; } Details::ComPtrRef> operator&() throw() { return Details::ComPtrRef>(this); } const Details::ComPtrRef> operator&() const throw() { return Details::ComPtrRef>(this); } InterfaceType *const *GetAddressOf() const throw() { return &ptr_; } InterfaceType **GetAddressOf() throw() { return &ptr_; } InterfaceType **ReleaseAndGetAddressOf() throw() { InternalRelease(); return &ptr_; } InterfaceType *Detach() throw() { T* ptr = ptr_; ptr_ = nullptr; return ptr; } void Attach(InterfaceType *other) throw() { if (ptr_ != other) { InternalRelease(); ptr_ = other; InternalAddRef(); } } unsigned long Reset() { return InternalRelease(); } HRESULT CopyTo(InterfaceType **ptr) const throw() { InternalAddRef(); *ptr = ptr_; return S_OK; } HRESULT CopyTo(REFIID riid, void **ptr) const throw() { return ptr_->QueryInterface(riid, ptr); } template HRESULT CopyTo(U **ptr) const throw() { return ptr_->QueryInterface(__uuidof(U), reinterpret_cast(ptr)); } template HRESULT As(Details::ComPtrRef> p) const throw() { return ptr_->QueryInterface(__uuidof(U), p); } template HRESULT As(ComPtr *p) const throw() { return ptr_->QueryInterface(__uuidof(U), reinterpret_cast(p->ReleaseAndGetAddressOf())); } HRESULT AsIID(REFIID riid, ComPtr *p) const throw() { return ptr_->QueryInterface(riid, reinterpret_cast(p->ReleaseAndGetAddressOf())); } /* HRESULT AsWeak(WeakRef *pWeakRef) const throw() { return ::Microsoft::WRL::AsWeak(ptr_, pWeakRef); } */ protected: InterfaceType *ptr_; void InternalAddRef() const throw() { if(ptr_) ptr_->AddRef(); } unsigned long InternalRelease() throw() { InterfaceType *tmp = ptr_; if(!tmp) return 0; ptr_ = nullptr; return tmp->Release(); } }; } } template void **IID_PPV_ARGS_Helper(::Microsoft::WRL::Details::ComPtrRef pp) throw() { static_assert(__is_base_of(IUnknown, typename T::InterfaceType), "Expected COM interface"); return pp; } namespace Windows { namespace Foundation { template inline HRESULT ActivateInstance(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef instance) throw() { return ActivateInstance(classid, instance.ReleaseAndGetAddressOf()); } template inline HRESULT GetActivationFactory(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef factory) throw() { return RoGetActivationFactory(classid, IID_INS_ARGS(factory.ReleaseAndGetAddressOf())); } } } namespace ABI { namespace Windows { namespace Foundation { template inline HRESULT ActivateInstance(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef instance) throw() { return ActivateInstance(classid, instance.ReleaseAndGetAddressOf()); } template inline HRESULT GetActivationFactory(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef factory) throw() { return RoGetActivationFactory(classid, IID_INS_ARGS(factory.ReleaseAndGetAddressOf())); } } } } #endif