VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/base/nscore.h@ 4837

Last change on this file since 4837 was 3149, checked in by vboxsync, 17 years ago

XPCOM: Ported necessary bits of IPC/DConnect tp OS/2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37#ifndef nscore_h___
38#define nscore_h___
39
40/**
41 * Make sure that we have the proper platform specific
42 * c++ definitions needed by nscore.h
43 */
44#ifndef _XPCOM_CONFIG_H_
45#include "xpcom-config.h"
46#endif
47
48/**
49 * Incorporate the core NSPR data types which XPCOM uses.
50 */
51#include "prtypes.h"
52
53/* Core XPCOM declarations. */
54
55/**
56 * Macros defining the target platform...
57 */
58#ifdef _WIN32
59#define NS_WIN32 1
60
61#elif defined(__unix)
62#define NS_UNIX 1
63
64#elif defined(XP_OS2)
65#define NS_OS2 1
66#endif
67/*----------------------------------------------------------------------*/
68/* Import/export defines */
69
70/**
71 * Using the visibility("hidden") attribute allows the compiler to use
72 * PC-relative addressing to call this function. If a function does not
73 * access any global data, and does not call any methods which are not either
74 * file-local or hidden, then on ELF systems we avoid loading the address of
75 * the PLT into a register at the start of the function, which reduces code
76 * size and frees up a register for general use.
77 *
78 * As a general rule, this should be used for any non-exported symbol
79 * (including virtual method implementations). NS_IMETHOD uses this by
80 * default; if you need to have your NS_IMETHOD functions exported, you can
81 * wrap your class as follows:
82 *
83 * #undef IMETHOD_VISIBILITY
84 * #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
85 *
86 * class Foo {
87 * ...
88 * };
89 *
90 * #undef IMETHOD_VISIBILITY
91 * #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
92 *
93 * Don't forget to change the visibility back to hidden before the end
94 * of a header!
95 *
96 * Other examples:
97 *
98 * NS_HIDDEN_(int) someMethod();
99 * SomeCtor() NS_HIDDEN;
100 */
101
102#ifdef HAVE_VISIBILITY_ATTRIBUTE
103#define NS_VISIBILITY_HIDDEN __attribute__ ((visibility ("hidden")))
104# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
105# define NS_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
106# else
107# define NS_VISIBILITY_DEFAULT
108# endif
109
110#define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
111#else
112#define NS_VISIBILITY_HIDDEN
113#define NS_VISIBILITY_DEFAULT
114
115#define NS_HIDDEN_(type) type
116#endif
117
118#define NS_HIDDEN NS_VISIBILITY_HIDDEN
119
120#undef IMETHOD_VISIBILITY
121#define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
122
123/**
124 * Mark a function as using a potentially non-standard function calling
125 * convention. This can be used on functions that are called very
126 * frequently, to reduce the overhead of the function call. It is still worth
127 * using the macro for C++ functions which take no parameters since it allows
128 * passing |this| in a register.
129 *
130 * - Do not use this on any scriptable interface method since xptcall won't be
131 * aware of the different calling convention.
132 * - This must appear on the declaration, not the definition.
133 * - Adding this to a public function _will_ break binary compatibility.
134 * - This may be used on virtual functions but you must ensure it is applied
135 * to all implementations - the compiler will _not_ warn but it will crash.
136 * - This has no effect for inline functions or functions which take a
137 * variable number of arguments.
138 *
139 * Examples: int NS_FASTCALL func1(char *foo);
140 * NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
141 */
142
143#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
144#define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
145#else
146#define NS_FASTCALL
147#endif
148
149/*
150 * NS_DEFCALL undoes the effect of a global regparm/stdcall setting
151 * so that xptcall works correctly.
152 */
153#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
154#define NS_DEFCALL __attribute__ ((regparm (0), cdecl))
155#else
156#define NS_DEFCALL
157#endif
158
159#ifdef NS_WIN32
160
161#define NS_IMPORT __declspec(dllimport)
162#define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
163#define NS_EXPORT __declspec(dllexport)
164#define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
165#define NS_IMETHOD_(type) virtual type __stdcall
166#define NS_IMETHODIMP_(type) type __stdcall
167#define NS_METHOD_(type) type __stdcall
168#define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
169#define NS_STDCALL __stdcall
170
171#elif defined(XP_MAC)
172
173#define NS_IMPORT
174#define NS_IMPORT_(type) type
175#define NS_EXPORT __declspec(export)
176#define NS_EXPORT_(type) __declspec(export) type
177#define NS_IMETHOD_(type) virtual type
178#define NS_IMETHODIMP_(type) type
179#define NS_METHOD_(type) type
180#define NS_CALLBACK_(_type, _name) _type (* _name)
181#define NS_STDCALL
182
183#elif defined(XP_OS2) && defined(__declspec)
184
185#define NS_IMPORT __declspec(dllimport)
186#define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
187#define NS_EXPORT __declspec(dllexport)
188#define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
189#define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
190#define NS_IMETHODIMP_(type) type
191#define NS_METHOD_(type) type
192#define NS_CALLBACK_(_type, _name) _type (* _name)
193#define NS_STDCALL
194
195#else
196
197# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
198# define NS_IMPORT
199# define NS_IMPORT_(type) type
200# define NS_EXPORT __attribute__((visibility("default")))
201# define NS_EXPORT_(type) __attribute__((visibility("default"))) type
202# define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
203# define NS_IMETHODIMP_(type) type
204# define NS_METHOD_(type) type
205# define NS_CALLBACK_(_type, _name) _type (* _name)
206# define NS_STDCALL
207# else
208# define NS_IMPORT
209# define NS_IMPORT_(type) type
210# define NS_EXPORT
211# define NS_EXPORT_(type) type
212# define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
213# define NS_IMETHODIMP_(type) type
214# define NS_METHOD_(type) type
215# define NS_CALLBACK_(_type, _name) _type (* _name)
216# define NS_STDCALL
217# endif
218#endif
219
220/**
221 * Macro for creating typedefs for pointer-to-member types which are
222 * declared with stdcall. It is important to use this for any type which is
223 * declared as stdcall (i.e. NS_IMETHOD). For example, instead of writing:
224 *
225 * typedef nsresult (nsIFoo::*someType)(nsISupports* arg);
226 *
227 * you should write:
228 *
229 * typedef
230 * NS_STDCALL_FUNCPROTO(nsresult, someType, nsIFoo, typeFunc, (nsISupports*));
231 *
232 * where nsIFoo::typeFunc is any method declared as
233 * NS_IMETHOD typeFunc(nsISupports*);
234 *
235 * XXX this can be simplified to always use the non-typeof implementation
236 * when http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11893 is fixed.
237 */
238
239#ifdef __GNUC__
240#define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
241 typeof(&class::func) name
242#else
243#define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
244 ret (NS_STDCALL class::*name) args
245#endif
246
247/**
248 * Generic API modifiers which return the standard XPCOM nsresult type
249 */
250#define NS_IMETHOD NS_IMETHOD_(nsresult)
251#define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
252#define NS_METHOD NS_METHOD_(nsresult)
253#define NS_CALLBACK(_name) NS_CALLBACK_(nsresult, _name)
254
255/**
256 * Import/Export macros for XPCOM APIs
257 */
258
259#ifdef _IMPL_NS_COM
260#define NS_COM NS_EXPORT
261#elif _IMPL_NS_COM_OFF
262#define NS_COM
263#elif XPCOM_GLUE
264#define NS_COM
265#else
266#define NS_COM NS_IMPORT
267#endif
268
269
270/**
271 * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
272 * xpidl can determine that the interface can't contain a constructor.
273 * This results in some space savings and possible runtime savings -
274 * see bug 49416. We undefine it first, as xpidl-generated headers
275 * define it for IDL uses that don't include this file.
276 */
277#ifdef NS_NO_VTABLE
278#undef NS_NO_VTABLE
279#endif
280#if defined(_MSC_VER) && _MSC_VER >= 1100
281#define NS_NO_VTABLE __declspec(novtable)
282#else
283#define NS_NO_VTABLE
284#endif
285
286
287/**
288 * Generic XPCOM result data type
289 */
290typedef PRUint32 nsresult;
291
292/**
293 * The preferred symbol for null.
294 */
295#define nsnull 0
296
297#include "nsError.h"
298
299/* ------------------------------------------------------------------------ */
300/* Casting macros for hiding C++ features from older compilers */
301
302 /*
303 All our compiler support template specialization, but not all support the
304 |template <>| notation. The compiler that don't understand this notation
305 just omit it for specialization.
306
307 Need to add an autoconf test for this.
308 */
309
310 /* under Metrowerks (Mac), we don't have autoconf yet */
311#ifdef __MWERKS__
312 #define HAVE_CPP_PARTIAL_SPECIALIZATION
313 #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
314
315 #define HAVE_CPP_ACCESS_CHANGING_USING
316 #define HAVE_CPP_AMBIGUITY_RESOLVING_USING
317 #define HAVE_CPP_EXPLICIT
318 #define HAVE_CPP_TYPENAME
319 #define HAVE_CPP_BOOL
320 #define HAVE_CPP_NAMESPACE_STD
321 #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
322 #define HAVE_CPP_2BYTE_WCHAR_T
323#endif
324
325 /* under VC++ (Windows), we don't have autoconf yet */
326#if defined(_MSC_VER) && (_MSC_VER>=1100)
327 /* VC++ 5.0 and greater implement template specialization, 4.2 is unknown */
328 #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
329
330 #define HAVE_CPP_EXPLICIT
331 #define HAVE_CPP_TYPENAME
332 #define HAVE_CPP_ACCESS_CHANGING_USING
333
334 #if (_MSC_VER==1100)
335 /* VC++5.0 has an internal compiler error (sometimes) without this */
336 #undef HAVE_CPP_ACCESS_CHANGING_USING
337 #endif
338
339 #define HAVE_CPP_NAMESPACE_STD
340 #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
341 #define HAVE_CPP_2BYTE_WCHAR_T
342#endif
343
344#ifndef __PRUNICHAR__
345#define __PRUNICHAR__
346 /* For now, don't use wchar_t on Unix because it breaks the Netscape
347 * commercial build. When this is fixed there will be no need for the
348 * |NS_REINTERPRET_CAST| in nsLiteralString.h either.
349 */
350 #if defined(HAVE_CPP_2BYTE_WCHAR_T) && (defined(NS_WIN32) || defined(XP_MAC))
351 typedef wchar_t PRUnichar;
352 #else
353 typedef PRUint16 PRUnichar;
354 #endif
355#endif
356
357 /*
358 If the compiler doesn't support |explicit|, we'll just make it go away, trusting
359 that the builds under compilers that do have it will keep us on the straight and narrow.
360 */
361#ifndef HAVE_CPP_EXPLICIT
362 #define explicit
363#endif
364
365#ifndef HAVE_CPP_TYPENAME
366 #define typename
367#endif
368
369#ifdef HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
370 #define NS_SPECIALIZE_TEMPLATE template <>
371#else
372 #define NS_SPECIALIZE_TEMPLATE
373#endif
374
375/* unix and beos now determine this automatically */
376#if ! defined XP_UNIX && ! defined XP_BEOS && !defined(XP_OS2)
377#ifndef HAVE_CPP_NEW_CASTS
378#define HAVE_CPP_NEW_CASTS 1 /* we'll be optimistic. */
379#endif
380#endif
381
382#if defined(HAVE_CPP_NEW_CASTS)
383#define NS_STATIC_CAST(__type, __ptr) static_cast< __type >(__ptr)
384#define NS_CONST_CAST(__type, __ptr) const_cast< __type >(__ptr)
385
386#define NS_REINTERPRET_POINTER_CAST(__type, __ptr) reinterpret_cast< __type >(__ptr)
387#define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) reinterpret_cast< __type >(__obj)
388#define NS_REINTERPRET_CAST(__type, __expr) reinterpret_cast< __type >(__expr)
389
390#else
391#define NS_STATIC_CAST(__type, __ptr) ((__type)(__ptr))
392#define NS_CONST_CAST(__type, __ptr) ((__type)(__ptr))
393
394#define NS_REINTERPRET_POINTER_CAST(__type, __ptr) ((__type)((void*)(__ptr)))
395#define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) ((__type)(__obj))
396
397 /* Note: the following is only appropriate for pointers. */
398#define NS_REINTERPRET_CAST(__type, __expr) NS_REINTERPRET_POINTER_CAST(__type, __expr)
399 /*
400 Why cast to a |void*| first? Well, when old-style casting from
401 a pointer to a base to a pointer to a derived class, the cast will be
402 ambiguous if the source pointer type appears multiple times in the
403 destination, e.g.,
404
405 class Base {};
406 class Derived : public Base, public Base {};
407
408 void foo( Base* b )
409 {
410 ((Derived*)b)->some_derived_member ... // Error: Ambiguous, expand from which |Base|?
411 }
412
413 an old-style cast (like |static_cast|) will change the pointer, but
414 here, doesn't know how. The cast to |void*| prevents it from thinking
415 it needs to expand the original pointer.
416
417 The cost is, |NS_REINTERPRET_CAST| is no longer appropriate for non-pointer
418 conversions. Also, mis-applying |NS_REINTERPRET_CAST| to cast |this| to something
419 will still expand the pointer to the outer object in standards complying compilers.
420 */
421
422 /*
423 No sense in making an NS_DYNAMIC_CAST() macro: you can't duplicate
424 the semantics. So if you want to dynamic_cast, then just use it
425 "straight", no macro.
426 */
427#endif
428
429/*
430 * Use these macros to do 64bit safe pointer conversions.
431 */
432
433#define NS_PTR_TO_INT32(x) ((char *)(x) - (char *)0)
434#define NS_INT32_TO_PTR(x) ((void *)((char *)0 + (x)))
435
436/*
437 * These macros allow you to give a hint to the compiler about branch
438 * probability so that it can better optimize. Use them like this:
439 *
440 * if (NS_LIKELY(v == 1)) {
441 * ... expected code path ...
442 * }
443 *
444 * if (NS_UNLIKELY(v == 0)) {
445 * ... non-expected code path ...
446 * }
447 *
448 */
449
450#if defined(__GNUC__) && (__GNUC__ > 2)
451#define NS_LIKELY(x) (__builtin_expect((x), 1))
452#define NS_UNLIKELY(x) (__builtin_expect((x), 0))
453#else
454#define NS_LIKELY(x) (x)
455#define NS_UNLIKELY(x) (x)
456#endif
457
458#endif /* nscore_h___ */
459
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use