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_COMPTR_HXX
21 #define INCLUDED_SYSTOOLS_WIN32_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.
43 /** initialize com ptr with given interface.
45 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 quering external object for the right interface.
65 ComPtr(IUnknown
* pIUnknown
)
68 pIUnknown
->QueryInterface(P_IID
, (void**)&m_pInterface
);
72 /** deinitialize com object right.
84 return CoCreateInstance(P_CLSID
, NULL
, CLSCTX_ALL
, P_IID
, (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();
179 template< class T_QUERYINTERFACE
>
180 HRESULT
query(T_QUERYINTERFACE
** pQuery
)
182 return m_pInterface
->QueryInterface(__uuidof(T_QUERYINTERFACE
), (void**)pQuery
);
187 HRESULT
query(REFIID rIID
,
190 return m_pInterface
->QueryInterface(rIID
, pQuery
);
194 HRESULT
unknown(IUnknown
** pQuery
)
196 return m_pInterface
->QueryInterface(IID_IUnknown
, (void**)pQuery
);
200 sal_Bool
equals(IUnknown
* pCheck
)
203 ( ! m_pInterface
) &&
208 IUnknown
* pCurrent
= NULL
;
209 m_pInterface
->QueryInterface(IID_IUnknown
, (void**)&pCurrent
);
211 sal_Bool bEquals
= (pCheck
== pCurrent
);
220 return (m_pInterface
!= 0);
224 T_INTERFACE
* m_pInterface
;
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */