1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: comtools.hxx,v $
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 ************************************************************************/
36 #pragma warning(push,1)
49 /* Simple exception class for propagating COM errors */
50 class ComError
: public std::runtime_error
53 ComError(const std::string
& message
, HRESULT hr
) :
54 std::runtime_error(message
),
58 HRESULT
GetHresult() const
67 /* A simple COM smart pointer template */
77 explicit COMReference(T
* comptr
) :
83 /* Explicitly controllable whether AddRef will be called or not */
84 COMReference(T
* comptr
, bool bAddRef
) :
91 COMReference(const COMReference
<T
>& other
) :
92 com_ptr_(other
.com_ptr_
)
97 COMReference
<T
>& operator=(const COMReference
<T
>& other
)
100 other
.com_ptr_
->AddRef();
102 com_ptr_
= other
.com_ptr_
;
106 COMReference
<T
>& operator=(T
* comptr
)
119 template<typename InterfaceType
>
120 COMReference
<InterfaceType
> QueryInterface(REFIID iid
)
122 COMReference
<InterfaceType
> ip
;
125 hr
= com_ptr_
->QueryInterface(iid
, reinterpret_cast<LPVOID
*>(&ip
));
128 throw ComError("QueryInterface failed: Interface not supported!", hr
);
133 T
* operator->() const
143 /* Necessary for assigning com_ptr_ from functions like
144 CoCreateInstance which require a 'void**' */
157 COMReference
<T
>& clear()
166 return (com_ptr_
!= NULL
);
174 cnt
= com_ptr_
->AddRef();
182 cnt
= com_ptr_
->Release();
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
;