Branch libreoffice-5-0-4
[LibreOffice.git] / include / systools / win32 / comptr.hxx
blobdb027274bc6396e1d41b138096a5ef5280ced984
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <shobjidl.h>
27 template< class T_INTERFACE ,
28 REFIID P_IID = IID_NULL ,
29 REFCLSID P_CLSID = CLSID_NULL >
30 class ComPtr
32 public:
35 /** initialize com ptr with null.
37 ComPtr()
39 m_pInterface = NULL;
43 /** initialize com ptr with given interface.
45 ComPtr(T_INTERFACE* pInterface)
47 m_pInterface = pInterface;
48 if (m_pInterface)
49 m_pInterface->AddRef();
53 /** copy ctor.
55 ComPtr(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy)
57 m_pInterface = aCopy.m_pInterface;
58 if (m_pInterface)
59 m_pInterface->AddRef();
63 /** initialize object by quering external object for the right interface.
65 ComPtr(IUnknown* pIUnknown)
67 if (pIUnknown)
68 pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
72 /** deinitialize com object right.
74 ~ComPtr()
76 release();
79 public:
82 HRESULT create()
84 return CoCreateInstance(P_CLSID, NULL, CLSCTX_ALL, P_IID, (void**)&m_pInterface);
88 operator T_INTERFACE*() const
90 return m_pInterface;
94 T_INTERFACE& operator*() const
96 return *m_pInterface;
100 T_INTERFACE** operator&()
102 return &m_pInterface;
106 T_INTERFACE* operator->() const
108 return m_pInterface;
112 T_INTERFACE* operator=(T_INTERFACE* pInterface)
114 if ( equals(pInterface) )
115 return m_pInterface;
117 m_pInterface->Release();
118 m_pInterface = pInterface;
119 if (m_pInterface)
120 m_pInterface->AddRef();
122 return m_pInterface;
126 T_INTERFACE* operator=(IUnknown* pIUnknown)
128 if (pIUnknown)
129 pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
130 return m_pInterface;
134 T_INTERFACE* operator=(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy)
136 m_pInterface = aCopy.m_pInterface;
137 if (m_pInterface)
138 m_pInterface->AddRef();
140 return m_pInterface;
144 T_INTERFACE* get() const
146 return m_pInterface;
150 void attach(T_INTERFACE* pInterface)
152 if (pInterface)
154 m_pInterface->Release();
155 m_pInterface = pInterface;
160 T_INTERFACE* detach()
162 T_INTERFACE* pInterface = m_pInterface;
163 m_pInterface = NULL;
164 return pInterface;
168 void release()
170 if (m_pInterface)
172 m_pInterface->Release();
173 m_pInterface = NULL;
177 #ifndef __MINGW32__
179 template< class T_QUERYINTERFACE >
180 HRESULT query(T_QUERYINTERFACE** pQuery)
182 return m_pInterface->QueryInterface(__uuidof(T_QUERYINTERFACE), (void**)pQuery);
184 #endif
187 HRESULT query(REFIID rIID ,
188 void** pQuery)
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)
202 if (
203 ( ! m_pInterface ) &&
204 ( ! pCheck )
206 return sal_True;
208 IUnknown* pCurrent = NULL;
209 m_pInterface->QueryInterface(IID_IUnknown, (void**)&pCurrent);
211 sal_Bool bEquals = (pCheck == pCurrent);
212 pCurrent->Release();
214 return bEquals;
218 sal_Bool is()
220 return (m_pInterface != 0);
223 private:
224 T_INTERFACE* m_pInterface;
227 #endif
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */