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