VirtualBox

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

Last change on this file since 8194 was 8194, checked in by vboxsync, 16 years ago

OS/2: Fixed builds.

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