1*565d668cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*565d668cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*565d668cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*565d668cSAndrew Rist * distributed with this work for additional information 6*565d668cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*565d668cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*565d668cSAndrew Rist * "License"); you may not use this file except in compliance 9*565d668cSAndrew Rist * with the License. You may obtain a copy of the License at 10*565d668cSAndrew Rist * 11*565d668cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*565d668cSAndrew Rist * 13*565d668cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*565d668cSAndrew Rist * software distributed under the License is distributed on an 15*565d668cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*565d668cSAndrew Rist * KIND, either express or implied. See the License for the 17*565d668cSAndrew Rist * specific language governing permissions and limitations 18*565d668cSAndrew Rist * under the License. 19*565d668cSAndrew Rist * 20*565d668cSAndrew Rist *************************************************************/ 21*565d668cSAndrew Rist 22*565d668cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #pragma once 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <string> 27cdf0e10cSrcweir #include <stdexcept> 28cdf0e10cSrcweir #if defined _MSC_VER 29cdf0e10cSrcweir #pragma warning(push,1) 30cdf0e10cSrcweir #endif 31cdf0e10cSrcweir #include <objbase.h> 32cdf0e10cSrcweir #if defined _MSC_VER 33cdf0e10cSrcweir #pragma warning(pop) 34cdf0e10cSrcweir #endif 35cdf0e10cSrcweir 36cdf0e10cSrcweir namespace sal 37cdf0e10cSrcweir { 38cdf0e10cSrcweir namespace systools 39cdf0e10cSrcweir { 40cdf0e10cSrcweir typedef int HRESULT; 41cdf0e10cSrcweir 42cdf0e10cSrcweir /* Simple exception class for propagating COM errors */ 43cdf0e10cSrcweir class ComError : public std::runtime_error 44cdf0e10cSrcweir { 45cdf0e10cSrcweir public: ComError(const std::string & message,HRESULT hr)46cdf0e10cSrcweir ComError(const std::string& message, HRESULT hr) : 47cdf0e10cSrcweir std::runtime_error(message), 48cdf0e10cSrcweir hr_(hr) 49cdf0e10cSrcweir {} 50cdf0e10cSrcweir GetHresult() const51cdf0e10cSrcweir HRESULT GetHresult() const 52cdf0e10cSrcweir { 53cdf0e10cSrcweir return hr_; 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir private: 57cdf0e10cSrcweir HRESULT hr_; 58cdf0e10cSrcweir }; 59cdf0e10cSrcweir 60cdf0e10cSrcweir /* A simple COM smart pointer template */ 61cdf0e10cSrcweir template <typename T> 62cdf0e10cSrcweir class COMReference 63cdf0e10cSrcweir { 64cdf0e10cSrcweir public: COMReference()65cdf0e10cSrcweir COMReference() : 66cdf0e10cSrcweir com_ptr_(NULL) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir } 69cdf0e10cSrcweir COMReference(T * comptr)70cdf0e10cSrcweir explicit COMReference(T* comptr) : 71cdf0e10cSrcweir com_ptr_(comptr) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir addRef(); 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir /* Explicitly controllable whether AddRef will be called or not */ COMReference(T * comptr,bool bAddRef)77cdf0e10cSrcweir COMReference(T* comptr, bool bAddRef) : 78cdf0e10cSrcweir com_ptr_(comptr) 79cdf0e10cSrcweir { 80cdf0e10cSrcweir if (bAddRef) 81cdf0e10cSrcweir addRef(); 82cdf0e10cSrcweir } 83cdf0e10cSrcweir COMReference(const COMReference<T> & other)84cdf0e10cSrcweir COMReference(const COMReference<T>& other) : 85cdf0e10cSrcweir com_ptr_(other.com_ptr_) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir addRef(); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir operator =(const COMReference<T> & other)90cdf0e10cSrcweir COMReference<T>& operator=(const COMReference<T>& other) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir if (other.com_ptr_) 93cdf0e10cSrcweir other.com_ptr_->AddRef(); 94cdf0e10cSrcweir release(); 95cdf0e10cSrcweir com_ptr_ = other.com_ptr_; 96cdf0e10cSrcweir return *this; 97cdf0e10cSrcweir } 98cdf0e10cSrcweir operator =(T * comptr)99cdf0e10cSrcweir COMReference<T>& operator=(T* comptr) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir release(); 102cdf0e10cSrcweir com_ptr_ = comptr; 103cdf0e10cSrcweir addRef(); 104cdf0e10cSrcweir return *this; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir ~COMReference()107cdf0e10cSrcweir ~COMReference() 108cdf0e10cSrcweir { 109cdf0e10cSrcweir release(); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir template<typename InterfaceType> QueryInterface(REFIID iid)113cdf0e10cSrcweir COMReference<InterfaceType> QueryInterface(REFIID iid) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir COMReference<InterfaceType> ip; 116cdf0e10cSrcweir HRESULT hr = E_FAIL; 117cdf0e10cSrcweir if (com_ptr_) 118cdf0e10cSrcweir hr = com_ptr_->QueryInterface(iid, reinterpret_cast<LPVOID*>(&ip)); 119cdf0e10cSrcweir 120cdf0e10cSrcweir if (FAILED(hr)) 121cdf0e10cSrcweir throw ComError("QueryInterface failed: Interface not supported!", hr); 122cdf0e10cSrcweir 123cdf0e10cSrcweir return ip; 124cdf0e10cSrcweir } 125cdf0e10cSrcweir operator ->() const126cdf0e10cSrcweir T* operator->() const 127cdf0e10cSrcweir { 128cdf0e10cSrcweir return com_ptr_; 129cdf0e10cSrcweir } 130cdf0e10cSrcweir operator *() const131cdf0e10cSrcweir T& operator*() const 132cdf0e10cSrcweir { 133cdf0e10cSrcweir return *com_ptr_; 134cdf0e10cSrcweir } 135cdf0e10cSrcweir 136cdf0e10cSrcweir /* Necessary for assigning com_ptr_ from functions like 137cdf0e10cSrcweir CoCreateInstance which require a 'void**' */ operator &()138cdf0e10cSrcweir T** operator&() 139cdf0e10cSrcweir { 140cdf0e10cSrcweir release(); 141cdf0e10cSrcweir com_ptr_ = NULL; 142cdf0e10cSrcweir return &com_ptr_; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir get() const145cdf0e10cSrcweir T* get() const 146cdf0e10cSrcweir { 147cdf0e10cSrcweir return com_ptr_; 148cdf0e10cSrcweir } 149cdf0e10cSrcweir clear()150cdf0e10cSrcweir COMReference<T>& clear() 151cdf0e10cSrcweir { 152cdf0e10cSrcweir release(); 153cdf0e10cSrcweir com_ptr_ = NULL; 154cdf0e10cSrcweir return *this; 155cdf0e10cSrcweir } 156cdf0e10cSrcweir is() const157cdf0e10cSrcweir bool is() const 158cdf0e10cSrcweir { 159cdf0e10cSrcweir return (com_ptr_ != NULL); 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir private: addRef()163cdf0e10cSrcweir ULONG addRef() 164cdf0e10cSrcweir { 165cdf0e10cSrcweir ULONG cnt = 0; 166cdf0e10cSrcweir if (com_ptr_) 167cdf0e10cSrcweir cnt = com_ptr_->AddRef(); 168cdf0e10cSrcweir return cnt; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir release()171cdf0e10cSrcweir ULONG release() 172cdf0e10cSrcweir { 173cdf0e10cSrcweir ULONG cnt = 0; 174cdf0e10cSrcweir if (com_ptr_) 175cdf0e10cSrcweir cnt = com_ptr_->Release(); 176cdf0e10cSrcweir return cnt; 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir private: 180cdf0e10cSrcweir T* com_ptr_; 181cdf0e10cSrcweir }; 182cdf0e10cSrcweir 183cdf0e10cSrcweir } // systools 184cdf0e10cSrcweir } // sal 185cdf0e10cSrcweir 186cdf0e10cSrcweir /* Typedefs for some popular COM interfaces */ 187cdf0e10cSrcweir typedef sal::systools::COMReference<IDataObject> IDataObjectPtr; 188cdf0e10cSrcweir typedef sal::systools::COMReference<IStream> IStreamPtr; 189cdf0e10cSrcweir typedef sal::systools::COMReference<IEnumFORMATETC> IEnumFORMATETCPtr; 190cdf0e10cSrcweir 191