| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: oleutils.h |
| 3 | // Purpose: OLE helper routines, OLE debugging support &c |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 19.02.1998 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_OLEUTILS_H |
| 13 | #define _WX_OLEUTILS_H |
| 14 | |
| 15 | #ifdef __GNUG__ |
| 16 | #pragma interface "oleutils.h" |
| 17 | #endif |
| 18 | |
| 19 | #include "wx/defs.h" |
| 20 | |
| 21 | // ============================================================================ |
| 22 | // General purpose functions and macros |
| 23 | // ============================================================================ |
| 24 | |
| 25 | // ---------------------------------------------------------------------------- |
| 26 | // misc helper functions/macros |
| 27 | // ---------------------------------------------------------------------------- |
| 28 | |
| 29 | // release the interface pointer (if !NULL) |
| 30 | inline void ReleaseInterface(IUnknown *pIUnk) |
| 31 | { |
| 32 | if ( pIUnk != NULL ) |
| 33 | pIUnk->Release(); |
| 34 | } |
| 35 | |
| 36 | // release the interface pointer (if !NULL) and make it NULL |
| 37 | #define RELEASE_AND_NULL(p) if ( (p) != NULL ) { p->Release(); p = NULL; }; |
| 38 | |
| 39 | // return TRUE if the iid is in the array |
| 40 | bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount); |
| 41 | |
| 42 | // ============================================================================ |
| 43 | // IUnknown implementation helpers |
| 44 | // ============================================================================ |
| 45 | |
| 46 | /* |
| 47 | The most dumb implementation of IUnknown methods. We don't support |
| 48 | aggregation nor containment, but for 99% of cases this simple |
| 49 | implementation is quite enough. |
| 50 | |
| 51 | Usage is trivial: here is all you should have |
| 52 | 1) DECLARE_IUNKNOWN_METHOS in your (IUnknown derived!) class declaration |
| 53 | 2) BEGIN/END_IID_TABLE with ADD_IID in between for all interfaces you |
| 54 | support (at least all for which you intent to return 'this' from QI, |
| 55 | i.e. you should derive from IFoo if you have ADD_IID(Foo)) somewhere else |
| 56 | 3) IMPLEMENT_IUNKNOWN_METHOS somewhere also |
| 57 | |
| 58 | These macros are quite simple: AddRef and Release are trivial and QI does |
| 59 | lookup in a static member array of IIDs and returns 'this' if it founds |
| 60 | the requested interface in it or E_NOINTERFACE if not. |
| 61 | */ |
| 62 | |
| 63 | // declare the methods and the member variable containing reference count |
| 64 | // you must also define the ms_aIids array somewhere with BEGIN_IID_TABLE |
| 65 | // and friends (see below) |
| 66 | #define DECLARE_IUNKNOWN_METHODS \ |
| 67 | public: \ |
| 68 | STDMETHODIMP QueryInterface(REFIID, void **); \ |
| 69 | STDMETHODIMP_(ULONG) AddRef(); \ |
| 70 | STDMETHODIMP_(ULONG) Release(); \ |
| 71 | private: \ |
| 72 | static const IID *ms_aIids[]; \ |
| 73 | ULONG m_cRef |
| 74 | |
| 75 | // macros for declaring supported interfaces |
| 76 | // NB: you should write ADD_INTERFACE(Foo) and not ADD_INTERFACE(IID_IFoo)! |
| 77 | #define BEGIN_IID_TABLE(cname) const IID *cname::ms_aIids[] = { |
| 78 | #define ADD_IID(iid) &IID_I##iid, |
| 79 | #define END_IID_TABLE } |
| 80 | |
| 81 | // implementation is as straightforward as possible |
| 82 | // Parameter: classname - the name of the class |
| 83 | #define IMPLEMENT_IUNKNOWN_METHODS(classname) \ |
| 84 | STDMETHODIMP classname::QueryInterface(REFIID riid, void **ppv) \ |
| 85 | { \ |
| 86 | wxLogQueryInterface(#classname, riid); \ |
| 87 | \ |
| 88 | if ( IsIidFromList(riid, ms_aIids, WXSIZEOF(ms_aIids)) ) { \ |
| 89 | *ppv = this; \ |
| 90 | AddRef(); \ |
| 91 | \ |
| 92 | return S_OK; \ |
| 93 | } \ |
| 94 | else { \ |
| 95 | *ppv = NULL; \ |
| 96 | \ |
| 97 | return (HRESULT) E_NOINTERFACE; \ |
| 98 | } \ |
| 99 | } \ |
| 100 | \ |
| 101 | STDMETHODIMP_(ULONG) classname::AddRef() \ |
| 102 | { \ |
| 103 | wxLogAddRef(#classname, m_cRef); \ |
| 104 | \ |
| 105 | return ++m_cRef; \ |
| 106 | } \ |
| 107 | \ |
| 108 | STDMETHODIMP_(ULONG) classname::Release() \ |
| 109 | { \ |
| 110 | wxLogRelease(#classname, m_cRef); \ |
| 111 | \ |
| 112 | if ( --m_cRef == 0 ) { \ |
| 113 | delete this; \ |
| 114 | return 0; \ |
| 115 | } \ |
| 116 | else \ |
| 117 | return m_cRef; \ |
| 118 | } |
| 119 | |
| 120 | // ============================================================================ |
| 121 | // Debugging support |
| 122 | // ============================================================================ |
| 123 | |
| 124 | // VZ: I don't know it's not done for compilers other than VC++ but I leave it |
| 125 | // as is. Please note, though, that tracing OLE interface calls may be |
| 126 | // incredibly useful when debugging OLE programs. |
| 127 | #if defined(__WXDEBUG__) && defined(__VISUALC__) && (__VISUALC__ >= 1000) |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | // All OLE specific log functions have DebugTrace level (as LogTrace) |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | // tries to translate riid into a symbolic name, if possible |
| 133 | void wxLogQueryInterface(const char *szInterface, REFIID riid); |
| 134 | |
| 135 | // these functions print out the new value of reference counter |
| 136 | void wxLogAddRef (const char *szInterface, ULONG cRef); |
| 137 | void wxLogRelease(const char *szInterface, ULONG cRef); |
| 138 | |
| 139 | #else //!WXDEBUG |
| 140 | #define wxLogQueryInterface(szInterface, riid) |
| 141 | #define wxLogAddRef(szInterface, cRef) |
| 142 | #define wxLogRelease(szInterface, cRef) |
| 143 | #endif //WXDEBUG |
| 144 | |
| 145 | #endif //_WX_OLEUTILS_H |
| 146 | |