1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
21 #define INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
26 #pragma warning(push,1)
39 /* Simple exception class for propagating COM errors */
40 class ComError
: public std::runtime_error
43 ComError(const std::string
& message
, HRESULT hr
) :
44 std::runtime_error(message
),
48 HRESULT
GetHresult() const
57 /* A simple COM smart pointer template */
67 explicit COMReference(T
* comptr
) :
73 /* Explicitly controllable whether AddRef will be called or not */
74 COMReference(T
* comptr
, bool bAddRef
) :
81 COMReference(const COMReference
<T
>& other
) :
82 com_ptr_(other
.com_ptr_
)
87 COMReference
<T
>& operator=(const COMReference
<T
>& other
)
90 other
.com_ptr_
->AddRef();
92 com_ptr_
= other
.com_ptr_
;
96 COMReference
<T
>& operator=(T
* comptr
)
109 template<typename InterfaceType
>
110 COMReference
<InterfaceType
> QueryInterface(REFIID iid
)
112 COMReference
<InterfaceType
> ip
;
115 hr
= com_ptr_
->QueryInterface(iid
, reinterpret_cast<LPVOID
*>(&ip
));
118 throw ComError("QueryInterface failed: Interface not supported!", hr
);
123 T
* operator->() const
133 /* Necessary for assigning com_ptr_ from functions like
134 CoCreateInstance which require a 'void**' */
147 COMReference
<T
>& clear()
156 return (com_ptr_
!= NULL
);
164 cnt
= com_ptr_
->AddRef();
172 cnt
= com_ptr_
->Release();
183 /* Typedefs for some popular COM interfaces */
184 typedef sal::systools::COMReference
<IDataObject
> IDataObjectPtr
;
185 typedef sal::systools::COMReference
<IStream
> IStreamPtr
;
186 typedef sal::systools::COMReference
<IEnumFORMATETC
> IEnumFORMATETCPtr
;
188 #endif // INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */