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-2008 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 definitions 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 Abstraction 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 QVector objects. QVector templates declared
|
---|
45 | * with a type that corresponds to the COM type of elements in the array using
|
---|
46 | * normal Qt-COM type mapping rules. Here is a code example that demonstrates
|
---|
47 | * how to call interface methods that take and return arrays (this example is
|
---|
48 | * based on examples given in @ref grp_COM_arrays):
|
---|
49 | * @code
|
---|
50 |
|
---|
51 | CSomething component;
|
---|
52 |
|
---|
53 | // ...
|
---|
54 |
|
---|
55 | QVector <LONG> in (3);
|
---|
56 | in [0] = -1;
|
---|
57 | in [1] = -2;
|
---|
58 | in [2] = -3;
|
---|
59 |
|
---|
60 | QVector <LONG> out;
|
---|
61 | QVector <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 | /* Qt includes */
|
---|
83 | #include <QString>
|
---|
84 | #include <QUuid>
|
---|
85 | #include <QVector>
|
---|
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 QVector <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, QVector <QT> &aVec)
|
---|
238 | {
|
---|
239 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
240 | }
|
---|
241 |
|
---|
242 | template <typename QT, typename CT>
|
---|
243 | static void ToSafeArray (const QVector <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, QVector <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 QVector <T> &aVec, com::SafeArray <T> &aArr)
|
---|
258 | {
|
---|
259 | aArr.reset (aVec.size());
|
---|
260 | for (int i = 0; i < aVec.size(); ++i)
|
---|
261 | aArr [i] = aVec.at (i);
|
---|
262 | }
|
---|
263 |
|
---|
264 | template <typename T>
|
---|
265 | static void FromSafeArray (const com::SafeArray <T> &aArr, QVector <T> &aVec)
|
---|
266 | {
|
---|
267 | aVec.resize (aArr.size());
|
---|
268 | for (int i = 0; i < aVec.size(); ++i)
|
---|
269 | aVec [i] = aArr [i];
|
---|
270 | }
|
---|
271 |
|
---|
272 | /* Arrays of strings */
|
---|
273 |
|
---|
274 | static void ToSafeArray (const QVector <QString> &aVec,
|
---|
275 | com::SafeArray <BSTR> &aArr);
|
---|
276 | static void FromSafeArray (const com::SafeArray <BSTR> &aArr,
|
---|
277 | QVector <QString> &aVec);
|
---|
278 |
|
---|
279 | /* Arrays of interface pointers. Note: we need a separate pair of names
|
---|
280 | * only because the MSVC8 template matching algorithm is poor and tries to
|
---|
281 | * instantiate a com::SafeIfaceArray <BSTR> (!!!) template otherwise for
|
---|
282 | * *no* reason and fails. Note that it's also not possible to choose the
|
---|
283 | * correct function by specifying template arguments explicitly because then
|
---|
284 | * it starts to try to instantiate the com::SafeArray <I> template for
|
---|
285 | * *no* reason again and fails too. Definitely, broken. Works in GCC like a
|
---|
286 | * charm. */
|
---|
287 |
|
---|
288 | template <class CI, class I>
|
---|
289 | static void ToSafeIfaceArray (const QVector <CI> &aVec,
|
---|
290 | com::SafeIfaceArray <I> &aArr)
|
---|
291 | {
|
---|
292 | aArr.reset (aVec.size());
|
---|
293 | for (int i = 0; i < aVec.size(); ++i)
|
---|
294 | {
|
---|
295 | aArr [i] = aVec.at (i);
|
---|
296 | if (aArr [i])
|
---|
297 | aArr [i]->AddRef();
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | template <class I, class CI>
|
---|
302 | static void FromSafeIfaceArray (const com::SafeIfaceArray <I> &aArr,
|
---|
303 | QVector <CI> &aVec)
|
---|
304 | {
|
---|
305 | aVec.resize (aArr.size());
|
---|
306 | for (int i = 0; i < aVec.size(); ++i)
|
---|
307 | aVec [i].attach (aArr [i]);
|
---|
308 | }
|
---|
309 |
|
---|
310 | protected:
|
---|
311 |
|
---|
312 | /* no arbitrary instance creations */
|
---|
313 | COMBase() : mRC (S_OK) {};
|
---|
314 |
|
---|
315 | #if defined (VBOX_WITH_XPCOM)
|
---|
316 | static XPCOMEventQSocketListener *sSocketListener;
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | /** Adapter to pass QString as input BSTR params */
|
---|
320 | class BSTRIn
|
---|
321 | {
|
---|
322 | public:
|
---|
323 |
|
---|
324 | BSTRIn (const QString &s) : bstr (SysAllocString ((const OLECHAR *) s.utf16())) {}
|
---|
325 |
|
---|
326 | ~BSTRIn()
|
---|
327 | {
|
---|
328 | if (bstr)
|
---|
329 | SysFreeString (bstr);
|
---|
330 | }
|
---|
331 |
|
---|
332 | operator BSTR() const { return bstr; }
|
---|
333 |
|
---|
334 | private:
|
---|
335 |
|
---|
336 | BSTR bstr;
|
---|
337 | };
|
---|
338 |
|
---|
339 | /** Adapter to pass QString as output BSTR params */
|
---|
340 | class BSTROut
|
---|
341 | {
|
---|
342 | public:
|
---|
343 |
|
---|
344 | BSTROut (QString &s) : str (s), bstr (0) {}
|
---|
345 |
|
---|
346 | ~BSTROut()
|
---|
347 | {
|
---|
348 | if (bstr) {
|
---|
349 | str = QString::fromUtf16 (bstr);
|
---|
350 | SysFreeString (bstr);
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | operator BSTR *() { return &bstr; }
|
---|
355 |
|
---|
356 | private:
|
---|
357 |
|
---|
358 | QString &str;
|
---|
359 | BSTR bstr;
|
---|
360 | };
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Adapter to pass K* enums as output COM enum params (*_T).
|
---|
364 | *
|
---|
365 | * @param QE K* enum.
|
---|
366 | * @param CE COM enum.
|
---|
367 | */
|
---|
368 | template <typename QE, typename CE>
|
---|
369 | class ENUMOut
|
---|
370 | {
|
---|
371 | public:
|
---|
372 |
|
---|
373 | ENUMOut (QE &e) : qe (e), ce ((CE) 0) {}
|
---|
374 | ~ENUMOut() { qe = (QE) ce; }
|
---|
375 | operator CE *() { return &ce; }
|
---|
376 |
|
---|
377 | private:
|
---|
378 |
|
---|
379 | QE &qe;
|
---|
380 | CE ce;
|
---|
381 | };
|
---|
382 |
|
---|
383 | #if !defined (VBOX_WITH_XPCOM)
|
---|
384 |
|
---|
385 | /** Adapter to pass QUuid as input GUID params */
|
---|
386 | static GUID GUIDIn (const QUuid &uuid) { return uuid; }
|
---|
387 |
|
---|
388 | /** Adapter to pass QUuid as output GUID params */
|
---|
389 | class GUIDOut
|
---|
390 | {
|
---|
391 | public:
|
---|
392 |
|
---|
393 | GUIDOut (QUuid &id) : uuid (id)
|
---|
394 | {
|
---|
395 | ::memset (&guid, 0, sizeof (GUID));
|
---|
396 | }
|
---|
397 |
|
---|
398 | ~GUIDOut()
|
---|
399 | {
|
---|
400 | uuid = QUuid (
|
---|
401 | guid.Data1, guid.Data2, guid.Data3,
|
---|
402 | guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
|
---|
403 | guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
|
---|
404 | }
|
---|
405 |
|
---|
406 | operator GUID *() { return &guid; }
|
---|
407 |
|
---|
408 | private:
|
---|
409 |
|
---|
410 | QUuid &uuid;
|
---|
411 | GUID guid;
|
---|
412 | };
|
---|
413 |
|
---|
414 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
415 |
|
---|
416 | /** Adapter to pass QUuid as input GUID params */
|
---|
417 | static const nsID &GUIDIn (const QUuid &uuid)
|
---|
418 | {
|
---|
419 | return *(const nsID *) &uuid;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** Adapter to pass QUuid as output GUID params */
|
---|
423 | class GUIDOut
|
---|
424 | {
|
---|
425 | public:
|
---|
426 |
|
---|
427 | GUIDOut (QUuid &id) : uuid (id), nsid (0) {}
|
---|
428 |
|
---|
429 | ~GUIDOut()
|
---|
430 | {
|
---|
431 | if (nsid)
|
---|
432 | {
|
---|
433 | uuid = QUuid (
|
---|
434 | nsid->m0, nsid->m1, nsid->m2,
|
---|
435 | nsid->m3[0], nsid->m3[1], nsid->m3[2], nsid->m3[3],
|
---|
436 | nsid->m3[4], nsid->m3[5], nsid->m3[6], nsid->m3[7]);
|
---|
437 | nsMemory::Free (nsid);
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | operator nsID **() { return &nsid; }
|
---|
442 |
|
---|
443 | private:
|
---|
444 |
|
---|
445 | QUuid &uuid;
|
---|
446 | nsID *nsid;
|
---|
447 | };
|
---|
448 |
|
---|
449 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
450 |
|
---|
451 | void fetchErrorInfo (IUnknown * /*callee*/, const GUID * /*calleeIID*/) const {}
|
---|
452 |
|
---|
453 | mutable HRESULT mRC;
|
---|
454 |
|
---|
455 | friend class COMErrorInfo;
|
---|
456 | };
|
---|
457 |
|
---|
458 | /////////////////////////////////////////////////////////////////////////////
|
---|
459 |
|
---|
460 | /**
|
---|
461 | * Alternative base class for the CInterface template that adds
|
---|
462 | * the errorInfo() method for providing extended error info about
|
---|
463 | * unsuccessful invocation of the last called interface method.
|
---|
464 | */
|
---|
465 | class COMBaseWithEI : public COMBase
|
---|
466 | {
|
---|
467 | public:
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Returns error info set by the last unsuccessfully invoked interface
|
---|
471 | * method. Returned error info is useful only if CInterface::lastRC()
|
---|
472 | * represents a failure or a warning (i.e. CInterface::isReallyOk() is
|
---|
473 | * false).
|
---|
474 | */
|
---|
475 | COMErrorInfo errorInfo() const { return mErrInfo; }
|
---|
476 |
|
---|
477 | protected:
|
---|
478 |
|
---|
479 | /* no arbitrary instance creations */
|
---|
480 | COMBaseWithEI() : COMBase () {};
|
---|
481 |
|
---|
482 | void fetchErrorInfo (IUnknown *callee, const GUID *calleeIID) const
|
---|
483 | {
|
---|
484 | mErrInfo.fetchFromCurrentThread (callee, calleeIID);
|
---|
485 | }
|
---|
486 |
|
---|
487 | mutable COMErrorInfo mErrInfo;
|
---|
488 | };
|
---|
489 |
|
---|
490 | /////////////////////////////////////////////////////////////////////////////
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Simple class that encapsulates the result code and COMErrorInfo.
|
---|
494 | */
|
---|
495 | class COMResult
|
---|
496 | {
|
---|
497 | public:
|
---|
498 |
|
---|
499 | COMResult() : mRC (S_OK) {}
|
---|
500 |
|
---|
501 | /** Queries the current result code and error info from the given component */
|
---|
502 | COMResult (const COMBase &aComponent)
|
---|
503 | {
|
---|
504 | mErrInfo = aComponent.errorInfo();
|
---|
505 | mRC = aComponent.lastRC();
|
---|
506 | }
|
---|
507 |
|
---|
508 | /** Queries the current result code and error info from the given component */
|
---|
509 | COMResult &operator= (const COMBase &aComponent)
|
---|
510 | {
|
---|
511 | mErrInfo = aComponent.errorInfo();
|
---|
512 | mRC = aComponent.lastRC();
|
---|
513 | return *this;
|
---|
514 | }
|
---|
515 |
|
---|
516 | bool isNull() const { return mErrInfo.isNull(); }
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Returns @c true if the result code represents success (with or without
|
---|
520 | * warnings).
|
---|
521 | */
|
---|
522 | bool isOk() const { return SUCCEEDED (mRC); }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Returns @c true if the result code represents success with one or more
|
---|
526 | * warnings.
|
---|
527 | */
|
---|
528 | bool isWarning() const { return SUCCEEDED_WARNING (mRC); }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Returns @c true if the result code represents success with no warnings.
|
---|
532 | */
|
---|
533 | bool isReallyOk() const { return mRC == S_OK; }
|
---|
534 |
|
---|
535 | COMErrorInfo errorInfo() const { return mErrInfo; }
|
---|
536 | HRESULT rc() const { return mRC; }
|
---|
537 |
|
---|
538 | private:
|
---|
539 |
|
---|
540 | COMErrorInfo mErrInfo;
|
---|
541 | HRESULT mRC;
|
---|
542 | };
|
---|
543 |
|
---|
544 | /////////////////////////////////////////////////////////////////////////////
|
---|
545 |
|
---|
546 | class CUnknown;
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * Wrapper template class for all interfaces.
|
---|
550 | *
|
---|
551 | * All interface methods named as they are in the original, i.e. starting
|
---|
552 | * with the capital letter. All utility non-interface methods are named
|
---|
553 | * starting with the small letter. Utility methods should be not normally
|
---|
554 | * called by the end-user client application.
|
---|
555 | *
|
---|
556 | * @param I interface class (i.e. IUnknown/nsISupports derivant)
|
---|
557 | * @param B base class, either COMBase (by default) or COMBaseWithEI
|
---|
558 | */
|
---|
559 | template <class I, class B = COMBase>
|
---|
560 | class CInterface : public B
|
---|
561 | {
|
---|
562 | public:
|
---|
563 |
|
---|
564 | typedef B Base;
|
---|
565 | typedef I Iface;
|
---|
566 |
|
---|
567 | /* constructors & destructor */
|
---|
568 |
|
---|
569 | CInterface() : mIface (NULL) {}
|
---|
570 |
|
---|
571 | CInterface (const CInterface &that) : B (that), mIface (that.mIface)
|
---|
572 | {
|
---|
573 | addref (mIface);
|
---|
574 | }
|
---|
575 |
|
---|
576 | CInterface (const CUnknown &that);
|
---|
577 |
|
---|
578 | CInterface (I *i) : mIface (i) { addref (mIface); }
|
---|
579 |
|
---|
580 | virtual ~CInterface() { release (mIface); }
|
---|
581 |
|
---|
582 | /* utility methods */
|
---|
583 |
|
---|
584 | void createInstance (const CLSID &clsid)
|
---|
585 | {
|
---|
586 | AssertMsg (!mIface, ("Instance is already non-NULL\n"));
|
---|
587 | if (!mIface)
|
---|
588 | {
|
---|
589 | #if !defined (VBOX_WITH_XPCOM)
|
---|
590 |
|
---|
591 | B::mRC = CoCreateInstance (clsid, NULL, CLSCTX_ALL,
|
---|
592 | _ATL_IIDOF (I), (void **) &mIface);
|
---|
593 |
|
---|
594 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
595 |
|
---|
596 | nsCOMPtr <nsIComponentManager> manager;
|
---|
597 | B::mRC = NS_GetComponentManager (getter_AddRefs (manager));
|
---|
598 | if (SUCCEEDED (B::mRC))
|
---|
599 | B::mRC = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I),
|
---|
600 | (void **) &mIface);
|
---|
601 |
|
---|
602 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
603 |
|
---|
604 | /* fetch error info, but don't assert if it's missing -- many other
|
---|
605 | * reasons can lead to an error (w/o providing error info), not only
|
---|
606 | * the instance initialization code (that should always provide it) */
|
---|
607 | B::fetchErrorInfo (NULL, NULL);
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | void attach (I *i)
|
---|
612 | {
|
---|
613 | /* be aware of self (from COM point of view) assignment */
|
---|
614 | I *old_iface = mIface;
|
---|
615 | mIface = i;
|
---|
616 | addref (mIface);
|
---|
617 | release (old_iface);
|
---|
618 | B::mRC = S_OK;
|
---|
619 | };
|
---|
620 |
|
---|
621 | void attachUnknown (IUnknown *i)
|
---|
622 | {
|
---|
623 | /* be aware of self (from COM point of view) assignment */
|
---|
624 | I *old_iface = mIface;
|
---|
625 | mIface = NULL;
|
---|
626 | B::mRC = S_OK;
|
---|
627 | if (i)
|
---|
628 | #if !defined (VBOX_WITH_XPCOM)
|
---|
629 | B::mRC = i->QueryInterface (_ATL_IIDOF (I), (void **) &mIface);
|
---|
630 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
631 | B::mRC = i->QueryInterface (NS_GET_IID (I), (void **) &mIface);
|
---|
632 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
633 | release (old_iface);
|
---|
634 | };
|
---|
635 |
|
---|
636 | void detach() { release (mIface); mIface = NULL; }
|
---|
637 |
|
---|
638 | bool isNull() const { return mIface == NULL; }
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * Returns @c true if the result code represents success (with or without
|
---|
642 | * warnings).
|
---|
643 | */
|
---|
644 | bool isOk() const { return !isNull() && SUCCEEDED (B::mRC); }
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Returns @c true if the result code represents success with one or more
|
---|
648 | * warnings.
|
---|
649 | */
|
---|
650 | bool isWarning() const { return !isNull() && SUCCEEDED_WARNING (B::mRC); }
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Returns @c true if the result code represents success with no warnings.
|
---|
654 | */
|
---|
655 | bool isReallyOk() const { return !isNull() && B::mRC == S_OK; }
|
---|
656 |
|
---|
657 | /* utility operators */
|
---|
658 |
|
---|
659 | CInterface &operator= (const CInterface &that)
|
---|
660 | {
|
---|
661 | attach (that.mIface);
|
---|
662 | B::operator= (that);
|
---|
663 | return *this;
|
---|
664 | }
|
---|
665 |
|
---|
666 | I *iface() const { return mIface; }
|
---|
667 |
|
---|
668 | bool operator== (const CInterface &that) const { return mIface == that.mIface; }
|
---|
669 | bool operator!= (const CInterface &that) const { return mIface != that.mIface; }
|
---|
670 |
|
---|
671 | CInterface &operator= (const CUnknown &that);
|
---|
672 |
|
---|
673 | protected:
|
---|
674 |
|
---|
675 | static void addref (I *i) { if (i) i->AddRef(); }
|
---|
676 | static void release (I *i) { if (i) i->Release(); }
|
---|
677 |
|
---|
678 | mutable I *mIface;
|
---|
679 | };
|
---|
680 |
|
---|
681 | /////////////////////////////////////////////////////////////////////////////
|
---|
682 |
|
---|
683 | class CUnknown : public CInterface <IUnknown, COMBaseWithEI>
|
---|
684 | {
|
---|
685 | public:
|
---|
686 |
|
---|
687 | CUnknown() : CInterface <IUnknown, COMBaseWithEI> () {}
|
---|
688 |
|
---|
689 | template <class C>
|
---|
690 | explicit CUnknown (const C &that)
|
---|
691 | {
|
---|
692 | mIface = NULL;
|
---|
693 | if (that.mIface)
|
---|
694 | #if !defined (VBOX_WITH_XPCOM)
|
---|
695 | mRC = that.mIface->QueryInterface (_ATL_IIDOF (IUnknown), (void**) &mIface);
|
---|
696 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
697 | mRC = that.mIface->QueryInterface (NS_GET_IID (IUnknown), (void**) &mIface);
|
---|
698 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
699 | if (SUCCEEDED (mRC))
|
---|
700 | {
|
---|
701 | mRC = that.lastRC();
|
---|
702 | mErrInfo = that.errorInfo();
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | /* specialization for CUnknown */
|
---|
707 | CUnknown (const CUnknown &that) : CInterface <IUnknown, COMBaseWithEI> ()
|
---|
708 | {
|
---|
709 | mIface = that.mIface;
|
---|
710 | addref (mIface);
|
---|
711 | COMBaseWithEI::operator= (that);
|
---|
712 | }
|
---|
713 |
|
---|
714 | template <class C>
|
---|
715 | CUnknown &operator= (const C &that)
|
---|
716 | {
|
---|
717 | /* be aware of self (from COM point of view) assignment */
|
---|
718 | IUnknown *old_iface = mIface;
|
---|
719 | mIface = NULL;
|
---|
720 | mRC = S_OK;
|
---|
721 | #if !defined (VBOX_WITH_XPCOM)
|
---|
722 | if (that.mIface)
|
---|
723 | mRC = that.mIface->QueryInterface (_ATL_IIDOF (IUnknown), (void**) &mIface);
|
---|
724 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
725 | if (that.mIface)
|
---|
726 | mRC = that.mIface->QueryInterface (NS_GET_IID (IUnknown), (void**) &mIface);
|
---|
727 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
728 | if (SUCCEEDED (mRC))
|
---|
729 | {
|
---|
730 | mRC = that.lastRC();
|
---|
731 | mErrInfo = that.errorInfo();
|
---|
732 | }
|
---|
733 | release (old_iface);
|
---|
734 | return *this;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /* specialization for CUnknown */
|
---|
738 | CUnknown &operator= (const CUnknown &that)
|
---|
739 | {
|
---|
740 | attach (that.mIface);
|
---|
741 | COMBaseWithEI::operator= (that);
|
---|
742 | return *this;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /* @internal Used in wrappers. */
|
---|
746 | IUnknown *&ifaceRef() { return mIface; };
|
---|
747 | };
|
---|
748 |
|
---|
749 | /* inlined CInterface methods that use CUnknown */
|
---|
750 |
|
---|
751 | template <class I, class B>
|
---|
752 | inline CInterface <I, B>::CInterface (const CUnknown &that)
|
---|
753 | : mIface (NULL)
|
---|
754 | {
|
---|
755 | attachUnknown (that.iface());
|
---|
756 | if (SUCCEEDED (B::mRC))
|
---|
757 | B::operator= ((B &) that);
|
---|
758 | }
|
---|
759 |
|
---|
760 | template <class I, class B>
|
---|
761 | inline CInterface <I, B> &CInterface <I, B>::operator =(const CUnknown &that)
|
---|
762 | {
|
---|
763 | attachUnknown (that.iface());
|
---|
764 | if (SUCCEEDED (B::mRC))
|
---|
765 | B::operator= ((B &) that);
|
---|
766 | return *this;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /////////////////////////////////////////////////////////////////////////////
|
---|
770 |
|
---|
771 | /* include the generated header containing concrete wrapper definitions */
|
---|
772 | #include "COMWrappers.h"
|
---|
773 |
|
---|
774 | /** @} */
|
---|
775 |
|
---|
776 | #endif // __COMDefs_h__
|
---|