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