1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * Various COM definitions and COM wrapper class declarations
|
---|
5 | *
|
---|
6 | * This header is used in conjunction with the header generated from
|
---|
7 | * XIDL expressed interface definitions to provide cross-platform Qt-based
|
---|
8 | * interface wrapper classes.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
13 | *
|
---|
14 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
15 | * available from http://www.virtualbox.org. This file is free software;
|
---|
16 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
17 | * General Public License (GPL) as published by the Free Software
|
---|
18 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
19 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
20 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
21 | *
|
---|
22 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
23 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
24 | * additional information or have any questions.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef __COMDefs_h__
|
---|
28 | #define __COMDefs_h__
|
---|
29 |
|
---|
30 | /** @defgroup grp_QT_COM Qt-COM Support Layer
|
---|
31 | * @{
|
---|
32 | *
|
---|
33 | * The Qt-COM support layer provides a set of defintions and smart classes for
|
---|
34 | * writing simple, clean and platform-independent code to access COM/XPCOM
|
---|
35 | * components through exposed COM interfaces. This layer is based on the
|
---|
36 | * COM/XPCOM Abstarction Layer library (the VBoxCOM glue library defined in
|
---|
37 | * include/VBox/com and implemented in src/VBox/Main/glue).
|
---|
38 | *
|
---|
39 | * ...
|
---|
40 | *
|
---|
41 | * @defgroup grp_QT_COM_arrays Arrays
|
---|
42 | * @{
|
---|
43 | *
|
---|
44 | * COM/XPCOM arrays are mapped to QValueVector objects. QValueVector templates
|
---|
45 | * declared with a type that corresponds to the COM type of elements in the
|
---|
46 | * array using normal Qt-COM type mapping rules. Here is a code example that
|
---|
47 | * demonstrates how to call interface methods that take and return arrays (this
|
---|
48 | * example is based on examples given in @ref grp_COM_arrays):
|
---|
49 | * @code
|
---|
50 |
|
---|
51 | CSomething component;
|
---|
52 |
|
---|
53 | // ...
|
---|
54 |
|
---|
55 | QValueVector <LONG> in (3);
|
---|
56 | in [0] = -1;
|
---|
57 | in [1] = -2;
|
---|
58 | in [2] = -3;
|
---|
59 |
|
---|
60 | QValueVector <LONG> out;
|
---|
61 | QValueVector <LONG> ret;
|
---|
62 |
|
---|
63 | ret = component.TestArrays (in, out);
|
---|
64 |
|
---|
65 | for (size_t i = 0; i < ret.size(); ++ i)
|
---|
66 | LogFlow (("*** ret[%u]=%d\n", i, ret [i]));
|
---|
67 |
|
---|
68 | * @endcode
|
---|
69 | * @}
|
---|
70 | */
|
---|
71 |
|
---|
72 | /* Both VBox/com/assert.h and qglobal.h contain a definition of ASSERT.
|
---|
73 | * Either of them can be already included here, so try to shut them up. */
|
---|
74 | #undef ASSERT
|
---|
75 |
|
---|
76 | #include <VBox/com/com.h>
|
---|
77 | #include <VBox/com/array.h>
|
---|
78 | #include <VBox/com/assert.h>
|
---|
79 |
|
---|
80 | #undef ASSERT
|
---|
81 |
|
---|
82 | #include <qglobal.h>
|
---|
83 | #include <qstring.h>
|
---|
84 | #include <quuid.h>
|
---|
85 | #include <qvaluevector.h>
|
---|
86 |
|
---|
87 | #include <iprt/memory> // for auto_copy_ptr
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Additional COM / XPCOM defines and includes
|
---|
91 | */
|
---|
92 |
|
---|
93 | #define IN_BSTRPARAM INPTR BSTR
|
---|
94 | #define IN_GUIDPARAM INPTR GUIDPARAM
|
---|
95 |
|
---|
96 | #if !defined (VBOX_WITH_XPCOM)
|
---|
97 |
|
---|
98 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
99 |
|
---|
100 | #include <nsXPCOM.h>
|
---|
101 | #include <nsMemory.h>
|
---|
102 | #include <nsIComponentManager.h>
|
---|
103 |
|
---|
104 | class XPCOMEventQSocketListener;
|
---|
105 |
|
---|
106 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
107 |
|
---|
108 |
|
---|
109 | /* VirtualBox interfaces declarations */
|
---|
110 | #if !defined (VBOX_WITH_XPCOM)
|
---|
111 | #include <VirtualBox.h>
|
---|
112 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
113 | #include <VirtualBox_XPCOM.h>
|
---|
114 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
115 |
|
---|
116 | #include "VBoxDefs.h"
|
---|
117 |
|
---|
118 |
|
---|
119 | /////////////////////////////////////////////////////////////////////////////
|
---|
120 |
|
---|
121 | class CVirtualBoxErrorInfo;
|
---|
122 |
|
---|
123 | /** Represents extended error information */
|
---|
124 | class COMErrorInfo
|
---|
125 | {
|
---|
126 | public:
|
---|
127 |
|
---|
128 | COMErrorInfo()
|
---|
129 | : mIsNull (true)
|
---|
130 | , mIsBasicAvailable (false), mIsFullAvailable (false)
|
---|
131 | , mResultCode (S_OK) {}
|
---|
132 |
|
---|
133 | COMErrorInfo (const CVirtualBoxErrorInfo &info) { init (info); }
|
---|
134 |
|
---|
135 | /* the default copy ctor and assignment op are ok */
|
---|
136 |
|
---|
137 | bool isNull() const { return mIsNull; }
|
---|
138 |
|
---|
139 | bool isBasicAvailable() const { return mIsBasicAvailable; }
|
---|
140 | bool isFullAvailable() const { return mIsFullAvailable; }
|
---|
141 |
|
---|
142 | HRESULT resultCode() const { return mResultCode; }
|
---|
143 | QUuid interfaceID() const { return mInterfaceID; }
|
---|
144 | QString component() const { return mComponent; }
|
---|
145 | QString text() const { return mText; }
|
---|
146 |
|
---|
147 | const COMErrorInfo *next() const { return mNext.get(); }
|
---|
148 |
|
---|
149 | QString interfaceName() const { return mInterfaceName; }
|
---|
150 | QUuid calleeIID() const { return mCalleeIID; }
|
---|
151 | QString calleeName() const { return mCalleeName; }
|
---|
152 |
|
---|
153 | private:
|
---|
154 |
|
---|
155 | void init (const CVirtualBoxErrorInfo &info);
|
---|
156 | void fetchFromCurrentThread (IUnknown *callee, const GUID *calleeIID);
|
---|
157 |
|
---|
158 | static QString getInterfaceNameFromIID (const QUuid &id);
|
---|
159 |
|
---|
160 | bool mIsNull : 1;
|
---|
161 | bool mIsBasicAvailable : 1;
|
---|
162 | bool mIsFullAvailable : 1;
|
---|
163 |
|
---|
164 | HRESULT mResultCode;
|
---|
165 | QUuid mInterfaceID;
|
---|
166 | QString mComponent;
|
---|
167 | QString mText;
|
---|
168 |
|
---|
169 | cppx::auto_copy_ptr <COMErrorInfo> mNext;
|
---|
170 |
|
---|
171 | QString mInterfaceName;
|
---|
172 | QUuid mCalleeIID;
|
---|
173 | QString mCalleeName;
|
---|
174 |
|
---|
175 | friend class COMBaseWithEI;
|
---|
176 | };
|
---|
177 |
|
---|
178 | /////////////////////////////////////////////////////////////////////////////
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Base COM class the CInterface template and all wrapper classes are derived
|
---|
182 | * from. Provides common functionality for all COM wrappers.
|
---|
183 | */
|
---|
184 | class COMBase
|
---|
185 | {
|
---|
186 | public:
|
---|
187 |
|
---|
188 | static HRESULT InitializeCOM();
|
---|
189 | static HRESULT CleanupCOM();
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Returns the result code of the last interface method called
|
---|
193 | * by the wrapper instance or the result of CInterface::createInstance()
|
---|
194 | * operation.
|
---|
195 | */
|
---|
196 | HRESULT lastRC() const { return mRC; }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Returns error info set by the last unsuccessfully invoked interface
|
---|
200 | * method. Returned error info is useful only if CInterface::lastRC()
|
---|
201 | * represents a failure or a warning (i.e. CInterface::isReallyOk() is
|
---|
202 | * false).
|
---|
203 | */
|
---|
204 | virtual COMErrorInfo errorInfo() const { return COMErrorInfo(); }
|
---|
205 |
|
---|
206 | #if !defined (VBOX_WITH_XPCOM)
|
---|
207 |
|
---|
208 | /** Converts a GUID value to QUuid */
|
---|
209 | static QUuid ToQUuid (const GUID &id)
|
---|
210 | {
|
---|
211 | return QUuid (id.Data1, id.Data2, id.Data3,
|
---|
212 | id.Data4[0], id.Data4[1], id.Data4[2], id.Data4[3],
|
---|
213 | id.Data4[4], id.Data4[5], id.Data4[6], id.Data4[7]);
|
---|
214 | }
|
---|
215 |
|
---|
216 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
217 |
|
---|
218 | /** Converts a GUID value to QUuid */
|
---|
219 | static QUuid ToQUuid (const nsID &id)
|
---|
220 | {
|
---|
221 | return QUuid (id.m0, id.m1, id.m2,
|
---|
222 | id.m3[0], id.m3[1], id.m3[2], id.m3[3],
|
---|
223 | id.m3[4], id.m3[5], id.m3[6], id.m3[7]);
|
---|
224 | }
|
---|
225 |
|
---|
226 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
227 |
|
---|
228 | /* Arrays of arbitrary types */
|
---|
229 |
|
---|
230 | template <typename QT, typename CT>
|
---|
231 | static void ToSafeArray (const QValueVector <QT> &aVec, com::SafeArray <CT> &aArr)
|
---|
232 | {
|
---|
233 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
234 | }
|
---|
235 |
|
---|
236 | template <typename CT, typename QT>
|
---|
237 | static void FromSafeArray (const com::SafeArray <CT> &aArr, QValueVector <QT> &aVec)
|
---|
238 | {
|
---|
239 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
240 | }
|
---|
241 |
|
---|
242 | template <typename QT, typename CT>
|
---|
243 | static void ToSafeArray (const QValueVector <QT *> &aVec, com::SafeArray <CT *> &aArr)
|
---|
244 | {
|
---|
245 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
246 | }
|
---|
247 |
|
---|
248 | template <typename CT, typename QT>
|
---|
249 | static void FromSafeArray (const com::SafeArray <CT *> &aArr, QValueVector <QT *> &aVec)
|
---|
250 | {
|
---|
251 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
252 | }
|
---|
253 |
|
---|
254 | /* Arrays of equal types */
|
---|
255 |
|
---|
256 | template <typename T>
|
---|
257 | static void ToSafeArray (const QValueVector <T> &aVec, com::SafeArray <T> &aArr)
|
---|
258 | {
|
---|
259 | aArr.reset (aVec.size());
|
---|
260 | size_t i = 0;
|
---|
261 | for (typename QValueVector <T>::const_iterator it = aVec.begin();
|
---|
262 | it != aVec.end(); ++ it, ++ i)
|
---|
263 | aArr [i] = *it;
|
---|
264 | }
|
---|
265 |
|
---|
266 | template <typename T>
|
---|
267 | static void FromSafeArray (const com::SafeArray <T> &aArr, QValueVector <T> &aVec)
|
---|
268 | {
|
---|
269 | aVec = QValueVector <T> (aArr.size());
|
---|
270 | size_t i = 0;
|
---|
271 | for (typename QValueVector <T>::iterator it = aVec.begin();
|
---|
272 | it != aVec.end(); ++ it, ++ i)
|
---|
273 | *it = aArr [i];
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* Arrays of strings */
|
---|
277 |
|
---|
278 | static void ToSafeArray (const QValueVector <QString> &aVec,
|
---|
279 | com::SafeArray <BSTR> &aArr);
|
---|
280 | static void FromSafeArray (const com::SafeArray <BSTR> &aArr,
|
---|
281 | QValueVector <QString> &aVec);
|
---|
282 |
|
---|
283 | /* Arrays of interface pointers. Note: we need a separate pair of names
|
---|
284 | * only because the MSVC8 template matching algorithm is poor and tries to
|
---|
285 | * instantiate a com::SafeIfaceArray <BSTR> (!!!) template otherwise for
|
---|
286 | * *no* reason and fails. Note that it's also not possible to choose the
|
---|
287 | * correct function by specifying template arguments explicitly because then
|
---|
288 | * it starts to try to instantiate the com::SafeArray <I> template for
|
---|
289 | * *no* reason again and fails too. Definitely, broken. Works in GCC like a
|
---|
290 | * charm. */
|
---|
291 |
|
---|
292 | template <class CI, class I>
|
---|
293 | static void ToSafeIfaceArray (const QValueVector <CI> &aVec,
|
---|
294 | com::SafeIfaceArray <I> &aArr)
|
---|
295 | {
|
---|
296 | aArr.reset (aVec.size());
|
---|
297 | size_t i = 0;
|
---|
298 | for (typename QValueVector <CI>::const_iterator it = aVec.begin();
|
---|
299 | it != aVec.end(); ++ it, ++ i)
|
---|
300 | {
|
---|
301 | aArr [i] = (*it).iface();
|
---|
302 | if (aArr [i])
|
---|
303 | aArr [i]->AddRef();
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | template <class I, class CI>
|
---|
308 | static void FromSafeIfaceArray (const com::SafeIfaceArray <I> &aArr,
|
---|
309 | QValueVector <CI> &aVec)
|
---|
310 | {
|
---|
311 | aVec = QValueVector <CI> (aArr.size());
|
---|
312 | size_t i = 0;
|
---|
313 | for (typename QValueVector <CI>::iterator it = aVec.begin();
|
---|
314 | it != aVec.end(); ++ it, ++ i)
|
---|
315 | (*it).attach (aArr [i]);
|
---|
316 | }
|
---|
317 |
|
---|
318 | protected:
|
---|
319 |
|
---|
320 | /* no arbitrary instance creations */
|
---|
321 | COMBase() : mRC (S_OK) {};
|
---|
322 |
|
---|
323 | #if defined (VBOX_WITH_XPCOM)
|
---|
324 | static XPCOMEventQSocketListener *sSocketListener;
|
---|
325 | #endif
|
---|
326 |
|
---|
327 | /** Adapter to pass QString as input BSTR params */
|
---|
328 | class BSTRIn
|
---|
329 | {
|
---|
330 | public:
|
---|
331 |
|
---|
332 | BSTRIn (const QString &s) : bstr (SysAllocString ((const OLECHAR *) s.ucs2())) {}
|
---|
333 |
|
---|
334 | ~BSTRIn()
|
---|
335 | {
|
---|
336 | if (bstr)
|
---|
337 | SysFreeString (bstr);
|
---|
338 | }
|
---|
339 |
|
---|
340 | operator BSTR() const { return bstr; }
|
---|
341 |
|
---|
342 | private:
|
---|
343 |
|
---|
344 | BSTR bstr;
|
---|
345 | };
|
---|
346 |
|
---|
347 | /** Adapter to pass QString as output BSTR params */
|
---|
348 | class BSTROut
|
---|
349 | {
|
---|
350 | public:
|
---|
351 |
|
---|
352 | BSTROut (QString &s) : str (s), bstr (0) {}
|
---|
353 |
|
---|
354 | ~BSTROut()
|
---|
355 | {
|
---|
356 | if (bstr) {
|
---|
357 | str = QString::fromUcs2 (bstr);
|
---|
358 | SysFreeString (bstr);
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | operator BSTR *() { return &bstr; }
|
---|
363 |
|
---|
364 | private:
|
---|
365 |
|
---|
366 | QString &str;
|
---|
367 | BSTR bstr;
|
---|
368 | };
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Adapter to pass K* enums as output COM enum params (*_T).
|
---|
372 | *
|
---|
373 | * @param QE K* enum.
|
---|
374 | * @param CE COM enum.
|
---|
375 | */
|
---|
376 | template <typename QE, typename CE>
|
---|
377 | class ENUMOut
|
---|
378 | {
|
---|
379 | public:
|
---|
380 |
|
---|
381 | ENUMOut (QE &e) : qe (e), ce ((CE) 0) {}
|
---|
382 | ~ENUMOut() { qe = (QE) ce; }
|
---|
383 | operator CE *() { return &ce; }
|
---|
384 |
|
---|
385 | private:
|
---|
386 |
|
---|
387 | QE &qe;
|
---|
388 | CE ce;
|
---|
389 | };
|
---|
390 |
|
---|
391 | #if !defined (VBOX_WITH_XPCOM)
|
---|
392 |
|
---|
393 | /** Adapter to pass QUuid as input GUID params */
|
---|
394 | static GUID GUIDIn (const QUuid &uuid) { return uuid; }
|
---|
395 |
|
---|
396 | /** Adapter to pass QUuid as output GUID params */
|
---|
397 | class GUIDOut
|
---|
398 | {
|
---|
399 | public:
|
---|
400 |
|
---|
401 | GUIDOut (QUuid &id) : uuid (id)
|
---|
402 | {
|
---|
403 | ::memset (&guid, 0, sizeof (GUID));
|
---|
404 | }
|
---|
405 |
|
---|
406 | ~GUIDOut()
|
---|
407 | {
|
---|
408 | uuid = QUuid (
|
---|
409 | guid.Data1, guid.Data2, guid.Data3,
|
---|
410 | guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
|
---|
411 | guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
|
---|
412 | }
|
---|
413 |
|
---|
414 | operator GUID *() { return &guid; }
|
---|
415 |
|
---|
416 | private:
|
---|
417 |
|
---|
418 | QUuid &uuid;
|
---|
419 | GUID guid;
|
---|
420 | };
|
---|
421 |
|
---|
422 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
423 |
|
---|
424 | /** Adapter to pass QUuid as input GUID params */
|
---|
425 | static const nsID &GUIDIn (const QUuid &uuid)
|
---|
426 | {
|
---|
427 | return *(const nsID *) &uuid;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /** Adapter to pass QUuid as output GUID params */
|
---|
431 | class GUIDOut
|
---|
432 | {
|
---|
433 | public:
|
---|
434 |
|
---|
435 | GUIDOut (QUuid &id) : uuid (id), nsid (0) {}
|
---|
436 |
|
---|
437 | ~GUIDOut()
|
---|
438 | {
|
---|
439 | if (nsid)
|
---|
440 | {
|
---|
441 | uuid = QUuid (
|
---|
442 | nsid->m0, nsid->m1, nsid->m2,
|
---|
443 | nsid->m3[0], nsid->m3[1], nsid->m3[2], nsid->m3[3],
|
---|
444 | nsid->m3[4], nsid->m3[5], nsid->m3[6], nsid->m3[7]);
|
---|
445 | nsMemory::Free (nsid);
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | operator nsID **() { return &nsid; }
|
---|
450 |
|
---|
451 | private:
|
---|
452 |
|
---|
453 | QUuid &uuid;
|
---|
454 | nsID *nsid;
|
---|
455 | };
|
---|
456 |
|
---|
457 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
458 |
|
---|
459 | void fetchErrorInfo (IUnknown * /*callee*/, const GUID * /*calleeIID*/) const {}
|
---|
460 |
|
---|
461 | mutable HRESULT mRC;
|
---|
462 |
|
---|
463 | friend class COMErrorInfo;
|
---|
464 | };
|
---|
465 |
|
---|
466 | /////////////////////////////////////////////////////////////////////////////
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Alternative base class for the CInterface template that adds
|
---|
470 | * the errorInfo() method for providing extended error info about
|
---|
471 | * unsuccessful invocation of the last called interface method.
|
---|
472 | */
|
---|
473 | class COMBaseWithEI : public COMBase
|
---|
474 | {
|
---|
475 | public:
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Returns error info set by the last unsuccessfully invoked interface
|
---|
479 | * method. Returned error info is useful only if CInterface::lastRC()
|
---|
480 | * represents a failure or a warning (i.e. CInterface::isReallyOk() is
|
---|
481 | * false).
|
---|
482 | */
|
---|
483 | COMErrorInfo errorInfo() const { return mErrInfo; }
|
---|
484 |
|
---|
485 | protected:
|
---|
486 |
|
---|
487 | /* no arbitrary instance creations */
|
---|
488 | COMBaseWithEI() : COMBase () {};
|
---|
489 |
|
---|
490 | void fetchErrorInfo (IUnknown *callee, const GUID *calleeIID) const
|
---|
491 | {
|
---|
492 | mErrInfo.fetchFromCurrentThread (callee, calleeIID);
|
---|
493 | }
|
---|
494 |
|
---|
495 | mutable COMErrorInfo mErrInfo;
|
---|
496 | };
|
---|
497 |
|
---|
498 | /////////////////////////////////////////////////////////////////////////////
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Simple class that encapsulates the result code and COMErrorInfo.
|
---|
502 | */
|
---|
503 | class COMResult
|
---|
504 | {
|
---|
505 | public:
|
---|
506 |
|
---|
507 | COMResult() : mRC (S_OK) {}
|
---|
508 |
|
---|
509 | /** Queries the current result code and error info from the given component */
|
---|
510 | COMResult (const COMBase &aComponent)
|
---|
511 | {
|
---|
512 | mErrInfo = aComponent.errorInfo();
|
---|
513 | mRC = aComponent.lastRC();
|
---|
514 | }
|
---|
515 |
|
---|
516 | /** Queries the current result code and error info from the given component */
|
---|
517 | COMResult &operator= (const COMBase &aComponent)
|
---|
518 | {
|
---|
519 | mErrInfo = aComponent.errorInfo();
|
---|
520 | mRC = aComponent.lastRC();
|
---|
521 | return *this;
|
---|
522 | }
|
---|
523 |
|
---|
524 | bool isNull() const { return mErrInfo.isNull(); }
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Returns @c true if the result code repesents success (with or without
|
---|
528 | * warnings).
|
---|
529 | */
|
---|
530 | bool isOk() const { return SUCCEEDED (mRC); }
|
---|
531 |
|
---|
532 | /**
|
---|
533 | * Returns @c true if the result code represends success with one or more
|
---|
534 | * warnings.
|
---|
535 | */
|
---|
536 | bool isWarning() const { return SUCCEEDED_WARNING (mRC); }
|
---|
537 |
|
---|
538 | /**
|
---|
539 | * Returns @c true if the result code represends success with no warnings.
|
---|
540 | */
|
---|
541 | bool isReallyOk() const { return mRC == S_OK; }
|
---|
542 |
|
---|
543 | COMErrorInfo errorInfo() const { return mErrInfo; }
|
---|
544 | HRESULT rc() const { return mRC; }
|
---|
545 |
|
---|
546 | private:
|
---|
547 |
|
---|
548 | COMErrorInfo mErrInfo;
|
---|
549 | HRESULT mRC;
|
---|
550 | };
|
---|
551 |
|
---|
552 | /////////////////////////////////////////////////////////////////////////////
|
---|
553 |
|
---|
554 | class CUnknown;
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Wrapper template class for all interfaces.
|
---|
558 | *
|
---|
559 | * All interface methods named as they are in the original, i.e. starting
|
---|
560 | * with the capital letter. All utility non-interface methods are named
|
---|
561 | * starting with the small letter. Utility methods should be not normally
|
---|
562 | * called by the end-user client application.
|
---|
563 | *
|
---|
564 | * @param I interface class (i.e. IUnknown/nsISupports derivant)
|
---|
565 | * @param B base class, either COMBase (by default) or COMBaseWithEI
|
---|
566 | */
|
---|
567 | template <class I, class B = COMBase>
|
---|
568 | class CInterface : public B
|
---|
569 | {
|
---|
570 | public:
|
---|
571 |
|
---|
572 | typedef B Base;
|
---|
573 | typedef I Iface;
|
---|
574 |
|
---|
575 | /* constructors & destructor */
|
---|
576 |
|
---|
577 | CInterface() : mIface (NULL) {}
|
---|
578 |
|
---|
579 | CInterface (const CInterface &that) : B (that), mIface (that.mIface)
|
---|
580 | {
|
---|
581 | addref (mIface);
|
---|
582 | }
|
---|
583 |
|
---|
584 | CInterface (const CUnknown &that);
|
---|
585 |
|
---|
586 | CInterface (I *i) : mIface (i) { addref (mIface); }
|
---|
587 |
|
---|
588 | virtual ~CInterface() { release (mIface); }
|
---|
589 |
|
---|
590 | /* utility methods */
|
---|
591 |
|
---|
592 | void createInstance (const CLSID &clsid)
|
---|
593 | {
|
---|
594 | AssertMsg (!mIface, ("Instance is already non-NULL\n"));
|
---|
595 | if (!mIface)
|
---|
596 | {
|
---|
597 | #if !defined (VBOX_WITH_XPCOM)
|
---|
598 |
|
---|
599 | B::mRC = CoCreateInstance (clsid, NULL, CLSCTX_ALL,
|
---|
600 | _ATL_IIDOF (I), (void **) &mIface);
|
---|
601 |
|
---|
602 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
603 |
|
---|
604 | nsCOMPtr <nsIComponentManager> manager;
|
---|
605 | B::mRC = NS_GetComponentManager (getter_AddRefs (manager));
|
---|
606 | if (SUCCEEDED (B::mRC))
|
---|
607 | B::mRC = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I),
|
---|
608 | (void **) &mIface);
|
---|
609 |
|
---|
610 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
611 |
|
---|
612 | /* fetch error info, but don't assert if it's missing -- many other
|
---|
613 | * reasons can lead to an error (w/o providing error info), not only
|
---|
614 | * the instance initialization code (that should always provide it) */
|
---|
615 | B::fetchErrorInfo (NULL, NULL);
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | void attach (I *i)
|
---|
620 | {
|
---|
621 | /* be aware of self (from COM point of view) assignment */
|
---|
622 | I *old_iface = mIface;
|
---|
623 | mIface = i;
|
---|
624 | addref (mIface);
|
---|
625 | release (old_iface);
|
---|
626 | B::mRC = S_OK;
|
---|
627 | };
|
---|
628 |
|
---|
629 | void attachUnknown (IUnknown *i)
|
---|
630 | {
|
---|
631 | /* be aware of self (from COM point of view) assignment */
|
---|
632 | I *old_iface = mIface;
|
---|
633 | mIface = NULL;
|
---|
634 | B::mRC = S_OK;
|
---|
635 | if (i)
|
---|
636 | #if !defined (VBOX_WITH_XPCOM)
|
---|
637 | B::mRC = i->QueryInterface (_ATL_IIDOF (I), (void **) &mIface);
|
---|
638 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
639 | B::mRC = i->QueryInterface (NS_GET_IID (I), (void **) &mIface);
|
---|
640 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
641 | release (old_iface);
|
---|
642 | };
|
---|
643 |
|
---|
644 | void detach() { release (mIface); mIface = NULL; }
|
---|
645 |
|
---|
646 | bool isNull() const { return mIface == NULL; }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Returns @c true if the result code repesents success (with or without
|
---|
650 | * warnings).
|
---|
651 | */
|
---|
652 | bool isOk() const { return !isNull() && SUCCEEDED (B::mRC); }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Returns @c true if the result code represends success with one or more
|
---|
656 | * warnings.
|
---|
657 | */
|
---|
658 | bool isWarning() const { return !isNull() && SUCCEEDED_WARNING (B::mRC); }
|
---|
659 |
|
---|
660 | /**
|
---|
661 | * Returns @c true if the result code represends success with no warnings.
|
---|
662 | */
|
---|
663 | bool isReallyOk() const { return !isNull() && B::mRC == S_OK; }
|
---|
664 |
|
---|
665 | /* utility operators */
|
---|
666 |
|
---|
667 | CInterface &operator= (const CInterface &that)
|
---|
668 | {
|
---|
669 | attach (that.mIface);
|
---|
670 | B::operator= (that);
|
---|
671 | return *this;
|
---|
672 | }
|
---|
673 |
|
---|
674 | I *iface() const { return mIface; }
|
---|
675 |
|
---|
676 | bool operator== (const CInterface &that) const { return mIface == that.mIface; }
|
---|
677 | bool operator!= (const CInterface &that) const { return mIface != that.mIface; }
|
---|
678 |
|
---|
679 | CInterface &operator= (const CUnknown &that);
|
---|
680 |
|
---|
681 | protected:
|
---|
682 |
|
---|
683 | static void addref (I *i) { if (i) i->AddRef(); }
|
---|
684 | static void release (I *i) { if (i) i->Release(); }
|
---|
685 |
|
---|
686 | mutable I *mIface;
|
---|
687 | };
|
---|
688 |
|
---|
689 | /////////////////////////////////////////////////////////////////////////////
|
---|
690 |
|
---|
691 | class CUnknown : public CInterface <IUnknown, COMBaseWithEI>
|
---|
692 | {
|
---|
693 | public:
|
---|
694 |
|
---|
695 | CUnknown() : CInterface <IUnknown, COMBaseWithEI> () {}
|
---|
696 |
|
---|
697 | template <class C>
|
---|
698 | explicit CUnknown (const C &that)
|
---|
699 | {
|
---|
700 | mIface = NULL;
|
---|
701 | if (that.mIface)
|
---|
702 | #if !defined (VBOX_WITH_XPCOM)
|
---|
703 | mRC = that.mIface->QueryInterface (_ATL_IIDOF (IUnknown), (void**) &mIface);
|
---|
704 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
705 | mRC = that.mIface->QueryInterface (NS_GET_IID (IUnknown), (void**) &mIface);
|
---|
706 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
707 | if (SUCCEEDED (mRC))
|
---|
708 | {
|
---|
709 | mRC = that.lastRC();
|
---|
710 | mErrInfo = that.errorInfo();
|
---|
711 | }
|
---|
712 | }
|
---|
713 |
|
---|
714 | /* specialization for CUnknown */
|
---|
715 | CUnknown (const CUnknown &that) : CInterface <IUnknown, COMBaseWithEI> ()
|
---|
716 | {
|
---|
717 | mIface = that.mIface;
|
---|
718 | addref (mIface);
|
---|
719 | COMBaseWithEI::operator= (that);
|
---|
720 | }
|
---|
721 |
|
---|
722 | template <class C>
|
---|
723 | CUnknown &operator= (const C &that)
|
---|
724 | {
|
---|
725 | /* be aware of self (from COM point of view) assignment */
|
---|
726 | IUnknown *old_iface = mIface;
|
---|
727 | mIface = NULL;
|
---|
728 | mRC = S_OK;
|
---|
729 | #if !defined (VBOX_WITH_XPCOM)
|
---|
730 | if (that.mIface)
|
---|
731 | mRC = that.mIface->QueryInterface (_ATL_IIDOF (IUnknown), (void**) &mIface);
|
---|
732 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
733 | if (that.mIface)
|
---|
734 | mRC = that.mIface->QueryInterface (NS_GET_IID (IUnknown), (void**) &mIface);
|
---|
735 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
736 | if (SUCCEEDED (mRC))
|
---|
737 | {
|
---|
738 | mRC = that.lastRC();
|
---|
739 | mErrInfo = that.errorInfo();
|
---|
740 | }
|
---|
741 | release (old_iface);
|
---|
742 | return *this;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /* specialization for CUnknown */
|
---|
746 | CUnknown &operator= (const CUnknown &that)
|
---|
747 | {
|
---|
748 | attach (that.mIface);
|
---|
749 | COMBaseWithEI::operator= (that);
|
---|
750 | return *this;
|
---|
751 | }
|
---|
752 |
|
---|
753 | /* @internal Used in wrappers. */
|
---|
754 | IUnknown *&ifaceRef() { return mIface; };
|
---|
755 | };
|
---|
756 |
|
---|
757 | /* inlined CInterface methods that use CUnknown */
|
---|
758 |
|
---|
759 | template <class I, class B>
|
---|
760 | inline CInterface <I, B>::CInterface (const CUnknown &that)
|
---|
761 | : mIface (NULL)
|
---|
762 | {
|
---|
763 | attachUnknown (that.iface());
|
---|
764 | if (SUCCEEDED (B::mRC))
|
---|
765 | B::operator= ((B &) that);
|
---|
766 | }
|
---|
767 |
|
---|
768 | template <class I, class B>
|
---|
769 | inline CInterface <I, B> &CInterface <I, B>::operator =(const CUnknown &that)
|
---|
770 | {
|
---|
771 | attachUnknown (that.iface());
|
---|
772 | if (SUCCEEDED (B::mRC))
|
---|
773 | B::operator= ((B &) that);
|
---|
774 | return *this;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /////////////////////////////////////////////////////////////////////////////
|
---|
778 |
|
---|
779 | /* include the generated header containing concrete wrapper definitions */
|
---|
780 | #include "COMWrappers.h"
|
---|
781 |
|
---|
782 | /** @} */
|
---|
783 |
|
---|
784 | #endif // __COMDefs_h__
|
---|