Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / fpicker / source / win32 / comptr.hxx
blob2469d8b5e289dd0f1b997888540988c88396159e
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_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>
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 = nullptr;
43 /** initialize com ptr with given interface.
45 explicit 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 querying external object for the right interface.
65 explicit 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, nullptr, CLSCTX_ALL, P_IID, reinterpret_cast<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 = 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)
185 if (
186 ( ! m_pInterface ) &&
187 ( ! pCheck )
189 return sal_True;
191 IUnknown* pCurrent = NULL;
192 m_pInterface->QueryInterface(IID_IUnknown, (void**)&pCurrent);
194 sal_Bool bEquals = (pCheck == pCurrent);
195 pCurrent->Release();
197 return bEquals;
201 bool is()
203 return (m_pInterface != nullptr);
206 private:
207 T_INTERFACE* m_pInterface;
210 #endif
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */