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