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: comptr.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 ************************************************************************/
34 #include <sal/types.h>
35 #include <osl/diagnose.h>
38 template< class T_INTERFACE
,
39 REFIID P_IID
= IID_NULL
,
40 REFCLSID P_CLSID
= CLSID_NULL
>
45 //---------------------------------------------------------------------
46 /** initialize com ptr with null.
53 //---------------------------------------------------------------------
54 /** initialize com ptr with given interface.
56 ComPtr(T_INTERFACE
* pInterface
)
58 m_pInterface
= pInterface
;
60 m_pInterface
->AddRef();
63 //---------------------------------------------------------------------
66 ComPtr(const ComPtr
< T_INTERFACE
, P_IID
, P_CLSID
>& aCopy
)
68 m_pInterface
= aCopy
.m_pInterface
;
70 m_pInterface
->AddRef();
73 //---------------------------------------------------------------------
74 /** initialize object by quering external object for the right interface.
76 ComPtr(IUnknown
* pIUnknown
)
79 pIUnknown
->QueryInterface(P_IID
, (void**)&m_pInterface
);
82 //---------------------------------------------------------------------
83 /** deinitialize com object right.
92 //---------------------------------------------------------------------
95 return CoCreateInstance(P_CLSID
, NULL
, CLSCTX_ALL
, P_IID
, (void**)&m_pInterface
);
98 //---------------------------------------------------------------------
99 operator T_INTERFACE
*() const
104 //---------------------------------------------------------------------
105 T_INTERFACE
& operator*() const
107 return *m_pInterface
;
110 //---------------------------------------------------------------------
111 T_INTERFACE
** operator&()
113 return &m_pInterface
;
116 //---------------------------------------------------------------------
117 T_INTERFACE
* operator->() const
122 //---------------------------------------------------------------------
123 T_INTERFACE
* operator=(T_INTERFACE
* pInterface
)
125 if ( equals(pInterface
) )
128 m_pInterface
->Release();
129 m_pInterface
= pInterface
;
131 m_pInterface
->AddRef();
136 //---------------------------------------------------------------------
137 T_INTERFACE
* operator=(IUnknown
* pIUnknown
)
140 pIUnknown
->QueryInterface(P_IID
, (void**)&m_pInterface
);
144 //---------------------------------------------------------------------
145 T_INTERFACE
* operator=(const ComPtr
< T_INTERFACE
, P_IID
, P_CLSID
>& aCopy
)
147 m_pInterface
= aCopy
.m_pInterface
;
149 m_pInterface
->AddRef();
154 //---------------------------------------------------------------------
155 T_INTERFACE
* get() const
160 //---------------------------------------------------------------------
161 void attach(T_INTERFACE
* pInterface
)
165 m_pInterface
->Release();
166 m_pInterface
= pInterface
;
170 //---------------------------------------------------------------------
171 T_INTERFACE
* detach()
173 T_INTERFACE
* pInterface
= m_pInterface
;
178 //---------------------------------------------------------------------
183 m_pInterface
->Release();
189 //---------------------------------------------------------------------
190 template< class T_QUERYINTERFACE
>
191 HRESULT
query(T_QUERYINTERFACE
** pQuery
)
193 return m_pInterface
->QueryInterface(__uuidof(T_QUERYINTERFACE
), (void**)pQuery
);
197 //---------------------------------------------------------------------
198 ::sal_Bool
equals(IUnknown
* pCheck
)
201 ( ! m_pInterface
) &&
206 IUnknown
* pCurrent
= NULL
;
207 m_pInterface
->QueryInterface(IID_IUnknown
, (void**)&pCurrent
);
209 ::sal_Bool bEquals
= (pCheck
== pCurrent
);
215 //---------------------------------------------------------------------
218 return (m_pInterface
!= 0);
222 T_INTERFACE
* m_pInterface
;