VirtualBox

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

Last change on this file since 6852 was 6851, checked in by vboxsync, 17 years ago

Ported r27277:27975 (array support) from branches/dmik/s2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Common definitions
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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/*
31 * Include iprt/types.h now to make sure iprt get to stdint.h first,
32 * otherwise a system/xpcom header might beat us and we'll be without
33 * the macros that are optional in C++.
34 */
35#include <iprt/types.h>
36
37#if !defined (VBOX_WITH_XPCOM)
38
39#if defined (RT_OS_WINDOWS)
40
41// Windows COM
42/////////////////////////////////////////////////////////////////////////////
43
44#include <objbase.h>
45#ifndef VBOX_COM_NO_ATL
46#include <atlbase.h>
47#endif
48
49#define NS_DECL_ISUPPORTS
50#define NS_IMPL_ISUPPORTS1_CI(a, b)
51
52/* these are XPCOM only, one for every interface implemented */
53#define NS_DECL_ISUPPORTS
54#define NS_DECL_IVIRTUALBOX
55#define NS_DECL_IMACHINECOLLECTION
56#define NS_DECL_IMACHINE
57
58/* input pointer argument to method */
59#define INPTR
60
61/* makes the name of the getter interface function (n must be capitalized) */
62#define COMGETTER(n) get_##n
63/* makes the name of the setter interface function (n must be capitalized) */
64#define COMSETTER(n) put_##n
65
66/* a type for an input GUID parameter in the interface method declaration */
67#define GUIDPARAM GUID
68/* a type for an output GUID parameter in the interface method declaration */
69#define GUIDPARAMOUT GUID*
70
71/**
72 * Declares an input safearray parameter in the COM method implementation. Also
73 * used to declare the COM attribute setter parameter. Corresponds to either of
74 * the following XIDL definitions:
75 * <pre>
76 * <param name="arg" ... dir="in" safearray="yes"/>
77 * ...
78 * <attribute name="arg" ... safearray="yes"/>
79 * </pre>
80 *
81 * The method implementation should use the com::SafeArray helper class to work
82 * with parameters declared using this define.
83 *
84 * @param aType Array element type.
85 * @param aArg Parameter/attribute name.
86 */
87#define ComSafeArrayIn(aType, aArg) SAFEARRAY **aArg
88
89/**
90 * Expands to @true if the given input safearray parameter is a "null pointer"
91 * which makes it impossible to use it for reading safearray data.
92 */
93#define ComSafeArrayInIsNull(aArg) (aArg == NULL)
94
95/**
96 * Wraps the given parameter name to generate an expression that is suitable for
97 * passing the parameter to functions that take input safearray parameters
98 * declared using the ComSafeArrayIn marco.
99 *
100 * @param aArg Parameter name to wrap. The given parameter must be declared
101 * within the calling function using the ComSafeArrayIn macro.
102 */
103#define ComSafeArrayInArg(aArg) aArg
104
105/**
106 * Declares an output safearray parameter in the COM method implementation. Also
107 * used to declare the COM attribute getter parameter. Corresponds to either of
108 * the following XIDL definitions:
109 * <pre>
110 * <param name="arg" ... dir="out" safearray="yes"/>
111 * <param name="arg" ... dir="return" safearray="yes"/>
112 * ...
113 * <attribute name="arg" ... safearray="yes"/>
114 * </pre>
115 *
116 * The method implementation should use the com::SafeArray helper class to work
117 * with parameters declared using this define.
118 *
119 * @param aType Array element type.
120 * @param aArg Parameter/attribute name.
121 */
122#define ComSafeArrayOut(aType, aArg) SAFEARRAY **aArg
123
124/**
125 * Expands to @true if the given output safearray parameter is a "null pointer"
126 * which makes it impossible to use it for returning a safearray.
127 */
128#define ComSafeArrayOutIsNull(aArg) (aArg == NULL)
129
130/**
131 * Wraps the given parameter name to generate an expression that is suitable for
132 * passing the parameter to functions that take output safearray parameters
133 * declared using the ComSafeArrayOut marco.
134 *
135 * @param aArg Parameter name to wrap. The given parameter must be declared
136 * within the calling function using the ComSafeArrayOut macro.
137 */
138#define ComSafeArrayOutArg(aArg) aArg
139
140/**
141 * Returns the const reference to the IID (i.e., |const GUID &|) of the given
142 * interface.
143 *
144 * @param i interface class
145 */
146#define COM_IIDOF(I) _ATL_IIDOF (I)
147
148#else /* defined (RT_OS_WINDOWS) */
149
150#error "VBOX_WITH_XPCOM must be defined on a platform other than Windows!"
151
152#endif /* defined (RT_OS_WINDOWS) */
153
154#else /* !defined (VBOX_WITH_XPCOM) */
155
156// XPCOM
157/////////////////////////////////////////////////////////////////////////////
158
159#if defined (RT_OS_OS2)
160
161/* Make sure OS/2 Toolkit headers are pulled in to have
162 * BOOL/ULONG/etc. typedefs already defined in order to be able to redefine
163 * them using #define. */
164#define INCL_BASE
165#define INCL_PM
166#include <os2.h>
167
168/* OS/2 Toolkit defines TRUE and FALSE */
169#undef FALSE
170#undef TRUE
171
172#endif /* defined (RT_OS_OS2) */
173
174#if defined (RT_OS_DARWIN)
175 /* CFBase.h defines these*/
176# undef FALSE
177# undef TRUE
178#endif /* RT_OS_DARWIN */
179
180#include <nsID.h>
181
182#define ATL_NO_VTABLE
183#define DECLARE_CLASSFACTORY(a)
184#define DECLARE_CLASSFACTORY_SINGLETON(a)
185#define DECLARE_REGISTRY_RESOURCEID(a)
186#define DECLARE_NOT_AGGREGATABLE(a)
187#define DECLARE_PROTECT_FINAL_CONSTRUCT(a)
188#define BEGIN_COM_MAP(a)
189#define COM_INTERFACE_ENTRY(a)
190#define COM_INTERFACE_ENTRY2(a,b)
191#define END_COM_MAP(a)
192
193#define HRESULT nsresult
194#define SUCCEEDED NS_SUCCEEDED
195#define FAILED NS_FAILED
196#define NS_NULL nsnull
197
198#define IUnknown nsISupports
199
200#define BOOL PRBool
201#define BYTE PRUint8
202#define SHORT PRInt16
203#define USHORT PRUint16
204#define LONG PRInt32
205#define ULONG PRUint32
206#define LONG64 PRInt64
207#define ULONG64 PRUint64
208
209#define BSTR PRUnichar *
210#define LPBSTR BSTR *
211#define OLECHAR wchar_t
212
213#define FALSE PR_FALSE
214#define TRUE PR_TRUE
215
216/* makes the name of the getter interface function (n must be capitalized) */
217#define COMGETTER(n) Get##n
218/* makes the name of the setter interface function (n must be capitalized) */
219#define COMSETTER(n) Set##n
220
221/* a type to define a raw GUID variable (better to use the Guid class) */
222#define GUID nsID
223/* a type for an input GUID parameter in the interface method declaration */
224#define GUIDPARAM nsID &
225/* a type for an output GUID parameter in the interface method declaration */
226#define GUIDPARAMOUT nsID **
227
228/* safearray input parameter macros */
229#define ComSafeArrayIn(aType, aArg) PRUint32 aArg##Size, aType *aArg
230#define ComSafeArrayInIsNull(aArg) (aArg == NULL)
231#define ComSafeArrayInArg(aArg) aArg##Size, aArg
232
233/* safearray output parameter macros */
234#define ComSafeArrayOut(aType, aArg) PRUint32 *aArg##Size, aType **aArg
235#define ComSafeArrayOutIsNull(aArg) (aArg == NULL)
236#define ComSafeArrayOutArg(aArg) aArg##Size, aArg
237
238/* CLSID and IID for compatibility with Win32 */
239typedef nsCID CLSID;
240typedef nsIID IID;
241
242/* OLE error codes */
243#define S_OK NS_OK
244#define E_UNEXPECTED NS_ERROR_UNEXPECTED
245#define E_NOTIMPL NS_ERROR_NOT_IMPLEMENTED
246#define E_OUTOFMEMORY NS_ERROR_OUT_OF_MEMORY
247#define E_INVALIDARG NS_ERROR_INVALID_ARG
248#define E_NOINTERFACE NS_ERROR_NO_INTERFACE
249#define E_POINTER NS_ERROR_NULL_POINTER
250#define E_ABORT NS_ERROR_ABORT
251#define E_FAIL NS_ERROR_FAILURE
252/* Note: a better analog for E_ACCESSDENIED would probably be
253 * NS_ERROR_NOT_AVAILABLE, but we want binary compatibility for now. */
254#define E_ACCESSDENIED ((nsresult) 0x80070005L)
255
256#define STDMETHOD(a) NS_IMETHOD a
257#define STDMETHODIMP NS_IMETHODIMP
258
259#define COM_IIDOF(I) NS_GET_IID (I)
260
261/* two very simple ATL emulator classes to provide
262 * FinalConstruct()/FinalRelease() functionality on Linux */
263
264class CComObjectRootEx
265{
266public:
267 HRESULT FinalConstruct() { return S_OK; }
268 void FinalRelease() {}
269};
270
271template <class Base> class CComObject : public Base
272{
273public:
274 virtual ~CComObject() { this->FinalRelease(); }
275};
276
277/* input pointer argument to method */
278#define INPTR const
279
280/* helper functions */
281extern "C"
282{
283BSTR SysAllocString (const OLECHAR* sz);
284BSTR SysAllocStringByteLen (char *psz, unsigned int len);
285BSTR SysAllocStringLen (const OLECHAR *pch, unsigned int cch);
286void SysFreeString (BSTR bstr);
287int SysReAllocString (BSTR *pbstr, const OLECHAR *psz);
288int SysReAllocStringLen (BSTR *pbstr, const OLECHAR *psz, unsigned int cch);
289unsigned int SysStringByteLen (BSTR bstr);
290unsigned int SysStringLen (BSTR bstr);
291}
292
293/**
294 * 'Constructor' for the component class.
295 * This constructor, as opposed to NS_GENERIC_FACTORY_CONSTRUCTOR,
296 * assumes that the component class is derived from the CComObjectRootEx<>
297 * template, so it calls FinalConstruct() right after object creation
298 * and ensures that FinalRelease() will be called right before destruction.
299 * The result from FinalConstruct() is returned to the caller.
300 */
301#define NS_GENERIC_FACTORY_CONSTRUCTOR_WITH_RC(_InstanceClass) \
302static NS_IMETHODIMP \
303_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
304 void **aResult) \
305{ \
306 nsresult rv; \
307 \
308 *aResult = NULL; \
309 if (NULL != aOuter) { \
310 rv = NS_ERROR_NO_AGGREGATION; \
311 return rv; \
312 } \
313 \
314 CComObject <_InstanceClass> *inst = new CComObject <_InstanceClass>(); \
315 if (NULL == inst) { \
316 rv = NS_ERROR_OUT_OF_MEMORY; \
317 return rv; \
318 } \
319 \
320 NS_ADDREF(inst); /* protect FinalConstruct() */ \
321 rv = inst->FinalConstruct(); \
322 if (NS_SUCCEEDED(rv)) \
323 rv = inst->QueryInterface(aIID, aResult); \
324 NS_RELEASE(inst); \
325 \
326 return rv; \
327}
328
329/**
330 * 'Constructor' that uses an existing getter function that gets a singleton.
331 * The getter function must have the following prototype:
332 * nsresult _GetterProc (_InstanceClass **inst)
333 * This constructor, as opposed to NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR,
334 * lets the getter function return a result code that is passed back to the
335 * caller that tries to instantiate the object.
336 * NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
337 */
338#define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR_WITH_RC(_InstanceClass, _GetterProc) \
339static NS_IMETHODIMP \
340_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
341 void **aResult) \
342{ \
343 nsresult rv; \
344 \
345 _InstanceClass * inst; \
346 \
347 *aResult = NULL; \
348 if (NULL != aOuter) { \
349 rv = NS_ERROR_NO_AGGREGATION; \
350 return rv; \
351 } \
352 \
353 rv = _GetterProc(&inst); \
354 if (NS_FAILED(rv)) \
355 return rv; \
356 \
357 /* sanity check */ \
358 if (NULL == inst) \
359 return NS_ERROR_OUT_OF_MEMORY; \
360 \
361 /* NS_ADDREF(inst); */ \
362 if (NS_SUCCEEDED(rv)) { \
363 rv = inst->QueryInterface(aIID, aResult); \
364 } \
365 NS_RELEASE(inst); \
366 \
367 return rv; \
368}
369
370#endif /* !defined (RT_OS_WINDOWS) */
371
372/**
373 * Declares a whar_t string literal from the argument.
374 * Necessary to overcome MSC / GCC differences.
375 * @param s expression to stringify
376 */
377#if defined (_MSC_VER)
378# define WSTR_LITERAL(s) L#s
379#elif defined (__GNUC__)
380# define WSTR_LITERAL(s) L""#s
381#else
382# error "Unsupported compiler!"
383#endif
384
385#endif /* ___VBox_com_defs_h */
386
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