2 * Copyright 1999, 2000 Juergen Schmied
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef __ROS_SHELL_UTILS_H
20 #define __ROS_SHELL_UTILS_H
24 #endif /* defined(__cplusplus) */
27 # define IID_PPV_ARG(Itype, ppType) IID_##Itype, reinterpret_cast<void**>((static_cast<Itype**>(ppType)))
28 # define IID_NULL_PPV_ARG(Itype, ppType) IID_##Itype, NULL, reinterpret_cast<void**>((static_cast<Itype**>(ppType)))
30 # define IID_PPV_ARG(Itype, ppType) IID_##Itype, (void**)(ppType)
31 # define IID_NULL_PPV_ARG(Itype, ppType) IID_##Itype, NULL, (void**)(ppType)
35 #define FAILED_UNEXPECTEDLY(hr) (FAILED(hr) && (DbgPrint("Unexpected failure %08x.\n", hr), TRUE))
37 #define FAILED_UNEXPECTEDLY(hr) FAILED(hr)
42 #endif /* defined(__cplusplus) */
46 class CComDebugObject
: public Base
49 CComDebugObject(void * = NULL
)
51 #if DEBUG_CCOMOBJECT_CREATION
52 DbgPrint("%S, this=%08p\n", __FUNCTION__
, static_cast<Base
*>(this));
57 virtual ~CComDebugObject()
60 _pAtlModule
->Unlock();
63 STDMETHOD_(ULONG
, AddRef
)()
65 int rc
= this->InternalAddRef();
66 #if DEBUG_CCOMOBJECT_REFCOUNTING
67 DbgPrint("%s, RefCount is now %d(--)! \n", __FUNCTION__
, rc
);
72 STDMETHOD_(ULONG
, Release
)()
74 int rc
= this->InternalRelease();
76 #if DEBUG_CCOMOBJECT_REFCOUNTING
77 DbgPrint("%s, RefCount is now %d(--)! \n", __FUNCTION__
, rc
);
82 #if DEBUG_CCOMOBJECT_DESTRUCTION
83 DbgPrint("%s, RefCount reached 0 Deleting!\n", __FUNCTION__
);
90 STDMETHOD(QueryInterface
)(REFIID iid
, void **ppvObject
)
92 return this->_InternalQueryInterface(iid
, ppvObject
);
95 static HRESULT WINAPI
CreateInstance(CComDebugObject
<Base
> **pp
)
97 CComDebugObject
<Base
> *newInstance
;
100 ATLASSERT(pp
!= NULL
);
104 hResult
= E_OUTOFMEMORY
;
106 ATLTRY(newInstance
= new CComDebugObject
<Base
>());
107 if (newInstance
!= NULL
)
109 newInstance
->SetVoid(NULL
);
110 newInstance
->InternalFinalConstructAddRef();
111 hResult
= newInstance
->_AtlInitialConstruct();
112 if (SUCCEEDED(hResult
))
113 hResult
= newInstance
->FinalConstruct();
114 if (SUCCEEDED(hResult
))
115 hResult
= newInstance
->_AtlFinalConstruct();
116 newInstance
->InternalFinalConstructRelease();
128 #ifdef DEBUG_CCOMOBJECT
129 # define _CComObject CComDebugObject
131 # define _CComObject CComObject
135 void ReleaseCComPtrExpectZero(CComPtr
<T
>& cptr
, BOOL forceRelease
= FALSE
)
139 int nrc
= cptr
->Release();
142 DbgPrint("WARNING: Unexpected RefCount > 0 (%d)!\n", nrc
);
147 nrc
= cptr
->Release();
155 template<class T
, class R
>
156 HRESULT
inline ShellDebugObjectCreator(REFIID riid
, R
** ppv
)
164 ATLTRY(obj
= new CComDebugObject
<T
>);
166 return E_OUTOFMEMORY
;
167 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(ppv
));
173 template<class T
, class R
>
174 HRESULT
inline ShellObjectCreator(REFIID riid
, R
** ppv
)
182 ATLTRY(obj
= new _CComObject
<T
>);
184 return E_OUTOFMEMORY
;
185 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(ppv
));
191 template<class T
, class R
>
192 HRESULT
inline ShellObjectCreatorInit(REFIID riid
, R
** ppv
)
201 ATLTRY(obj
= new _CComObject
<T
>);
203 return E_OUTOFMEMORY
;
204 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
208 hResult
= obj
->Initialize();
212 *ppv
= result
.Detach();
218 HRESULT
inline ShellObjectCreatorInit(REFIID riid
, void ** ppv
)
221 CComPtr
<IUnknown
> result
;
227 ATLTRY(obj
= new _CComObject
<T
>);
229 return E_OUTOFMEMORY
;
230 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
234 hResult
= obj
->Initialize();
238 *ppv
= result
.Detach();
243 template<class T
, class T1
>
244 HRESULT
inline ShellObjectCreatorInit(T1 initArg1
, REFIID riid
, void ** ppv
)
252 ATLTRY(obj
= new _CComObject
<T
>);
254 return E_OUTOFMEMORY
;
255 hResult
= obj
->QueryInterface(riid
, ppv
);
259 hResult
= obj
->Initialize(initArg1
);
266 template<class T
, class T1
, class R
>
267 HRESULT
inline ShellObjectCreatorInit(T1 initArg1
, REFIID riid
, R
** ppv
)
276 ATLTRY(obj
= new _CComObject
<T
>);
278 return E_OUTOFMEMORY
;
279 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
283 hResult
= obj
->Initialize(initArg1
);
287 *ppv
= result
.Detach();
292 template<class T
, class T1
, class T2
, class R
>
293 HRESULT
inline ShellObjectCreatorInit(T1 initArg1
, T2 initArg2
, REFIID riid
, R
** ppv
)
302 ATLTRY(obj
= new _CComObject
<T
>);
304 return E_OUTOFMEMORY
;
305 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
309 hResult
= obj
->Initialize(initArg1
, initArg2
);
313 *ppv
= result
.Detach();
318 template<class T
, class T1
, class T2
, class T3
, class R
>
319 HRESULT
inline ShellObjectCreatorInit(T1 initArg1
, T2 initArg2
, T3 initArg3
, REFIID riid
, R
** ppv
)
328 ATLTRY(obj
= new _CComObject
<T
>);
330 return E_OUTOFMEMORY
;
331 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
335 hResult
= obj
->Initialize(initArg1
, initArg2
, initArg3
);
339 *ppv
= result
.Detach();
344 template<class T
, class T1
, class T2
, class T3
, class T4
, class R
>
345 HRESULT
inline ShellObjectCreatorInit(T1 initArg1
, T2 initArg2
, T3 initArg3
, T4 initArg4
, REFIID riid
, R
** ppv
)
354 ATLTRY(obj
= new _CComObject
<T
>);
356 return E_OUTOFMEMORY
;
357 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
361 hResult
= obj
->Initialize(initArg1
, initArg2
, initArg3
, initArg4
);
365 *ppv
= result
.Detach();
370 template<class T
, class T1
, class T2
, class T3
, class T4
, class T5
, class R
>
371 HRESULT
inline ShellObjectCreatorInit(T1 initArg1
, T2 initArg2
, T3 initArg3
, T4 initArg4
, T5 initArg5
, REFIID riid
, R
** ppv
)
380 ATLTRY(obj
= new _CComObject
<T
>);
382 return E_OUTOFMEMORY
;
383 hResult
= obj
->QueryInterface(riid
, reinterpret_cast<void **>(&result
));
387 hResult
= obj
->Initialize(initArg1
, initArg2
, initArg3
, initArg4
, initArg5
);
391 *ppv
= result
.Detach();
395 #endif /* __cplusplus */
397 #endif /* __ROS_SHELL_UTILS_H */