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
33 /* Simple exception class for propagating COM errors */
34 class ComError
: public std::runtime_error
37 ComError(const std::string
& message
, HRESULT hr
) :
38 std::runtime_error(message
),
42 HRESULT
GetHresult() const
51 /* A simple COM smart pointer template */
61 explicit COMReference(T
* comptr
) :
67 /* Explicitly controllable whether AddRef will be called or not */
68 COMReference(T
* comptr
, bool bAddRef
) :
75 COMReference(const COMReference
<T
>& other
) :
76 com_ptr_(other
.com_ptr_
)
81 COMReference
<T
>& operator=(const COMReference
<T
>& other
)
84 other
.com_ptr_
->AddRef();
86 com_ptr_
= other
.com_ptr_
;
90 COMReference
<T
>& operator=(T
* comptr
)
103 template<typename InterfaceType
>
104 COMReference
<InterfaceType
> QueryInterface(REFIID iid
)
106 COMReference
<InterfaceType
> ip
;
109 hr
= com_ptr_
->QueryInterface(iid
, reinterpret_cast<LPVOID
*>(&ip
));
112 throw ComError("QueryInterface failed: Interface not supported!", hr
);
117 T
* operator->() const
127 /* Necessary for assigning com_ptr_ from functions like
128 CoCreateInstance which require a 'void**' */
141 COMReference
<T
>& clear()
150 return (com_ptr_
!= NULL
);
158 cnt
= com_ptr_
->AddRef();
166 cnt
= com_ptr_
->Release();
177 /* Typedefs for some popular COM interfaces */
178 typedef sal::systools::COMReference
<IDataObject
> IDataObjectPtr
;
179 typedef sal::systools::COMReference
<IStream
> IStreamPtr
;
181 #endif // INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */