VirtualBox

source: vbox/trunk/include/VBox/com/defs.h@ 35722

Last change on this file since 35722 was 35722, checked in by vboxsync, 14 years ago

Main: reworked listener objects creation, fixes Win problems with events, few cleanups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.9 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Common definitions
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_com_defs_h
28#define ___VBox_com_defs_h
29
30/* Make sure all the stdint.h macros are included - must come first! */
31#ifndef __STDC_LIMIT_MACROS
32# define __STDC_LIMIT_MACROS
33#endif
34#ifndef __STDC_CONSTANT_MACROS
35# define __STDC_CONSTANT_MACROS
36#endif
37
38#if defined (RT_OS_OS2)
39
40# if defined(RT_MAX) && RT_MAX != 22
41# undef RT_MAX
42# define REDEFINE_RT_MAX
43# endif
44# undef RT_MAX
45
46/* Make sure OS/2 Toolkit headers are pulled in to have BOOL/ULONG/etc. typedefs
47 * already defined in order to be able to redefine them using #define. */
48# define INCL_BASE
49# define INCL_PM
50# include <os2.h>
51
52/* OS/2 Toolkit defines TRUE and FALSE */
53# undef FALSE
54# undef TRUE
55
56/* */
57# undef RT_MAX
58# ifdef REDEFINE_RT_MAX
59# define RT_MAX(Value1, Value2) ( (Value1) >= (Value2) ? (Value1) : (Value2) )
60# endif
61
62#endif /* defined (RT_OS_OS2) */
63
64/* Include iprt/types.h (which also includes iprt/types.h) now to make sure iprt
65 * gets to stdint.h first, otherwise a system/xpcom header might beat us and
66 * we'll be without the macros that are optional in C++. */
67#include <iprt/types.h>
68
69#if !defined (VBOX_WITH_XPCOM)
70
71#if defined (RT_OS_WINDOWS)
72
73// Windows COM
74/////////////////////////////////////////////////////////////////////////////
75
76#include <objbase.h>
77#ifndef VBOX_COM_NO_ATL
78# define _ATL_FREE_THREADED
79
80# include <atlbase.h>
81#include <atlcom.h>
82#endif
83
84#define NS_DECL_ISUPPORTS
85#define NS_IMPL_ISUPPORTS1_CI(a, b)
86
87/* these are XPCOM only, one for every interface implemented */
88#define NS_DECL_ISUPPORTS
89
90/** Returns @c true if @a rc represents a warning result code */
91#define SUCCEEDED_WARNING(rc) (SUCCEEDED (rc) && (rc) != S_OK)
92
93/** Tests is a COM result code indicates that the process implementing the
94 * interface is dead.
95 *
96 * COM status codes:
97 * 0x800706ba - RPC_S_SERVER_UNAVAILABLE. Killed before call was made.
98 * 0x800706be - RPC_S_CALL_FAILED. Killed after call was made.
99 * 0x800706bf - RPC_S_CALL_FAILED_DNE. Not observed, but should be matter of timing.
100 */
101#define FAILED_DEAD_INTERFACE(rc) \
102 ( (rc) == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE) \
103 || (rc) == HRESULT_FROM_WIN32(RPC_S_CALL_FAILED) \
104 || (rc) == HRESULT_FROM_WIN32(RPC_S_CALL_FAILED_DNE) \
105 )
106
107/** Immutable BSTR string */
108typedef const OLECHAR *CBSTR;
109
110/** Input BSTR argument of interface method declaration. */
111#define IN_BSTR BSTR
112
113/** Input GUID argument of interface method declaration. */
114#define IN_GUID GUID
115/** Output GUID argument of interface method declaration. */
116#define OUT_GUID GUID*
117
118/** Makes the name of the getter interface function (n must be capitalized). */
119#define COMGETTER(n) get_##n
120/** Makes the name of the setter interface function (n must be capitalized). */
121#define COMSETTER(n) put_##n
122
123/**
124 * Declares an input safearray parameter in the COM method implementation. Also
125 * used to declare the COM attribute setter parameter. Corresponds to either of
126 * the following XIDL definitions:
127 * <pre>
128 * <param name="arg" ... dir="in" safearray="yes"/>
129 * ...
130 * <attribute name="arg" ... safearray="yes"/>
131 * </pre>
132 *
133 * The method implementation should use the com::SafeArray helper class to work
134 * with parameters declared using this define.
135 *
136 * @param aType Array element type.
137 * @param aArg Parameter/attribute name.
138 */
139#define ComSafeArrayIn(aType, aArg) SAFEARRAY **aArg
140
141/**
142 * Expands to @true if the given input safearray parameter is a "null pointer"
143 * which makes it impossible to use it for reading safearray data.
144 */
145#define ComSafeArrayInIsNull(aArg) ((aArg) == NULL || *(aArg) == NULL)
146
147/**
148 * Wraps the given parameter name to generate an expression that is suitable for
149 * passing the parameter to functions that take input safearray parameters
150 * declared using the ComSafeArrayIn marco.
151 *
152 * @param aArg Parameter name to wrap. The given parameter must be declared
153 * within the calling function using the ComSafeArrayIn macro.
154 */
155#define ComSafeArrayInArg(aArg) aArg
156
157/**
158 * Declares an output safearray parameter in the COM method implementation. Also
159 * used to declare the COM attribute getter parameter. Corresponds to either of
160 * the following XIDL definitions:
161 * <pre>
162 * <param name="arg" ... dir="out" safearray="yes"/>
163 * <param name="arg" ... dir="return" safearray="yes"/>
164 * ...
165 * <attribute name="arg" ... safearray="yes"/>
166 * </pre>
167 *
168 * The method implementation should use the com::SafeArray helper class to work
169 * with parameters declared using this define.
170 *
171 * @param aType Array element type.
172 * @param aArg Parameter/attribute name.
173 */
174#define ComSafeArrayOut(aType, aArg) SAFEARRAY **aArg
175
176/**
177 * Expands to @true if the given output safearray parameter is a "null pointer"
178 * which makes it impossible to use it for returning a safearray.
179 */
180#define ComSafeArrayOutIsNull(aArg) ((aArg) == NULL)
181
182/**
183 * Wraps the given parameter name to generate an expression that is suitable for
184 * passing the parameter to functions that take output safearray parameters
185 * declared using the ComSafeArrayOut marco.
186 *
187 * @param aArg Parameter name to wrap. The given parameter must be declared
188 * within the calling function using the ComSafeArrayOut macro.
189 */
190#define ComSafeArrayOutArg(aArg) aArg
191
192/**
193 * Version of ComSafeArrayIn for GUID.
194 * @param aArg Parameter name to wrap.
195 */
196#define ComSafeGUIDArrayIn(aArg) SAFEARRAY **aArg
197
198/**
199 * Version of ComSafeArrayInIsNull for GUID.
200 * @param aArg Parameter name to wrap.
201 */
202#define ComSafeGUIDArrayInIsNull(aArg) ComSafeArrayInIsNull (aArg)
203
204/**
205 * Version of ComSafeArrayInArg for GUID.
206 * @param aArg Parameter name to wrap.
207 */
208#define ComSafeGUIDArrayInArg(aArg) ComSafeArrayInArg (aArg)
209
210/**
211 * Version of ComSafeArrayOut for GUID.
212 * @param aArg Parameter name to wrap.
213 */
214#define ComSafeGUIDArrayOut(aArg) SAFEARRAY **aArg
215
216/**
217 * Version of ComSafeArrayOutIsNull for GUID.
218 * @param aArg Parameter name to wrap.
219 */
220#define ComSafeGUIDArrayOutIsNull(aArg) ComSafeArrayOutIsNull (aArg)
221
222/**
223 * Version of ComSafeArrayOutArg for GUID.
224 * @param aArg Parameter name to wrap.
225 */
226#define ComSafeGUIDArrayOutArg(aArg) ComSafeArrayOutArg (aArg)
227
228/**
229 * Returns the const reference to the IID (i.e., |const GUID &|) of the given
230 * interface.
231 *
232 * @param i interface class
233 */
234#define COM_IIDOF(I) _ATL_IIDOF(I)
235
236/**
237 * For using interfaces before including the interface definitions. This will
238 * deal with XPCOM using 'class' and COM using 'struct' when defining
239 * interfaces.
240 *
241 * @param I interface name.
242 */
243#define COM_STRUCT_OR_CLASS(I) struct I
244
245#else /* defined (RT_OS_WINDOWS) */
246
247#error "VBOX_WITH_XPCOM must be defined on a platform other than Windows!"
248
249#endif /* defined (RT_OS_WINDOWS) */
250
251#else /* !defined (VBOX_WITH_XPCOM) */
252
253// XPCOM
254/////////////////////////////////////////////////////////////////////////////
255
256#if defined (RT_OS_DARWIN) || (defined (QT_VERSION) && (QT_VERSION >= 0x040000))
257 /* CFBase.h defines these &
258 * qglobal.h from Qt4 defines these */
259# undef FALSE
260# undef TRUE
261#endif /* RT_OS_DARWIN || QT_VERSION */
262
263#include <nsID.h>
264
265#define ATL_NO_VTABLE
266#define DECLARE_CLASSFACTORY(a)
267#define DECLARE_CLASSFACTORY_SINGLETON(a)
268#define DECLARE_REGISTRY_RESOURCEID(a)
269#define DECLARE_NOT_AGGREGATABLE(a)
270#define DECLARE_PROTECT_FINAL_CONSTRUCT()
271#define BEGIN_COM_MAP(a)
272#define COM_INTERFACE_ENTRY(a)
273#define COM_INTERFACE_ENTRY2(a,b)
274#define END_COM_MAP() NS_DECL_ISUPPORTS
275
276#define HRESULT nsresult
277#define SUCCEEDED NS_SUCCEEDED
278#define FAILED NS_FAILED
279
280#define SUCCEEDED_WARNING(rc) (NS_SUCCEEDED (rc) && (rc) != NS_OK)
281
282#define FAILED_DEAD_INTERFACE(rc) ( (rc) == NS_ERROR_ABORT \
283 || (rc) == NS_ERROR_CALL_FAILED \
284 )
285
286#define IUnknown nsISupports
287
288#define BOOL PRBool
289#define BYTE PRUint8
290#define SHORT PRInt16
291#define USHORT PRUint16
292#define LONG PRInt32
293#define ULONG PRUint32
294#define LONG64 PRInt64
295#define ULONG64 PRUint64
296/* XPCOM has only 64bit floats */
297#define FLOAT PRFloat64
298#define DOUBLE PRFloat64
299
300#define FALSE PR_FALSE
301#define TRUE PR_TRUE
302
303#define OLECHAR wchar_t
304
305/* note: typedef to semantically match BSTR on Win32 */
306typedef PRUnichar *BSTR;
307typedef const PRUnichar *CBSTR;
308typedef BSTR *LPBSTR;
309
310/** Input BSTR argument the interface method declaration. */
311#define IN_BSTR CBSTR
312
313/**
314 * Type to define a raw GUID variable (for members use the com::Guid class
315 * instead).
316 */
317#define GUID nsID
318/** Input GUID argument the interface method declaration. */
319#define IN_GUID const nsID &
320/** Output GUID argument the interface method declaration. */
321#define OUT_GUID nsID **
322
323/** Makes the name of the getter interface function (n must be capitalized). */
324#define COMGETTER(n) Get##n
325/** Makes the name of the setter interface function (n must be capitalized). */
326#define COMSETTER(n) Set##n
327
328/* safearray input parameter macros */
329#define ComSafeArrayIn(aType, aArg) PRUint32 aArg##Size, aType *aArg
330#define ComSafeArrayInIsNull(aArg) ((aArg) == NULL)
331#define ComSafeArrayInArg(aArg) aArg##Size, aArg
332
333/* safearray output parameter macros */
334#define ComSafeArrayOut(aType, aArg) PRUint32 *aArg##Size, aType **aArg
335#define ComSafeArrayOutIsNull(aArg) ((aArg) == NULL)
336#define ComSafeArrayOutArg(aArg) aArg##Size, aArg
337
338/* safearray input parameter macros for GUID */
339#define ComSafeGUIDArrayIn(aArg) PRUint32 aArg##Size, const nsID **aArg
340#define ComSafeGUIDArrayInIsNull(aArg) ComSafeArrayInIsNull (aArg)
341#define ComSafeGUIDArrayInArg(aArg) ComSafeArrayInArg (aArg)
342
343/* safearray output parameter macros for GUID */
344#define ComSafeGUIDArrayOut(aArg) PRUint32 *aArg##Size, nsID ***aArg
345#define ComSafeGUIDArrayOutIsNull(aArg) ComSafeArrayOutIsNull (aArg)
346#define ComSafeGUIDArrayOutArg(aArg) ComSafeArrayOutArg (aArg)
347
348/* CLSID and IID for compatibility with Win32 */
349typedef nsCID CLSID;
350typedef nsIID IID;
351
352/* OLE error codes */
353#define S_OK ((nsresult) NS_OK)
354#define E_UNEXPECTED NS_ERROR_UNEXPECTED
355#define E_NOTIMPL NS_ERROR_NOT_IMPLEMENTED
356#define E_OUTOFMEMORY NS_ERROR_OUT_OF_MEMORY
357#define E_INVALIDARG NS_ERROR_INVALID_ARG
358#define E_NOINTERFACE NS_ERROR_NO_INTERFACE
359#define E_POINTER NS_ERROR_NULL_POINTER
360#define E_ABORT NS_ERROR_ABORT
361#define E_FAIL NS_ERROR_FAILURE
362/* Note: a better analog for E_ACCESSDENIED would probably be
363 * NS_ERROR_NOT_AVAILABLE, but we want binary compatibility for now. */
364#define E_ACCESSDENIED ((nsresult) 0x80070005L)
365
366#define STDMETHOD(a) NS_IMETHOD a
367#define STDMETHODIMP NS_IMETHODIMP
368
369#define COM_IIDOF(I) NS_GET_IID(I)
370
371#define COM_STRUCT_OR_CLASS(I) class I
372
373/* A few very simple ATL emulator classes to provide
374 * FinalConstruct()/FinalRelease() functionality on Linux. */
375
376class CComMultiThreadModel
377{
378};
379
380template <class Base> class CComObjectRootEx : public Base
381{
382public:
383 HRESULT FinalConstruct() { return S_OK; }
384 void FinalRelease() {}
385};
386
387template <class Base> class CComObject : public Base
388{
389public:
390 virtual ~CComObject() { this->FinalRelease(); }
391};
392
393/* helper functions */
394extern "C"
395{
396BSTR SysAllocString (const OLECHAR* sz);
397BSTR SysAllocStringByteLen (char *psz, unsigned int len);
398BSTR SysAllocStringLen (const OLECHAR *pch, unsigned int cch);
399void SysFreeString (BSTR bstr);
400int SysReAllocString (BSTR *pbstr, const OLECHAR *psz);
401int SysReAllocStringLen (BSTR *pbstr, const OLECHAR *psz, unsigned int cch);
402unsigned int SysStringByteLen (BSTR bstr);
403unsigned int SysStringLen (BSTR bstr);
404}
405
406/**
407 * 'Constructor' for the component class.
408 * This constructor, as opposed to NS_GENERIC_FACTORY_CONSTRUCTOR,
409 * assumes that the component class is derived from the CComObjectRootEx<>
410 * template, so it calls FinalConstruct() right after object creation
411 * and ensures that FinalRelease() will be called right before destruction.
412 * The result from FinalConstruct() is returned to the caller.
413 */
414#define NS_GENERIC_FACTORY_CONSTRUCTOR_WITH_RC(_InstanceClass) \
415static NS_IMETHODIMP \
416_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
417 void **aResult) \
418{ \
419 nsresult rv; \
420 \
421 *aResult = NULL; \
422 if (NULL != aOuter) { \
423 rv = NS_ERROR_NO_AGGREGATION; \
424 return rv; \
425 } \
426 \
427 CComObject <_InstanceClass> *inst = new CComObject <_InstanceClass>(); \
428 if (NULL == inst) { \
429 rv = NS_ERROR_OUT_OF_MEMORY; \
430 return rv; \
431 } \
432 \
433 NS_ADDREF(inst); /* protect FinalConstruct() */ \
434 rv = inst->FinalConstruct(); \
435 if (NS_SUCCEEDED(rv)) \
436 rv = inst->QueryInterface(aIID, aResult); \
437 NS_RELEASE(inst); \
438 \
439 return rv; \
440}
441
442/**
443 * 'Constructor' that uses an existing getter function that gets a singleton.
444 * The getter function must have the following prototype:
445 * nsresult _GetterProc (_InstanceClass **inst)
446 * This constructor, as opposed to NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR,
447 * lets the getter function return a result code that is passed back to the
448 * caller that tries to instantiate the object.
449 * NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
450 */
451#define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR_WITH_RC(_InstanceClass, _GetterProc) \
452static NS_IMETHODIMP \
453_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
454 void **aResult) \
455{ \
456 nsresult rv; \
457 \
458 _InstanceClass * inst = NULL; /* initialized to shut up gcc */ \
459 \
460 *aResult = NULL; \
461 if (NULL != aOuter) { \
462 rv = NS_ERROR_NO_AGGREGATION; \
463 return rv; \
464 } \
465 \
466 rv = _GetterProc(&inst); \
467 if (NS_FAILED(rv)) \
468 return rv; \
469 \
470 /* sanity check */ \
471 if (NULL == inst) \
472 return NS_ERROR_OUT_OF_MEMORY; \
473 \
474 /* NS_ADDREF(inst); */ \
475 if (NS_SUCCEEDED(rv)) { \
476 rv = inst->QueryInterface(aIID, aResult); \
477 } \
478 NS_RELEASE(inst); \
479 \
480 return rv; \
481}
482
483#endif /* !defined (VBOX_WITH_XPCOM) */
484
485/**
486 * Declares a wchar_t string literal from the argument.
487 * Necessary to overcome MSC / GCC differences.
488 * @param s expression to stringify
489 */
490#if defined (_MSC_VER)
491# define WSTR_LITERAL(s) L#s
492#elif defined (__GNUC__)
493# define WSTR_LITERAL(s) L""#s
494#else
495# error "Unsupported compiler!"
496#endif
497
498namespace com
499{
500
501// use this macro to implement scriptable interfaces
502#ifdef RT_OS_WINDOWS
503#define VBOX_SCRIPTABLE_IMPL(iface) \
504 public IDispatchImpl<iface, &IID_##iface, &LIBID_VirtualBox, \
505 kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>
506
507#define VBOX_SCRIPTABLE_DISPATCH_IMPL(iface) \
508 STDMETHOD(QueryInterface)(REFIID riid , void **ppObj) \
509 { \
510 if (riid == IID_##iface) \
511 { \
512 *ppObj = (iface*)this; \
513 AddRef(); \
514 return S_OK; \
515 } \
516 if (riid == IID_IUnknown) \
517 { \
518 *ppObj = (IUnknown*)this; \
519 AddRef(); \
520 return S_OK; \
521 } \
522 if (riid == IID_IDispatch) \
523 { \
524 *ppObj = (IDispatch*)this; \
525 AddRef(); \
526 return S_OK; \
527 } \
528 *ppObj = NULL; \
529 return E_NOINTERFACE; \
530 }
531
532
533#define VBOX_DEFAULT_INTERFACE_ENTRIES(iface) \
534 COM_INTERFACE_ENTRY(ISupportErrorInfo) \
535 COM_INTERFACE_ENTRY(iface) \
536 COM_INTERFACE_ENTRY2(IDispatch,iface) \
537 COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.p)
538#else
539#define VBOX_SCRIPTABLE_IMPL(iface) \
540 public iface
541#define VBOX_SCRIPTABLE_DISPATCH_IMPL(iface)
542#define VBOX_DEFAULT_INTERFACE_ENTRIES(iface)
543#endif
544
545
546} /* namespace com */
547
548#endif /* ___VBox_com_defs_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette