From e3f1d722e7fc73fd5a40d29dcd35855e667fbb72 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Tue, 23 Sep 2014 19:31:55 +0200 Subject: [PATCH] Remove ATL dependency from MSMF capture code Use _com_ptr_t instead of CComPtr in ComPtr wrapper to avoid ATL dependency. --- modules/highgui/src/cap_msmf.cpp | 4 +- modules/highgui/src/cap_msmf.hpp | 113 ++++++++++++++++++++++--------- 2 files changed, 83 insertions(+), 34 deletions(-) diff --git a/modules/highgui/src/cap_msmf.cpp b/modules/highgui/src/cap_msmf.cpp index 2645649770..e04ee258d6 100644 --- a/modules/highgui/src/cap_msmf.cpp +++ b/modules/highgui/src/cap_msmf.cpp @@ -72,6 +72,7 @@ #include #include +#ifdef _MSC_VER #pragma warning(disable:4503) #pragma comment(lib, "mfplat") #pragma comment(lib, "mf") @@ -81,6 +82,7 @@ #if (WINVER >= 0x0602) // Available since Win 8 #pragma comment(lib, "MinCore_Downlevel") #endif +#endif #include @@ -260,7 +262,7 @@ public: #include "ppltasks_winrt.h" #endif #else -#include +#include #endif struct IMFMediaType; diff --git a/modules/highgui/src/cap_msmf.hpp b/modules/highgui/src/cap_msmf.hpp index 1624177b28..c1b3f94b70 100644 --- a/modules/highgui/src/cap_msmf.hpp +++ b/modules/highgui/src/cap_msmf.hpp @@ -600,27 +600,65 @@ hr = orig.As(&obj); #define _ComPtr Microsoft::WRL::ComPtr #else +#define _COM_SMARTPTR_DECLARE(T,var) T ## Ptr var + template -class ComPtr : public ATL::CComPtr +class ComPtr { public: ComPtr() throw() { } - ComPtr(int nNull) throw() : - CComPtr((T*)nNull) + ComPtr(int nNull) throw() + { + assert(nNull == 0); + p = NULL; + } + ComPtr(T* lp) throw() + { + p = lp; + } + ComPtr(_In_ const ComPtr& lp) throw() + { + p = lp.p; + } + virtual ~ComPtr() { } - ComPtr(T* lp) throw() : - CComPtr(lp) + T** operator&() throw() { + assert(p == NULL); + return p.operator&(); } - ComPtr(_In_ const CComPtr& lp) throw() : - CComPtr(lp.p) + T* operator->() const throw() { + assert(p != NULL); + return p.operator->(); + } + bool operator!() const throw() + { + return p.operator==(NULL); + } + bool operator==(_In_opt_ T* pT) const throw() + { + return p.operator==(pT); + } + // For comparison to NULL + bool operator==(int nNull) const + { + assert(nNull == 0); + return p.operator==(NULL); + } + + bool operator!=(_In_opt_ T* pT) const throw() + { + return p.operator!=(pT); + } + operator bool() + { + return p.operator!=(NULL); } - virtual ~ComPtr() {} T* const* GetAddressOf() const throw() { @@ -634,7 +672,7 @@ public: T** ReleaseAndGetAddressOf() throw() { - InternalRelease(); + p.Release(); return &p; } @@ -642,27 +680,38 @@ public: { return p; } - ComPtr& operator=(decltype(__nullptr)) throw() + + // Attach to an existing interface (does not AddRef) + void Attach(_In_opt_ T* p2) throw() { - InternalRelease(); - return *this; + p.Attach(p2); } - ComPtr& operator=(_In_ const int nNull) throw() + // Detach the interface (does not Release) + T* Detach() throw() { - ASSERT(nNull == 0); - (void)nNull; - InternalRelease(); - return *this; + return p.Detach(); } - unsigned long Reset() + _Check_return_ HRESULT CopyTo(_Deref_out_opt_ T** ppT) throw() { - return InternalRelease(); + assert(ppT != NULL); + if (ppT == NULL) + return E_POINTER; + *ppT = p; + if (p != NULL) + p->AddRef(); + return S_OK; } + + void Reset() + { + p.Release(); + } + // query for U interface template HRESULT As(_Inout_ U** lp) const throw() { - return p->QueryInterface(__uuidof(U), (void**)lp); + return p->QueryInterface(__uuidof(U), reinterpret_cast(lp)); } // query for U interface template @@ -671,19 +720,8 @@ public: return p->QueryInterface(__uuidof(U), reinterpret_cast(lp->ReleaseAndGetAddressOf())); } private: - unsigned long InternalRelease() throw() - { - unsigned long ref = 0; - T* temp = p; - - if (temp != nullptr) - { - p = nullptr; - ref = temp->Release(); - } - - return ref; - } + _COM_SMARTPTR_TYPEDEF(T, __uuidof(T)); + _COM_SMARTPTR_DECLARE(T, p); }; #define _ComPtr ComPtr @@ -2262,6 +2300,11 @@ public: // succeed but return a nullptr pointer. By default, the list does not allow nullptr // pointers. +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable: 4127) // constant expression +#endif + template class ComPtrList : public List { @@ -2352,6 +2395,10 @@ protected: } }; +#ifdef _MSC_VER +#pragma warning(pop) +#endif + /* Be sure to declare webcam device capability in manifest For better media capture support, add the following snippet with correct module name to the project manifest (highgui needs DLL activation class factoryentry points):