VirtualBox

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

Last change on this file since 60553 was 60506, checked in by vboxsync, 9 years ago

include/VBox/com/defs.h: more build break fixing for XPCOM

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

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