Update ooo320-m1
[ooovba.git] / fpicker / source / win32 / filepicker / comptr.hxx
blob8c6d87e4141c4341e49d592c7bb5d31ac67be15b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: comptr.hxx,v $
10 * $Revision: 1.4 $
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 ************************************************************************/
31 #ifndef COMPTR_HXX
32 #define COMPTR_HXX
34 #include <sal/types.h>
35 #include <osl/diagnose.h>
36 #include <shobjidl.h>
38 template< class T_INTERFACE ,
39 REFIID P_IID = IID_NULL ,
40 REFCLSID P_CLSID = CLSID_NULL >
41 class ComPtr
43 public:
45 //---------------------------------------------------------------------
46 /** initialize com ptr with null.
48 ComPtr()
50 m_pInterface = NULL;
53 //---------------------------------------------------------------------
54 /** initialize com ptr with given interface.
56 ComPtr(T_INTERFACE* pInterface)
58 m_pInterface = pInterface;
59 if (m_pInterface)
60 m_pInterface->AddRef();
63 //---------------------------------------------------------------------
64 /** copy ctor.
66 ComPtr(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy)
68 m_pInterface = aCopy.m_pInterface;
69 if (m_pInterface)
70 m_pInterface->AddRef();
73 //---------------------------------------------------------------------
74 /** initialize object by quering external object for the right interface.
76 ComPtr(IUnknown* pIUnknown)
78 if (pIUnknown)
79 pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
82 //---------------------------------------------------------------------
83 /** deinitialize com object right.
85 ~ComPtr()
87 release();
90 public:
92 //---------------------------------------------------------------------
93 HRESULT create()
95 return CoCreateInstance(P_CLSID, NULL, CLSCTX_ALL, P_IID, (void**)&m_pInterface);
98 //---------------------------------------------------------------------
99 operator T_INTERFACE*() const
101 return m_pInterface;
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
119 return m_pInterface;
122 //---------------------------------------------------------------------
123 T_INTERFACE* operator=(T_INTERFACE* pInterface)
125 if ( equals(pInterface) )
126 return m_pInterface;
128 m_pInterface->Release();
129 m_pInterface = pInterface;
130 if (m_pInterface)
131 m_pInterface->AddRef();
133 return m_pInterface;
136 //---------------------------------------------------------------------
137 T_INTERFACE* operator=(IUnknown* pIUnknown)
139 if (pIUnknown)
140 pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
141 return m_pInterface;
144 //---------------------------------------------------------------------
145 T_INTERFACE* operator=(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy)
147 m_pInterface = aCopy.m_pInterface;
148 if (m_pInterface)
149 m_pInterface->AddRef();
151 return m_pInterface;
154 //---------------------------------------------------------------------
155 T_INTERFACE* get() const
157 return m_pInterface;
160 //---------------------------------------------------------------------
161 void attach(T_INTERFACE* pInterface)
163 if (pInterface)
165 m_pInterface->Release();
166 m_pInterface = pInterface;
170 //---------------------------------------------------------------------
171 T_INTERFACE* detach()
173 T_INTERFACE* pInterface = m_pInterface;
174 m_pInterface = NULL;
175 return pInterface;
178 //---------------------------------------------------------------------
179 void release()
181 if (m_pInterface)
183 m_pInterface->Release();
184 m_pInterface = NULL;
188 #ifndef __MINGW32__
189 //---------------------------------------------------------------------
190 template< class T_QUERYINTERFACE >
191 HRESULT query(T_QUERYINTERFACE** pQuery)
193 return m_pInterface->QueryInterface(__uuidof(T_QUERYINTERFACE), (void**)pQuery);
195 #endif
197 //---------------------------------------------------------------------
198 ::sal_Bool equals(IUnknown* pCheck)
200 if (
201 ( ! m_pInterface ) &&
202 ( ! pCheck )
204 return sal_True;
206 IUnknown* pCurrent = NULL;
207 m_pInterface->QueryInterface(IID_IUnknown, (void**)&pCurrent);
209 ::sal_Bool bEquals = (pCheck == pCurrent);
210 pCurrent->Release();
212 return bEquals;
215 //---------------------------------------------------------------------
216 ::sal_Bool is()
218 return (m_pInterface != 0);
221 private:
222 T_INTERFACE* m_pInterface;
225 #endif