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_FPICKER_SOURCE_WIN32_FILEPICKER_COMPTR_HXX
21 #define INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_COMPTR_HXX
23 #include <sal/types.h>
24 #include <osl/diagnose.h>
27 template< class T_INTERFACE
,
28 REFIID P_IID
= IID_NULL
,
29 REFCLSID P_CLSID
= CLSID_NULL
>
35 /** initialize com ptr with null.
39 m_pInterface
= nullptr;
43 /** initialize com ptr with given interface.
45 explicit ComPtr(T_INTERFACE
* pInterface
)
47 m_pInterface
= pInterface
;
49 m_pInterface
->AddRef();
55 ComPtr(const ComPtr
< T_INTERFACE
, P_IID
, P_CLSID
>& aCopy
)
57 m_pInterface
= aCopy
.m_pInterface
;
59 m_pInterface
->AddRef();
63 /** initialize object by querying external object for the right interface.
65 explicit ComPtr(IUnknown
* pIUnknown
)
68 pIUnknown
->QueryInterface(P_IID
, (void**)&m_pInterface
);
72 /** deinitialize com object right.
84 return CoCreateInstance(P_CLSID
, nullptr, CLSCTX_ALL
, P_IID
, reinterpret_cast<void**>(&m_pInterface
));
88 operator T_INTERFACE
*() const
94 T_INTERFACE
& operator*() const
100 T_INTERFACE
** operator&()
102 return &m_pInterface
;
106 T_INTERFACE
* operator->() const
112 T_INTERFACE
* operator=(T_INTERFACE
* pInterface
)
114 if ( equals(pInterface
) )
117 m_pInterface
->Release();
118 m_pInterface
= pInterface
;
120 m_pInterface
->AddRef();
126 T_INTERFACE
* operator=(IUnknown
* pIUnknown
)
129 pIUnknown
->QueryInterface(P_IID
, (void**)&m_pInterface
);
134 T_INTERFACE
* operator=(const ComPtr
< T_INTERFACE
, P_IID
, P_CLSID
>& aCopy
)
136 m_pInterface
= aCopy
.m_pInterface
;
138 m_pInterface
->AddRef();
144 T_INTERFACE
* get() const
150 void attach(T_INTERFACE
* pInterface
)
154 m_pInterface
->Release();
155 m_pInterface
= pInterface
;
160 T_INTERFACE
* detach()
162 T_INTERFACE
* pInterface
= m_pInterface
;
172 m_pInterface
->Release();
173 m_pInterface
= nullptr;
177 template< class T_QUERYINTERFACE
>
178 HRESULT
query(T_QUERYINTERFACE
** pQuery
)
180 return m_pInterface
->QueryInterface(__uuidof(T_QUERYINTERFACE
), reinterpret_cast<void**>(pQuery
));
183 bool equals(IUnknown
* pCheck
)
186 ( ! m_pInterface
) &&
191 IUnknown
* pCurrent
= NULL
;
192 m_pInterface
->QueryInterface(IID_IUnknown
, (void**)&pCurrent
);
194 sal_Bool bEquals
= (pCheck
== pCurrent
);
203 return (m_pInterface
!= nullptr);
207 T_INTERFACE
* m_pInterface
;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */