update dev300-m58
[ooovba.git] / sal / inc / systools / win32 / comtools.hxx
blob2285db1a6436d18a7c5e13352babb9a285414ba0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: comtools.hxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #pragma once
33 #include <string>
34 #include <stdexcept>
35 #if defined _MSC_VER
36 #pragma warning(push,1)
37 #endif
38 #include <objbase.h>
39 #if defined _MSC_VER
40 #pragma warning(pop)
41 #endif
43 namespace sal
45 namespace systools
47 typedef int HRESULT;
49 /* Simple exception class for propagating COM errors */
50 class ComError : public std::runtime_error
52 public:
53 ComError(const std::string& message, HRESULT hr) :
54 std::runtime_error(message),
55 hr_(hr)
58 HRESULT GetHresult() const
60 return hr_;
63 private:
64 HRESULT hr_;
67 /* A simple COM smart pointer template */
68 template <typename T>
69 class COMReference
71 public:
72 COMReference() :
73 com_ptr_(NULL)
77 explicit COMReference(T* comptr) :
78 com_ptr_(comptr)
80 addRef();
83 /* Explicitly controllable whether AddRef will be called or not */
84 COMReference(T* comptr, bool bAddRef) :
85 com_ptr_(comptr)
87 if (bAddRef)
88 addRef();
91 COMReference(const COMReference<T>& other) :
92 com_ptr_(other.com_ptr_)
94 addRef();
97 COMReference<T>& operator=(const COMReference<T>& other)
99 if (other.com_ptr_)
100 other.com_ptr_->AddRef();
101 release();
102 com_ptr_ = other.com_ptr_;
103 return *this;
106 COMReference<T>& operator=(T* comptr)
108 release();
109 com_ptr_ = comptr;
110 addRef();
111 return *this;
114 ~COMReference()
116 release();
119 template<typename InterfaceType>
120 COMReference<InterfaceType> QueryInterface(REFIID iid)
122 COMReference<InterfaceType> ip;
123 HRESULT hr = E_FAIL;
124 if (com_ptr_)
125 hr = com_ptr_->QueryInterface(iid, reinterpret_cast<LPVOID*>(&ip));
127 if (FAILED(hr))
128 throw ComError("QueryInterface failed: Interface not supported!", hr);
130 return ip;
133 T* operator->() const
135 return com_ptr_;
138 T& operator*() const
140 return *com_ptr_;
143 /* Necessary for assigning com_ptr_ from functions like
144 CoCreateInstance which require a 'void**' */
145 T** operator&()
147 release();
148 com_ptr_ = NULL;
149 return &com_ptr_;
152 T* get() const
154 return com_ptr_;
157 COMReference<T>& clear()
159 release();
160 com_ptr_ = NULL;
161 return *this;
164 bool is() const
166 return (com_ptr_ != NULL);
169 private:
170 ULONG addRef()
172 ULONG cnt = 0;
173 if (com_ptr_)
174 cnt = com_ptr_->AddRef();
175 return cnt;
178 ULONG release()
180 ULONG cnt = 0;
181 if (com_ptr_)
182 cnt = com_ptr_->Release();
183 return cnt;
186 private:
187 T* com_ptr_;
190 } // systools
191 } // sal
193 /* Typedefs for some popular COM interfaces */
194 typedef sal::systools::COMReference<IDataObject> IDataObjectPtr;
195 typedef sal::systools::COMReference<IStream> IStreamPtr;
196 typedef sal::systools::COMReference<IEnumFORMATETC> IEnumFORMATETCPtr;