LibreOffice Module fpicker (master)  1
comptr.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_COMPTR_HXX
21 #define INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_COMPTR_HXX
22 
23 #include <sal/types.h>
24 #include <osl/diagnose.h>
25 #include <shobjidl.h>
26 
27 template< class T_INTERFACE ,
28  REFIID P_IID = IID_NULL ,
29  REFCLSID P_CLSID = CLSID_NULL >
30 class ComPtr
31 {
32  public:
33 
34 
38  {
39  m_pInterface = nullptr;
40  }
41 
42 
45  explicit ComPtr(T_INTERFACE* pInterface)
46  {
47  m_pInterface = pInterface;
48  if (m_pInterface)
49  m_pInterface->AddRef();
50  }
51 
52 
56  {
57  m_pInterface = aCopy.m_pInterface;
58  if (m_pInterface)
59  m_pInterface->AddRef();
60  }
61 
62 
65  explicit ComPtr(IUnknown* pIUnknown)
66  {
67  if (pIUnknown)
68  pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
69  }
70 
71 
75  {
76  release();
77  }
78 
79  public:
80 
81 
82  HRESULT create()
83  {
84  return CoCreateInstance(P_CLSID, nullptr, CLSCTX_ALL, P_IID, reinterpret_cast<void**>(&m_pInterface));
85  }
86 
87 
88  operator T_INTERFACE*() const
89  {
90  return m_pInterface;
91  }
92 
93 
94  T_INTERFACE& operator*() const
95  {
96  return *m_pInterface;
97  }
98 
99 
100  T_INTERFACE** operator&()
101  {
102  return &m_pInterface;
103  }
104 
105 
106  T_INTERFACE* operator->() const
107  {
108  return m_pInterface;
109  }
110 
111 
112  T_INTERFACE* operator=(T_INTERFACE* pInterface)
113  {
114  if ( equals(pInterface) )
115  return m_pInterface;
116 
117  m_pInterface->Release();
118  m_pInterface = pInterface;
119  if (m_pInterface)
120  m_pInterface->AddRef();
121 
122  return m_pInterface;
123  }
124 
125 
126  T_INTERFACE* operator=(IUnknown* pIUnknown)
127  {
128  if (pIUnknown)
129  pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
130  return m_pInterface;
131  }
132 
133 
135  {
136  m_pInterface = aCopy.m_pInterface;
137  if (m_pInterface)
138  m_pInterface->AddRef();
139 
140  return m_pInterface;
141  }
142 
143 
144  T_INTERFACE* get() const
145  {
146  return m_pInterface;
147  }
148 
149 
150  void attach(T_INTERFACE* pInterface)
151  {
152  if (pInterface)
153  {
154  m_pInterface->Release();
155  m_pInterface = pInterface;
156  }
157  }
158 
159 
160  T_INTERFACE* detach()
161  {
162  T_INTERFACE* pInterface = m_pInterface;
163  m_pInterface = NULL;
164  return pInterface;
165  }
166 
167 
168  void release()
169  {
170  if (m_pInterface)
171  {
172  m_pInterface->Release();
173  m_pInterface = nullptr;
174  }
175  }
176 
177  template< class T_QUERYINTERFACE >
178  HRESULT query(T_QUERYINTERFACE** pQuery)
179  {
180  return m_pInterface->QueryInterface(__uuidof(T_QUERYINTERFACE), reinterpret_cast<void**>(pQuery));
181  }
182 
183  bool equals(IUnknown* pCheck)
184  {
185  if (
186  ( ! m_pInterface ) &&
187  ( ! pCheck )
188  )
189  return sal_True;
190 
191  IUnknown* pCurrent = NULL;
192  m_pInterface->QueryInterface(IID_IUnknown, (void**)&pCurrent);
193 
194  sal_Bool bEquals = (pCheck == pCurrent);
195  pCurrent->Release();
196 
197  return bEquals;
198  }
199 
200 
201  bool is()
202  {
203  return (m_pInterface != nullptr);
204  }
205 
206  private:
207  T_INTERFACE* m_pInterface;
208 };
209 
210 #endif
211 
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ComPtr(const ComPtr< T_INTERFACE, P_IID, P_CLSID > &aCopy)
copy ctor.
Definition: comptr.hxx:55
T_INTERFACE * operator->() const
Definition: comptr.hxx:106
void attach(T_INTERFACE *pInterface)
Definition: comptr.hxx:150
T_INTERFACE ** operator&()
Definition: comptr.hxx:100
HRESULT create()
Definition: comptr.hxx:82
void release()
Definition: comptr.hxx:168
T_INTERFACE * operator=(T_INTERFACE *pInterface)
Definition: comptr.hxx:112
return NULL
bool is()
Definition: comptr.hxx:201
T_INTERFACE * detach()
Definition: comptr.hxx:160
HRESULT query(T_QUERYINTERFACE **pQuery)
Definition: comptr.hxx:178
#define sal_True
unsigned char sal_Bool
ComPtr(T_INTERFACE *pInterface)
initialize com ptr with given interface.
Definition: comptr.hxx:45
ComPtr()
initialize com ptr with null.
Definition: comptr.hxx:37
ComPtr(IUnknown *pIUnknown)
initialize object by querying external object for the right interface.
Definition: comptr.hxx:65
T_INTERFACE * operator=(const ComPtr< T_INTERFACE, P_IID, P_CLSID > &aCopy)
Definition: comptr.hxx:134
T_INTERFACE * m_pInterface
Definition: comptr.hxx:207
T_INTERFACE & operator*() const
Definition: comptr.hxx:94
T_INTERFACE * operator=(IUnknown *pIUnknown)
Definition: comptr.hxx:126
~ComPtr()
deinitialize com object right.
Definition: comptr.hxx:74
bool equals(IUnknown *pCheck)
Definition: comptr.hxx:183