1*10ce8018SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*10ce8018SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*10ce8018SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*10ce8018SAndrew Rist  * distributed with this work for additional information
6*10ce8018SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*10ce8018SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*10ce8018SAndrew Rist  * "License"); you may not use this file except in compliance
9*10ce8018SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*10ce8018SAndrew Rist  *
11*10ce8018SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*10ce8018SAndrew Rist  *
13*10ce8018SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*10ce8018SAndrew Rist  * software distributed under the License is distributed on an
15*10ce8018SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*10ce8018SAndrew Rist  * KIND, either express or implied.  See the License for the
17*10ce8018SAndrew Rist  * specific language governing permissions and limitations
18*10ce8018SAndrew Rist  * under the License.
19*10ce8018SAndrew Rist  *
20*10ce8018SAndrew Rist  *************************************************************/
21*10ce8018SAndrew Rist 
22*10ce8018SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef COMPTR_HXX
25cdf0e10cSrcweir #define COMPTR_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <shobjidl.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir template< class    T_INTERFACE          ,
32cdf0e10cSrcweir           REFIID   P_IID   = IID_NULL   ,
33cdf0e10cSrcweir           REFCLSID P_CLSID = CLSID_NULL >
34cdf0e10cSrcweir class ComPtr
35cdf0e10cSrcweir {
36cdf0e10cSrcweir     public:
37cdf0e10cSrcweir 
38cdf0e10cSrcweir         //---------------------------------------------------------------------
39cdf0e10cSrcweir         /** initialize com ptr with null.
40cdf0e10cSrcweir          */
ComPtr()41cdf0e10cSrcweir         ComPtr()
42cdf0e10cSrcweir         {
43cdf0e10cSrcweir             m_pInterface = NULL;
44cdf0e10cSrcweir         }
45cdf0e10cSrcweir 
46cdf0e10cSrcweir         //---------------------------------------------------------------------
47cdf0e10cSrcweir         /** initialize com ptr with given interface.
48cdf0e10cSrcweir          */
ComPtr(T_INTERFACE * pInterface)49cdf0e10cSrcweir         ComPtr(T_INTERFACE* pInterface)
50cdf0e10cSrcweir         {
51cdf0e10cSrcweir             m_pInterface = pInterface;
52cdf0e10cSrcweir             if (m_pInterface)
53cdf0e10cSrcweir                 m_pInterface->AddRef();
54cdf0e10cSrcweir         }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir         //---------------------------------------------------------------------
57cdf0e10cSrcweir         /** copy ctor.
58cdf0e10cSrcweir          */
ComPtr(const ComPtr<T_INTERFACE,P_IID,P_CLSID> & aCopy)59cdf0e10cSrcweir         ComPtr(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy)
60cdf0e10cSrcweir         {
61cdf0e10cSrcweir             m_pInterface = aCopy.m_pInterface;
62cdf0e10cSrcweir             if (m_pInterface)
63cdf0e10cSrcweir                 m_pInterface->AddRef();
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         //---------------------------------------------------------------------
67cdf0e10cSrcweir         /** initialize object by quering external object for the right interface.
68cdf0e10cSrcweir          */
ComPtr(IUnknown * pIUnknown)69cdf0e10cSrcweir         ComPtr(IUnknown* pIUnknown)
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             if (pIUnknown)
72cdf0e10cSrcweir                 pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         //---------------------------------------------------------------------
76cdf0e10cSrcweir         /** deinitialize com object right.
77cdf0e10cSrcweir          */
~ComPtr()78cdf0e10cSrcweir         ~ComPtr()
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             release();
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     public:
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         //---------------------------------------------------------------------
create()86cdf0e10cSrcweir         HRESULT create()
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             return CoCreateInstance(P_CLSID, NULL, CLSCTX_ALL, P_IID, (void**)&m_pInterface);
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         //---------------------------------------------------------------------
operator T_INTERFACE*() const92cdf0e10cSrcweir         operator T_INTERFACE*() const
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             return m_pInterface;
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         //---------------------------------------------------------------------
operator *() const98cdf0e10cSrcweir         T_INTERFACE& operator*() const
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             return *m_pInterface;
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         //---------------------------------------------------------------------
operator &()104cdf0e10cSrcweir         T_INTERFACE** operator&()
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             return &m_pInterface;
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         //---------------------------------------------------------------------
operator ->() const110cdf0e10cSrcweir         T_INTERFACE* operator->() const
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             return m_pInterface;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         //---------------------------------------------------------------------
operator =(T_INTERFACE * pInterface)116cdf0e10cSrcweir         T_INTERFACE* operator=(T_INTERFACE* pInterface)
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             if ( equals(pInterface) )
119cdf0e10cSrcweir                 return m_pInterface;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             m_pInterface->Release();
122cdf0e10cSrcweir             m_pInterface = pInterface;
123cdf0e10cSrcweir             if (m_pInterface)
124cdf0e10cSrcweir                 m_pInterface->AddRef();
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             return m_pInterface;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         //---------------------------------------------------------------------
operator =(IUnknown * pIUnknown)130cdf0e10cSrcweir         T_INTERFACE* operator=(IUnknown* pIUnknown)
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir             if (pIUnknown)
133cdf0e10cSrcweir                 pIUnknown->QueryInterface(P_IID, (void**)&m_pInterface);
134cdf0e10cSrcweir             return m_pInterface;
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         //---------------------------------------------------------------------
operator =(const ComPtr<T_INTERFACE,P_IID,P_CLSID> & aCopy)138cdf0e10cSrcweir         T_INTERFACE* operator=(const ComPtr< T_INTERFACE, P_IID, P_CLSID >& aCopy)
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             m_pInterface = aCopy.m_pInterface;
141cdf0e10cSrcweir             if (m_pInterface)
142cdf0e10cSrcweir                 m_pInterface->AddRef();
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             return m_pInterface;
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         //---------------------------------------------------------------------
get() const148cdf0e10cSrcweir         T_INTERFACE* get() const
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             return m_pInterface;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         //---------------------------------------------------------------------
attach(T_INTERFACE * pInterface)154cdf0e10cSrcweir         void attach(T_INTERFACE* pInterface)
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             if (pInterface)
157cdf0e10cSrcweir             {
158cdf0e10cSrcweir                 m_pInterface->Release();
159cdf0e10cSrcweir                 m_pInterface = pInterface;
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir         //---------------------------------------------------------------------
detach()164cdf0e10cSrcweir         T_INTERFACE* detach()
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir             T_INTERFACE* pInterface = m_pInterface;
167cdf0e10cSrcweir             m_pInterface = NULL;
168cdf0e10cSrcweir             return pInterface;
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         //---------------------------------------------------------------------
release()172cdf0e10cSrcweir         void release()
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             if (m_pInterface)
175cdf0e10cSrcweir             {
176cdf0e10cSrcweir                 m_pInterface->Release();
177cdf0e10cSrcweir                 m_pInterface = NULL;
178cdf0e10cSrcweir             }
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir #ifndef __MINGW32__
182cdf0e10cSrcweir         //---------------------------------------------------------------------
183cdf0e10cSrcweir         template< class T_QUERYINTERFACE >
query(T_QUERYINTERFACE ** pQuery)184cdf0e10cSrcweir         HRESULT query(T_QUERYINTERFACE** pQuery)
185cdf0e10cSrcweir         {
186cdf0e10cSrcweir             return m_pInterface->QueryInterface(__uuidof(T_QUERYINTERFACE), (void**)pQuery);
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir #endif
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         //---------------------------------------------------------------------
equals(IUnknown * pCheck)191cdf0e10cSrcweir         ::sal_Bool equals(IUnknown* pCheck)
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             if (
194cdf0e10cSrcweir                 ( ! m_pInterface ) &&
195cdf0e10cSrcweir                 ( ! pCheck       )
196cdf0e10cSrcweir                )
197cdf0e10cSrcweir                 return sal_True;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir             IUnknown* pCurrent = NULL;
200cdf0e10cSrcweir             m_pInterface->QueryInterface(IID_IUnknown, (void**)&pCurrent);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir             ::sal_Bool bEquals = (pCheck == pCurrent);
203cdf0e10cSrcweir             pCurrent->Release();
204cdf0e10cSrcweir 
205cdf0e10cSrcweir             return bEquals;
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         //---------------------------------------------------------------------
is()209cdf0e10cSrcweir         ::sal_Bool is()
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             return (m_pInterface != 0);
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     private:
215cdf0e10cSrcweir         T_INTERFACE* m_pInterface;
216cdf0e10cSrcweir };
217cdf0e10cSrcweir 
218cdf0e10cSrcweir #endif
219