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 | #if !defined (VBOX_WITH_XPCOM)
|
---|
94 |
|
---|
95 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
96 |
|
---|
97 | #include <nsXPCOM.h>
|
---|
98 | #include <nsMemory.h>
|
---|
99 | #include <nsIComponentManager.h>
|
---|
100 |
|
---|
101 | class XPCOMEventQSocketListener;
|
---|
102 |
|
---|
103 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
104 |
|
---|
105 |
|
---|
106 | /* VirtualBox interfaces declarations */
|
---|
107 | #if !defined (VBOX_WITH_XPCOM)
|
---|
108 | #include <VirtualBox.h>
|
---|
109 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
110 | #include <VirtualBox_XPCOM.h>
|
---|
111 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
112 |
|
---|
113 | #include "VBoxDefs.h"
|
---|
114 |
|
---|
115 |
|
---|
116 | /////////////////////////////////////////////////////////////////////////////
|
---|
117 |
|
---|
118 | class CVirtualBoxErrorInfo;
|
---|
119 |
|
---|
120 | /** Represents extended error information */
|
---|
121 | class COMErrorInfo
|
---|
122 | {
|
---|
123 | public:
|
---|
124 |
|
---|
125 | COMErrorInfo()
|
---|
126 | : mIsNull (true)
|
---|
127 | , mIsBasicAvailable (false), mIsFullAvailable (false)
|
---|
128 | , mResultCode (S_OK) {}
|
---|
129 |
|
---|
130 | COMErrorInfo (const CVirtualBoxErrorInfo &info) { init (info); }
|
---|
131 |
|
---|
132 | /* the default copy ctor and assignment op are ok */
|
---|
133 |
|
---|
134 | bool isNull() const { return mIsNull; }
|
---|
135 |
|
---|
136 | bool isBasicAvailable() const { return mIsBasicAvailable; }
|
---|
137 | bool isFullAvailable() const { return mIsFullAvailable; }
|
---|
138 |
|
---|
139 | HRESULT resultCode() const { return mResultCode; }
|
---|
140 | QUuid interfaceID() const { return mInterfaceID; }
|
---|
141 | QString component() const { return mComponent; }
|
---|
142 | QString text() const { return mText; }
|
---|
143 |
|
---|
144 | const COMErrorInfo *next() const { return mNext.get(); }
|
---|
145 |
|
---|
146 | QString interfaceName() const { return mInterfaceName; }
|
---|
147 | QUuid calleeIID() const { return mCalleeIID; }
|
---|
148 | QString calleeName() const { return mCalleeName; }
|
---|
149 |
|
---|
150 | private:
|
---|
151 |
|
---|
152 | void init (const CVirtualBoxErrorInfo &info);
|
---|
153 | void fetchFromCurrentThread (IUnknown *callee, const GUID *calleeIID);
|
---|
154 |
|
---|
155 | static QString getInterfaceNameFromIID (const QUuid &id);
|
---|
156 |
|
---|
157 | bool mIsNull : 1;
|
---|
158 | bool mIsBasicAvailable : 1;
|
---|
159 | bool mIsFullAvailable : 1;
|
---|
160 |
|
---|
161 | HRESULT mResultCode;
|
---|
162 | QUuid mInterfaceID;
|
---|
163 | QString mComponent;
|
---|
164 | QString mText;
|
---|
165 |
|
---|
166 | cppx::auto_copy_ptr <COMErrorInfo> mNext;
|
---|
167 |
|
---|
168 | QString mInterfaceName;
|
---|
169 | QUuid mCalleeIID;
|
---|
170 | QString mCalleeName;
|
---|
171 |
|
---|
172 | friend class COMBaseWithEI;
|
---|
173 | };
|
---|
174 |
|
---|
175 | /////////////////////////////////////////////////////////////////////////////
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Base COM class the CInterface template and all wrapper classes are derived
|
---|
179 | * from. Provides common functionality for all COM wrappers.
|
---|
180 | */
|
---|
181 | class COMBase
|
---|
182 | {
|
---|
183 | public:
|
---|
184 |
|
---|
185 | static HRESULT InitializeCOM();
|
---|
186 | static HRESULT CleanupCOM();
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Returns the result code of the last interface method called by the
|
---|
190 | * wrapper instance or the result of CInterface::createInstance()
|
---|
191 | * operation.
|
---|
192 | */
|
---|
193 | HRESULT lastRC() const { return mRC; }
|
---|
194 |
|
---|
195 | #if !defined (VBOX_WITH_XPCOM)
|
---|
196 |
|
---|
197 | /** Converts a GUID value to QUuid */
|
---|
198 | static QUuid ToQUuid (const GUID &id)
|
---|
199 | {
|
---|
200 | return QUuid (id.Data1, id.Data2, id.Data3,
|
---|
201 | id.Data4[0], id.Data4[1], id.Data4[2], id.Data4[3],
|
---|
202 | id.Data4[4], id.Data4[5], id.Data4[6], id.Data4[7]);
|
---|
203 | }
|
---|
204 |
|
---|
205 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
206 |
|
---|
207 | /** Converts a GUID value to QUuid */
|
---|
208 | static QUuid ToQUuid (const nsID &id)
|
---|
209 | {
|
---|
210 | return QUuid (id.m0, id.m1, id.m2,
|
---|
211 | id.m3[0], id.m3[1], id.m3[2], id.m3[3],
|
---|
212 | id.m3[4], id.m3[5], id.m3[6], id.m3[7]);
|
---|
213 | }
|
---|
214 |
|
---|
215 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
216 |
|
---|
217 | /* Arrays of arbitrary types */
|
---|
218 |
|
---|
219 | template <typename QT, typename CT>
|
---|
220 | static void ToSafeArray (const QVector <QT> &aVec, com::SafeArray <CT> &aArr)
|
---|
221 | {
|
---|
222 | Q_UNUSED (aVec);
|
---|
223 | Q_UNUSED (aArr);
|
---|
224 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
225 | }
|
---|
226 |
|
---|
227 | template <typename CT, typename QT>
|
---|
228 | static void FromSafeArray (const com::SafeArray <CT> &aArr, QVector <QT> &aVec)
|
---|
229 | {
|
---|
230 | Q_UNUSED (aArr);
|
---|
231 | Q_UNUSED (aVec);
|
---|
232 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
233 | }
|
---|
234 |
|
---|
235 | template <typename QT, typename CT>
|
---|
236 | static void ToSafeArray (const QVector <QT *> &aVec, com::SafeArray <CT *> &aArr)
|
---|
237 | {
|
---|
238 | Q_UNUSED (aVec);
|
---|
239 | Q_UNUSED (aArr);
|
---|
240 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
241 | }
|
---|
242 |
|
---|
243 | template <typename CT, typename QT>
|
---|
244 | static void FromSafeArray (const com::SafeArray <CT *> &aArr, QVector <QT *> &aVec)
|
---|
245 | {
|
---|
246 | Q_UNUSED (aArr);
|
---|
247 | Q_UNUSED (aVec);
|
---|
248 | AssertMsgFailedReturnVoid (("No conversion!\n"));
|
---|
249 | }
|
---|
250 |
|
---|
251 | /* Arrays of equal types */
|
---|
252 |
|
---|
253 | template <typename T>
|
---|
254 | static void ToSafeArray (const QVector <T> &aVec, com::SafeArray <T> &aArr)
|
---|
255 | {
|
---|
256 | aArr.reset (aVec.size());
|
---|
257 | for (int i = 0; i < aVec.size(); ++i)
|
---|
258 | aArr [i] = aVec.at (i);
|
---|
259 | }
|
---|
260 |
|
---|
261 | template <typename T>
|
---|
262 | static void FromSafeArray (const com::SafeArray <T> &aArr, QVector <T> &aVec)
|
---|
263 | {
|
---|
264 | aVec.resize (static_cast<int> (aArr.size()));
|
---|
265 | for (int i = 0; i < aVec.size(); ++i)
|
---|
266 | aVec [i] = aArr [i];
|
---|
267 | }
|
---|
268 |
|
---|
269 | /* Arrays of strings */
|
---|
270 |
|
---|
271 | static void ToSafeArray (const QVector <QString> &aVec,
|
---|
272 | com::SafeArray <BSTR> &aArr);
|
---|
273 | static void FromSafeArray (const com::SafeArray <BSTR> &aArr,
|
---|
274 | QVector <QString> &aVec);
|
---|
275 |
|
---|
276 | /* Arrays of GUID */
|
---|
277 |
|
---|
278 | static void ToSafeArray (const QVector <QUuid> &aVec,
|
---|
279 | com::SafeGUIDArray &aArr);
|
---|
280 | static void FromSafeArray (const com::SafeGUIDArray &aArr,
|
---|
281 | QVector <QUuid> &aVec);
|
---|
282 |
|
---|
283 | /* Arrays of enums. Does a cast similar to what ENUMOut does. */
|
---|
284 |
|
---|
285 | template <typename QE, typename CE>
|
---|
286 | static void ToSafeArray (const QVector <QE> &aVec,
|
---|
287 | com::SafeIfaceArray <CE> &aArr)
|
---|
288 | {
|
---|
289 | aArr.reset (static_cast <int> (aVec.size()));
|
---|
290 | for (int i = 0; i < aVec.size(); ++i)
|
---|
291 | aArr [i] = static_cast <CE> (aVec.at (i));
|
---|
292 | }
|
---|
293 |
|
---|
294 | template <typename CE, typename QE>
|
---|
295 | static void FromSafeArray (const com::SafeIfaceArray <CE> &aArr,
|
---|
296 | QVector <QE> &aVec)
|
---|
297 | {
|
---|
298 | aVec.resize (static_cast <int> (aArr.size()));
|
---|
299 | for (int i = 0; i < aVec.size(); ++i)
|
---|
300 | aVec [i] = static_cast <QE> (aArr [i]);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* Arrays of interface pointers. Note: we need a separate pair of names
|
---|
304 | * only because the MSVC8 template matching algorithm is poor and tries to
|
---|
305 | * instantiate a com::SafeIfaceArray <BSTR> (!!!) template otherwise for
|
---|
306 | * *no* reason and fails. Note that it's also not possible to choose the
|
---|
307 | * correct function by specifying template arguments explicitly because then
|
---|
308 | * it starts to try to instantiate the com::SafeArray <I> template for
|
---|
309 | * *no* reason again and fails too. Definitely, broken. Works in GCC like a
|
---|
310 | * charm. */
|
---|
311 |
|
---|
312 | template <class CI, class I>
|
---|
313 | static void ToSafeIfaceArray (const QVector <CI> &aVec,
|
---|
314 | com::SafeIfaceArray <I> &aArr)
|
---|
315 | {
|
---|
316 | aArr.reset (static_cast<int> (aVec.size()));
|
---|
317 | for (int i = 0; i < aVec.size(); ++i)
|
---|
318 | {
|
---|
319 | aArr [i] = aVec.at (i).raw();
|
---|
320 | if (aArr [i])
|
---|
321 | aArr [i]->AddRef();
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | template <class I, class CI>
|
---|
326 | static void FromSafeIfaceArray (const com::SafeIfaceArray <I> &aArr,
|
---|
327 | QVector <CI> &aVec)
|
---|
328 | {
|
---|
329 | aVec.resize (static_cast<int> (aArr.size()));
|
---|
330 | for (int i = 0; i < aVec.size(); ++i)
|
---|
331 | aVec [i].attach (aArr [i]);
|
---|
332 | }
|
---|
333 |
|
---|
334 | protected:
|
---|
335 |
|
---|
336 | /* no arbitrary instance creations */
|
---|
337 | COMBase() : mRC (S_OK) {};
|
---|
338 |
|
---|
339 | #if defined (VBOX_WITH_XPCOM)
|
---|
340 | static XPCOMEventQSocketListener *sSocketListener;
|
---|
341 | #endif
|
---|
342 |
|
---|
343 | /** Adapter to pass QString as input BSTR params */
|
---|
344 | class BSTRIn
|
---|
345 | {
|
---|
346 | public:
|
---|
347 |
|
---|
348 | BSTRIn (const QString &s) : bstr (SysAllocString ((const OLECHAR *)
|
---|
349 | (s.isNull() ? 0 : s.utf16()))) {}
|
---|
350 |
|
---|
351 | ~BSTRIn()
|
---|
352 | {
|
---|
353 | if (bstr)
|
---|
354 | SysFreeString (bstr);
|
---|
355 | }
|
---|
356 |
|
---|
357 | operator BSTR() const { return bstr; }
|
---|
358 |
|
---|
359 | private:
|
---|
360 |
|
---|
361 | BSTR bstr;
|
---|
362 | };
|
---|
363 |
|
---|
364 | /** Adapter to pass QString as output BSTR params */
|
---|
365 | class BSTROut
|
---|
366 | {
|
---|
367 | public:
|
---|
368 |
|
---|
369 | BSTROut (QString &s) : str (s), bstr (0) {}
|
---|
370 |
|
---|
371 | ~BSTROut()
|
---|
372 | {
|
---|
373 | if (bstr) {
|
---|
374 | str = QString::fromUtf16 (bstr);
|
---|
375 | SysFreeString (bstr);
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | operator BSTR *() { return &bstr; }
|
---|
380 |
|
---|
381 | private:
|
---|
382 |
|
---|
383 | QString &str;
|
---|
384 | BSTR bstr;
|
---|
385 | };
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Adapter to pass K* enums as output COM enum params (*_T).
|
---|
389 | *
|
---|
390 | * @param QE K* enum.
|
---|
391 | * @param CE COM enum.
|
---|
392 | */
|
---|
393 | template <typename QE, typename CE>
|
---|
394 | class ENUMOut
|
---|
395 | {
|
---|
396 | public:
|
---|
397 |
|
---|
398 | ENUMOut (QE &e) : qe (e), ce ((CE) 0) {}
|
---|
399 | ~ENUMOut() { qe = (QE) ce; }
|
---|
400 | operator CE *() { return &ce; }
|
---|
401 |
|
---|
402 | private:
|
---|
403 |
|
---|
404 | QE &qe;
|
---|
405 | CE ce;
|
---|
406 | };
|
---|
407 |
|
---|
408 | #if !defined (VBOX_WITH_XPCOM)
|
---|
409 |
|
---|
410 | /** Adapter to pass QUuid as input GUID params */
|
---|
411 | static GUID GUIDIn (const QUuid &uuid) { return uuid; }
|
---|
412 |
|
---|
413 | /** Adapter to pass QUuid as output GUID params */
|
---|
414 | class GUIDOut
|
---|
415 | {
|
---|
416 | public:
|
---|
417 |
|
---|
418 | GUIDOut (QUuid &id) : uuid (id)
|
---|
419 | {
|
---|
420 | ::memset (&guid, 0, sizeof (GUID));
|
---|
421 | }
|
---|
422 |
|
---|
423 | ~GUIDOut()
|
---|
424 | {
|
---|
425 | uuid = QUuid (
|
---|
426 | guid.Data1, guid.Data2, guid.Data3,
|
---|
427 | guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
|
---|
428 | guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
|
---|
429 | }
|
---|
430 |
|
---|
431 | operator GUID *() { return &guid; }
|
---|
432 |
|
---|
433 | private:
|
---|
434 |
|
---|
435 | QUuid &uuid;
|
---|
436 | GUID guid;
|
---|
437 | };
|
---|
438 |
|
---|
439 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
440 |
|
---|
441 | /** Adapter to pass QUuid as input GUID params */
|
---|
442 | static const nsID &GUIDIn (const QUuid &uuid)
|
---|
443 | {
|
---|
444 | return *(const nsID *) &uuid;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /** Adapter to pass QUuid as output GUID params */
|
---|
448 | class GUIDOut
|
---|
449 | {
|
---|
450 | public:
|
---|
451 |
|
---|
452 | GUIDOut (QUuid &id) : uuid (id), nsid (0) {}
|
---|
453 |
|
---|
454 | ~GUIDOut()
|
---|
455 | {
|
---|
456 | if (nsid)
|
---|
457 | {
|
---|
458 | uuid = QUuid (
|
---|
459 | nsid->m0, nsid->m1, nsid->m2,
|
---|
460 | nsid->m3[0], nsid->m3[1], nsid->m3[2], nsid->m3[3],
|
---|
461 | nsid->m3[4], nsid->m3[5], nsid->m3[6], nsid->m3[7]);
|
---|
462 | nsMemory::Free (nsid);
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | operator nsID **() { return &nsid; }
|
---|
467 |
|
---|
468 | private:
|
---|
469 |
|
---|
470 | QUuid &uuid;
|
---|
471 | nsID *nsid;
|
---|
472 | };
|
---|
473 |
|
---|
474 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
475 |
|
---|
476 | static void addref (IUnknown *aIface) { if (aIface) aIface->AddRef(); }
|
---|
477 | static void release (IUnknown *aIface) { if (aIface) aIface->Release(); }
|
---|
478 |
|
---|
479 | protected:
|
---|
480 |
|
---|
481 | mutable HRESULT mRC;
|
---|
482 |
|
---|
483 | friend class COMErrorInfo;
|
---|
484 | };
|
---|
485 |
|
---|
486 | /////////////////////////////////////////////////////////////////////////////
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Alternative base class for the CInterface template that adds the errorInfo()
|
---|
490 | * method for providing extended error info about unsuccessful invocation of the
|
---|
491 | * last called interface method.
|
---|
492 | */
|
---|
493 | class COMBaseWithEI : public COMBase
|
---|
494 | {
|
---|
495 | public:
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Returns error info set by the last unsuccessfully invoked interface
|
---|
499 | * method. Returned error info is useful only if CInterface::lastRC()
|
---|
500 | * represents a failure or a warning (i.e. CInterface::isReallyOk() is
|
---|
501 | * false).
|
---|
502 | */
|
---|
503 | const COMErrorInfo &errorInfo() const { return mErrInfo; }
|
---|
504 |
|
---|
505 | protected:
|
---|
506 |
|
---|
507 | /* no arbitrary instance creation */
|
---|
508 | COMBaseWithEI() : COMBase () {};
|
---|
509 |
|
---|
510 | void setErrorInfo (const COMErrorInfo &aErrInfo) { mErrInfo = aErrInfo; }
|
---|
511 |
|
---|
512 | void fetchErrorInfo (IUnknown *aCallee, const GUID *aCalleeIID) const
|
---|
513 | {
|
---|
514 | mErrInfo.fetchFromCurrentThread (aCallee, aCalleeIID);
|
---|
515 | }
|
---|
516 |
|
---|
517 | mutable COMErrorInfo mErrInfo;
|
---|
518 | };
|
---|
519 |
|
---|
520 | /////////////////////////////////////////////////////////////////////////////
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * Simple class that encapsulates the result code and COMErrorInfo.
|
---|
524 | */
|
---|
525 | class COMResult
|
---|
526 | {
|
---|
527 | public:
|
---|
528 |
|
---|
529 | COMResult() : mRC (S_OK) {}
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Queries the current result code from the given component.
|
---|
533 | */
|
---|
534 | explicit COMResult (const COMBase &aComponent)
|
---|
535 | : mRC (aComponent.lastRC()) {}
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * Queries the current result code and error info from the given component.
|
---|
539 | */
|
---|
540 | COMResult (const COMBaseWithEI &aComponent)
|
---|
541 | : mRC (aComponent.lastRC())
|
---|
542 | , mErrInfo (aComponent.errorInfo()) {}
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * Queries the current result code from the given component.
|
---|
546 | */
|
---|
547 | COMResult &operator= (const COMBase &aComponent)
|
---|
548 | {
|
---|
549 | mRC = aComponent.lastRC();
|
---|
550 | return *this;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Queries the current result code and error info from the given component.
|
---|
555 | */
|
---|
556 | COMResult &operator= (const COMBaseWithEI &aComponent)
|
---|
557 | {
|
---|
558 | mRC = aComponent.lastRC();
|
---|
559 | mErrInfo = aComponent.errorInfo();
|
---|
560 | return *this;
|
---|
561 | }
|
---|
562 |
|
---|
563 | bool isNull() const { return mErrInfo.isNull(); }
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Returns @c true if the result code represents success (with or without
|
---|
567 | * warnings).
|
---|
568 | */
|
---|
569 | bool isOk() const { return SUCCEEDED (mRC); }
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * Returns @c true if the result code represents success with one or more
|
---|
573 | * warnings.
|
---|
574 | */
|
---|
575 | bool isWarning() const { return SUCCEEDED_WARNING (mRC); }
|
---|
576 |
|
---|
577 | /**
|
---|
578 | * Returns @c true if the result code represents success with no warnings.
|
---|
579 | */
|
---|
580 | bool isReallyOk() const { return mRC == S_OK; }
|
---|
581 |
|
---|
582 | COMErrorInfo errorInfo() const { return mErrInfo; }
|
---|
583 | HRESULT rc() const { return mRC; }
|
---|
584 |
|
---|
585 | private:
|
---|
586 |
|
---|
587 | HRESULT mRC;
|
---|
588 | COMErrorInfo mErrInfo;
|
---|
589 | };
|
---|
590 |
|
---|
591 | /////////////////////////////////////////////////////////////////////////////
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Wrapper template class for all interfaces.
|
---|
595 | *
|
---|
596 | * All interface methods named as they are in the original, i.e. starting
|
---|
597 | * with the capital letter. All utility non-interface methods are named
|
---|
598 | * starting with the small letter. Utility methods should be not normally
|
---|
599 | * called by the end-user client application.
|
---|
600 | *
|
---|
601 | * @param I Interface class (i.e. IUnknown/nsISupports derivant).
|
---|
602 | * @param B Base class, either COMBase (by default) or COMBaseWithEI.
|
---|
603 | */
|
---|
604 | template <class I, class B = COMBase>
|
---|
605 | class CInterface : public B
|
---|
606 | {
|
---|
607 | public:
|
---|
608 |
|
---|
609 | typedef B Base;
|
---|
610 | typedef I Iface;
|
---|
611 |
|
---|
612 | // constructors & destructor
|
---|
613 |
|
---|
614 | CInterface() : mIface (NULL) {}
|
---|
615 |
|
---|
616 | CInterface (const CInterface &that) : B (that), mIface (that.mIface)
|
---|
617 | {
|
---|
618 | addref (mIface);
|
---|
619 | }
|
---|
620 |
|
---|
621 | CInterface (I *aIface) : mIface (aIface) { addref (mIface); }
|
---|
622 |
|
---|
623 | virtual ~CInterface() { release (mIface); }
|
---|
624 |
|
---|
625 | // utility methods
|
---|
626 |
|
---|
627 | void createInstance (const CLSID &aClsId)
|
---|
628 | {
|
---|
629 | AssertMsg (!mIface, ("Instance is already non-NULL\n"));
|
---|
630 | if (!mIface)
|
---|
631 | {
|
---|
632 | #if !defined (VBOX_WITH_XPCOM)
|
---|
633 |
|
---|
634 | B::mRC = CoCreateInstance (aClsId, NULL, CLSCTX_ALL,
|
---|
635 | _ATL_IIDOF (I), (void **) &mIface);
|
---|
636 |
|
---|
637 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
638 |
|
---|
639 | nsCOMPtr <nsIComponentManager> manager;
|
---|
640 | B::mRC = NS_GetComponentManager (getter_AddRefs (manager));
|
---|
641 | if (SUCCEEDED (B::mRC))
|
---|
642 | B::mRC = manager->CreateInstance (aClsId, nsnull, NS_GET_IID (I),
|
---|
643 | (void **) &mIface);
|
---|
644 |
|
---|
645 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
646 |
|
---|
647 | /* fetch error info, but don't assert if it's missing -- many other
|
---|
648 | * reasons can lead to an error (w/o providing error info), not only
|
---|
649 | * the instance initialization code (that should always provide it) */
|
---|
650 | B::fetchErrorInfo (NULL, NULL);
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Attaches to the given foreign interface pointer by querying the own
|
---|
656 | * interface on it. The operation may fail.
|
---|
657 | */
|
---|
658 | template <class OI>
|
---|
659 | void attach (OI *aIface)
|
---|
660 | {
|
---|
661 | /* be aware of self assignment */
|
---|
662 | addref (aIface);
|
---|
663 | release (mIface);
|
---|
664 | if (aIface)
|
---|
665 | {
|
---|
666 | mIface = NULL;
|
---|
667 | #if !defined (VBOX_WITH_XPCOM)
|
---|
668 | B::mRC = aIface->QueryInterface (_ATL_IIDOF (I), (void **) &mIface);
|
---|
669 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
670 | B::mRC = aIface->QueryInterface (NS_GET_IID (I), (void **) &mIface);
|
---|
671 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
672 | }
|
---|
673 | else
|
---|
674 | {
|
---|
675 | mIface = NULL;
|
---|
676 | B::mRC = S_OK;
|
---|
677 | }
|
---|
678 | };
|
---|
679 |
|
---|
680 | /** Specialization of attach() for our own interface I. Never fails. */
|
---|
681 | void attach (I *aIface)
|
---|
682 | {
|
---|
683 | /* be aware of self assignment */
|
---|
684 | addref (aIface);
|
---|
685 | release (mIface);
|
---|
686 | mIface = aIface;
|
---|
687 | B::mRC = S_OK;
|
---|
688 | };
|
---|
689 |
|
---|
690 | /** Detaches from the underlying interface pointer. */
|
---|
691 | void detach() { release (mIface); mIface = NULL; }
|
---|
692 |
|
---|
693 | /** Returns @c true if not attached to any interface pointer. */
|
---|
694 | bool isNull() const { return mIface == NULL; }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Returns @c true if the result code represents success (with or without
|
---|
698 | * warnings).
|
---|
699 | */
|
---|
700 | bool isOk() const { return !isNull() && SUCCEEDED (B::mRC); }
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Returns @c true if the result code represents success with one or more
|
---|
704 | * warnings.
|
---|
705 | */
|
---|
706 | bool isWarning() const { return !isNull() && SUCCEEDED_WARNING (B::mRC); }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Returns @c true if the result code represents success with no warnings.
|
---|
710 | */
|
---|
711 | bool isReallyOk() const { return !isNull() && B::mRC == S_OK; }
|
---|
712 |
|
---|
713 | // utility operators
|
---|
714 |
|
---|
715 | CInterface &operator= (const CInterface &that)
|
---|
716 | {
|
---|
717 | attach (that.mIface);
|
---|
718 | B::operator= (that);
|
---|
719 | return *this;
|
---|
720 | }
|
---|
721 |
|
---|
722 | CInterface &operator= (I *aIface)
|
---|
723 | {
|
---|
724 | attach (aIface);
|
---|
725 | return *this;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Returns the raw interface pointer. Not intended to be used for anything
|
---|
730 | * else but in generated wrappers and for debugging. You've been warned.
|
---|
731 | */
|
---|
732 | I *raw() const { return mIface; }
|
---|
733 |
|
---|
734 | bool operator== (const CInterface &that) const { return mIface == that.mIface; }
|
---|
735 | bool operator!= (const CInterface &that) const { return mIface != that.mIface; }
|
---|
736 |
|
---|
737 | protected:
|
---|
738 |
|
---|
739 | mutable I *mIface;
|
---|
740 | };
|
---|
741 |
|
---|
742 | /////////////////////////////////////////////////////////////////////////////
|
---|
743 |
|
---|
744 | class CUnknown : public CInterface <IUnknown, COMBaseWithEI>
|
---|
745 | {
|
---|
746 | public:
|
---|
747 |
|
---|
748 | typedef CInterface <IUnknown, COMBaseWithEI> Base;
|
---|
749 |
|
---|
750 | CUnknown() {}
|
---|
751 |
|
---|
752 | /** Creates an instance given another CInterface-based instance. */
|
---|
753 | template <class OI, class OB>
|
---|
754 | explicit CUnknown (const CInterface <OI, OB> &that)
|
---|
755 | {
|
---|
756 | attach (that.mIface);
|
---|
757 | if (SUCCEEDED (mRC))
|
---|
758 | {
|
---|
759 | /* preserve old error info if any */
|
---|
760 | mRC = that.lastRC();
|
---|
761 | setErrorInfo (that.errorInfo());
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | /** Constructor specialization for IUnknown. */
|
---|
766 | CUnknown (const CUnknown &that) : Base (that) {}
|
---|
767 |
|
---|
768 | /** Creates an instance given a foreign interface pointer. */
|
---|
769 | template <class OI>
|
---|
770 | explicit CUnknown (OI *aIface)
|
---|
771 | {
|
---|
772 | attach (aIface);
|
---|
773 | }
|
---|
774 |
|
---|
775 | /** Constructor specialization for IUnknown. */
|
---|
776 | explicit CUnknown (IUnknown *aIface) : Base (aIface) {}
|
---|
777 |
|
---|
778 | /** Assigns from another CInterface-based instance. */
|
---|
779 | template <class OI, class OB>
|
---|
780 | CUnknown &operator= (const CInterface <OI, OB> &that)
|
---|
781 | {
|
---|
782 | attach (that.mIface);
|
---|
783 | if (SUCCEEDED (mRC))
|
---|
784 | {
|
---|
785 | /* preserve old error info if any */
|
---|
786 | mRC = that.lastRC();
|
---|
787 | setErrorInfo (that.errorInfo());
|
---|
788 | }
|
---|
789 | return *this;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /** Assignment specialization for CUnknown. */
|
---|
793 | CUnknown &operator= (const CUnknown &that)
|
---|
794 | {
|
---|
795 | Base::operator= (that);
|
---|
796 | return *this;
|
---|
797 | }
|
---|
798 |
|
---|
799 | /** Assigns from a foreign interface pointer. */
|
---|
800 | template <class OI>
|
---|
801 | CUnknown &operator= (OI *aIface)
|
---|
802 | {
|
---|
803 | attach (aIface);
|
---|
804 | return *this;
|
---|
805 | }
|
---|
806 |
|
---|
807 | /** Assignment specialization for IUnknown. */
|
---|
808 | CUnknown &operator= (IUnknown *aIface)
|
---|
809 | {
|
---|
810 | Base::operator= (aIface);
|
---|
811 | return *this;
|
---|
812 | }
|
---|
813 |
|
---|
814 | /* @internal Used in generated wrappers. Never use directly. */
|
---|
815 | IUnknown *&rawRef() { return mIface; };
|
---|
816 | };
|
---|
817 |
|
---|
818 | /////////////////////////////////////////////////////////////////////////////
|
---|
819 |
|
---|
820 | /* include the generated header containing concrete wrapper definitions */
|
---|
821 | #include "COMWrappers.h"
|
---|
822 |
|
---|
823 | /** @} */
|
---|
824 |
|
---|
825 | #endif // __COMDefs_h__
|
---|