VirtualBox

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

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

warnings

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