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 | #define NS_VISIBILITY_DEFAULT
|
---|
105 |
|
---|
106 | #define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
|
---|
107 | #else
|
---|
108 | #define NS_VISIBILITY_HIDDEN
|
---|
109 | #define NS_VISIBILITY_DEFAULT
|
---|
110 |
|
---|
111 | #define NS_HIDDEN_(type) type
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | #define NS_HIDDEN NS_VISIBILITY_HIDDEN
|
---|
115 |
|
---|
116 | #undef IMETHOD_VISIBILITY
|
---|
117 | #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Mark a function as using a potentially non-standard function calling
|
---|
121 | * convention. This can be used on functions that are called very
|
---|
122 | * frequently, to reduce the overhead of the function call. It is still worth
|
---|
123 | * using the macro for C++ functions which take no parameters since it allows
|
---|
124 | * passing |this| in a register.
|
---|
125 | *
|
---|
126 | * - Do not use this on any scriptable interface method since xptcall won't be
|
---|
127 | * aware of the different calling convention.
|
---|
128 | * - This must appear on the declaration, not the definition.
|
---|
129 | * - Adding this to a public function _will_ break binary compatibility.
|
---|
130 | * - This may be used on virtual functions but you must ensure it is applied
|
---|
131 | * to all implementations - the compiler will _not_ warn but it will crash.
|
---|
132 | * - This has no effect for inline functions or functions which take a
|
---|
133 | * variable number of arguments.
|
---|
134 | *
|
---|
135 | * Examples: int NS_FASTCALL func1(char *foo);
|
---|
136 | * NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
|
---|
137 | */
|
---|
138 |
|
---|
139 | #if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
|
---|
140 | #define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
|
---|
141 | #else
|
---|
142 | #define NS_FASTCALL
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * NS_DEFCALL undoes the effect of a global regparm/stdcall setting
|
---|
147 | * so that xptcall works correctly.
|
---|
148 | */
|
---|
149 | #if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
|
---|
150 | #define NS_DEFCALL __attribute__ ((regparm (0), cdecl))
|
---|
151 | #else
|
---|
152 | #define NS_DEFCALL
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | #ifdef NS_WIN32
|
---|
156 |
|
---|
157 | #define NS_IMPORT __declspec(dllimport)
|
---|
158 | #define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
|
---|
159 | #define NS_EXPORT __declspec(dllexport)
|
---|
160 | #define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
|
---|
161 | #define NS_IMETHOD_(type) virtual type __stdcall
|
---|
162 | #define NS_IMETHODIMP_(type) type __stdcall
|
---|
163 | #define NS_METHOD_(type) type __stdcall
|
---|
164 | #define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
|
---|
165 | #define NS_STDCALL __stdcall
|
---|
166 |
|
---|
167 | #elif defined(XP_MAC)
|
---|
168 |
|
---|
169 | #define NS_IMPORT
|
---|
170 | #define NS_IMPORT_(type) type
|
---|
171 | #define NS_EXPORT __declspec(export)
|
---|
172 | #define NS_EXPORT_(type) __declspec(export) type
|
---|
173 | #define NS_IMETHOD_(type) virtual type
|
---|
174 | #define NS_IMETHODIMP_(type) type
|
---|
175 | #define NS_METHOD_(type) type
|
---|
176 | #define NS_CALLBACK_(_type, _name) _type (* _name)
|
---|
177 | #define NS_STDCALL
|
---|
178 |
|
---|
179 | #else
|
---|
180 |
|
---|
181 | #define NS_IMPORT
|
---|
182 | #define NS_IMPORT_(type) type
|
---|
183 | #define NS_EXPORT
|
---|
184 | #define NS_EXPORT_(type) type
|
---|
185 | #define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
|
---|
186 | #define NS_IMETHODIMP_(type) type
|
---|
187 | #define NS_METHOD_(type) type
|
---|
188 | #define NS_CALLBACK_(_type, _name) _type (* _name)
|
---|
189 | #define NS_STDCALL
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Macro for creating typedefs for pointer-to-member types which are
|
---|
194 | * declared with stdcall. It is important to use this for any type which is
|
---|
195 | * declared as stdcall (i.e. NS_IMETHOD). For example, instead of writing:
|
---|
196 | *
|
---|
197 | * typedef nsresult (nsIFoo::*someType)(nsISupports* arg);
|
---|
198 | *
|
---|
199 | * you should write:
|
---|
200 | *
|
---|
201 | * typedef
|
---|
202 | * NS_STDCALL_FUNCPROTO(nsresult, someType, nsIFoo, typeFunc, (nsISupports*));
|
---|
203 | *
|
---|
204 | * where nsIFoo::typeFunc is any method declared as
|
---|
205 | * NS_IMETHOD typeFunc(nsISupports*);
|
---|
206 | *
|
---|
207 | * XXX this can be simplified to always use the non-typeof implementation
|
---|
208 | * when http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11893 is fixed.
|
---|
209 | */
|
---|
210 |
|
---|
211 | #ifdef __GNUC__
|
---|
212 | #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
|
---|
213 | typeof(&class::func) name
|
---|
214 | #else
|
---|
215 | #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
|
---|
216 | ret (NS_STDCALL class::*name) args
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Generic API modifiers which return the standard XPCOM nsresult type
|
---|
221 | */
|
---|
222 | #define NS_IMETHOD NS_IMETHOD_(nsresult)
|
---|
223 | #define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
|
---|
224 | #define NS_METHOD NS_METHOD_(nsresult)
|
---|
225 | #define NS_CALLBACK(_name) NS_CALLBACK_(nsresult, _name)
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Import/Export macros for XPCOM APIs
|
---|
229 | */
|
---|
230 |
|
---|
231 | #ifdef _IMPL_NS_COM
|
---|
232 | #define NS_COM NS_EXPORT
|
---|
233 | #elif _IMPL_NS_COM_OFF
|
---|
234 | #define NS_COM
|
---|
235 | #elif XPCOM_GLUE
|
---|
236 | #define NS_COM
|
---|
237 | #else
|
---|
238 | #define NS_COM NS_IMPORT
|
---|
239 | #endif
|
---|
240 |
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
|
---|
244 | * xpidl can determine that the interface can't contain a constructor.
|
---|
245 | * This results in some space savings and possible runtime savings -
|
---|
246 | * see bug 49416. We undefine it first, as xpidl-generated headers
|
---|
247 | * define it for IDL uses that don't include this file.
|
---|
248 | */
|
---|
249 | #ifdef NS_NO_VTABLE
|
---|
250 | #undef NS_NO_VTABLE
|
---|
251 | #endif
|
---|
252 | #if defined(_MSC_VER) && _MSC_VER >= 1100
|
---|
253 | #define NS_NO_VTABLE __declspec(novtable)
|
---|
254 | #else
|
---|
255 | #define NS_NO_VTABLE
|
---|
256 | #endif
|
---|
257 |
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Generic XPCOM result data type
|
---|
261 | */
|
---|
262 | typedef PRUint32 nsresult;
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * The preferred symbol for null.
|
---|
266 | */
|
---|
267 | #define nsnull 0
|
---|
268 |
|
---|
269 | #include "nsError.h"
|
---|
270 |
|
---|
271 | /* ------------------------------------------------------------------------ */
|
---|
272 | /* Casting macros for hiding C++ features from older compilers */
|
---|
273 |
|
---|
274 | /*
|
---|
275 | All our compiler support template specialization, but not all support the
|
---|
276 | |template <>| notation. The compiler that don't understand this notation
|
---|
277 | just omit it for specialization.
|
---|
278 |
|
---|
279 | Need to add an autoconf test for this.
|
---|
280 | */
|
---|
281 |
|
---|
282 | /* under Metrowerks (Mac), we don't have autoconf yet */
|
---|
283 | #ifdef __MWERKS__
|
---|
284 | #define HAVE_CPP_PARTIAL_SPECIALIZATION
|
---|
285 | #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
|
---|
286 |
|
---|
287 | #define HAVE_CPP_ACCESS_CHANGING_USING
|
---|
288 | #define HAVE_CPP_AMBIGUITY_RESOLVING_USING
|
---|
289 | #define HAVE_CPP_EXPLICIT
|
---|
290 | #define HAVE_CPP_TYPENAME
|
---|
291 | #define HAVE_CPP_BOOL
|
---|
292 | #define HAVE_CPP_NAMESPACE_STD
|
---|
293 | #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
|
---|
294 | #define HAVE_CPP_2BYTE_WCHAR_T
|
---|
295 | #endif
|
---|
296 |
|
---|
297 | /* under VC++ (Windows), we don't have autoconf yet */
|
---|
298 | #if defined(_MSC_VER) && (_MSC_VER>=1100)
|
---|
299 | /* VC++ 5.0 and greater implement template specialization, 4.2 is unknown */
|
---|
300 | #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
|
---|
301 |
|
---|
302 | #define HAVE_CPP_EXPLICIT
|
---|
303 | #define HAVE_CPP_TYPENAME
|
---|
304 | #define HAVE_CPP_ACCESS_CHANGING_USING
|
---|
305 |
|
---|
306 | #if (_MSC_VER==1100)
|
---|
307 | /* VC++5.0 has an internal compiler error (sometimes) without this */
|
---|
308 | #undef HAVE_CPP_ACCESS_CHANGING_USING
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | #define HAVE_CPP_NAMESPACE_STD
|
---|
312 | #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
|
---|
313 | #define HAVE_CPP_2BYTE_WCHAR_T
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | #ifndef __PRUNICHAR__
|
---|
317 | #define __PRUNICHAR__
|
---|
318 | /* For now, don't use wchar_t on Unix because it breaks the Netscape
|
---|
319 | * commercial build. When this is fixed there will be no need for the
|
---|
320 | * |NS_REINTERPRET_CAST| in nsLiteralString.h either.
|
---|
321 | */
|
---|
322 | #if defined(HAVE_CPP_2BYTE_WCHAR_T) && (defined(NS_WIN32) || defined(XP_MAC))
|
---|
323 | typedef wchar_t PRUnichar;
|
---|
324 | #else
|
---|
325 | typedef PRUint16 PRUnichar;
|
---|
326 | #endif
|
---|
327 | #endif
|
---|
328 |
|
---|
329 | /*
|
---|
330 | If the compiler doesn't support |explicit|, we'll just make it go away, trusting
|
---|
331 | that the builds under compilers that do have it will keep us on the straight and narrow.
|
---|
332 | */
|
---|
333 | #ifndef HAVE_CPP_EXPLICIT
|
---|
334 | #define explicit
|
---|
335 | #endif
|
---|
336 |
|
---|
337 | #ifndef HAVE_CPP_TYPENAME
|
---|
338 | #define typename
|
---|
339 | #endif
|
---|
340 |
|
---|
341 | #ifdef HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
|
---|
342 | #define NS_SPECIALIZE_TEMPLATE template <>
|
---|
343 | #else
|
---|
344 | #define NS_SPECIALIZE_TEMPLATE
|
---|
345 | #endif
|
---|
346 |
|
---|
347 | /* unix and beos now determine this automatically */
|
---|
348 | #if ! defined XP_UNIX && ! defined XP_BEOS && !defined(XP_OS2)
|
---|
349 | #ifndef HAVE_CPP_NEW_CASTS
|
---|
350 | #define HAVE_CPP_NEW_CASTS 1 /* we'll be optimistic. */
|
---|
351 | #endif
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | #if defined(HAVE_CPP_NEW_CASTS)
|
---|
355 | #define NS_STATIC_CAST(__type, __ptr) static_cast< __type >(__ptr)
|
---|
356 | #define NS_CONST_CAST(__type, __ptr) const_cast< __type >(__ptr)
|
---|
357 |
|
---|
358 | #define NS_REINTERPRET_POINTER_CAST(__type, __ptr) reinterpret_cast< __type >(__ptr)
|
---|
359 | #define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) reinterpret_cast< __type >(__obj)
|
---|
360 | #define NS_REINTERPRET_CAST(__type, __expr) reinterpret_cast< __type >(__expr)
|
---|
361 |
|
---|
362 | #else
|
---|
363 | #define NS_STATIC_CAST(__type, __ptr) ((__type)(__ptr))
|
---|
364 | #define NS_CONST_CAST(__type, __ptr) ((__type)(__ptr))
|
---|
365 |
|
---|
366 | #define NS_REINTERPRET_POINTER_CAST(__type, __ptr) ((__type)((void*)(__ptr)))
|
---|
367 | #define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) ((__type)(__obj))
|
---|
368 |
|
---|
369 | /* Note: the following is only appropriate for pointers. */
|
---|
370 | #define NS_REINTERPRET_CAST(__type, __expr) NS_REINTERPRET_POINTER_CAST(__type, __expr)
|
---|
371 | /*
|
---|
372 | Why cast to a |void*| first? Well, when old-style casting from
|
---|
373 | a pointer to a base to a pointer to a derived class, the cast will be
|
---|
374 | ambiguous if the source pointer type appears multiple times in the
|
---|
375 | destination, e.g.,
|
---|
376 |
|
---|
377 | class Base {};
|
---|
378 | class Derived : public Base, public Base {};
|
---|
379 |
|
---|
380 | void foo( Base* b )
|
---|
381 | {
|
---|
382 | ((Derived*)b)->some_derived_member ... // Error: Ambiguous, expand from which |Base|?
|
---|
383 | }
|
---|
384 |
|
---|
385 | an old-style cast (like |static_cast|) will change the pointer, but
|
---|
386 | here, doesn't know how. The cast to |void*| prevents it from thinking
|
---|
387 | it needs to expand the original pointer.
|
---|
388 |
|
---|
389 | The cost is, |NS_REINTERPRET_CAST| is no longer appropriate for non-pointer
|
---|
390 | conversions. Also, mis-applying |NS_REINTERPRET_CAST| to cast |this| to something
|
---|
391 | will still expand the pointer to the outer object in standards complying compilers.
|
---|
392 | */
|
---|
393 |
|
---|
394 | /*
|
---|
395 | No sense in making an NS_DYNAMIC_CAST() macro: you can't duplicate
|
---|
396 | the semantics. So if you want to dynamic_cast, then just use it
|
---|
397 | "straight", no macro.
|
---|
398 | */
|
---|
399 | #endif
|
---|
400 |
|
---|
401 | /*
|
---|
402 | * Use these macros to do 64bit safe pointer conversions.
|
---|
403 | */
|
---|
404 |
|
---|
405 | #define NS_PTR_TO_INT32(x) ((char *)(x) - (char *)0)
|
---|
406 | #define NS_INT32_TO_PTR(x) ((void *)((char *)0 + (x)))
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * These macros allow you to give a hint to the compiler about branch
|
---|
410 | * probability so that it can better optimize. Use them like this:
|
---|
411 | *
|
---|
412 | * if (NS_LIKELY(v == 1)) {
|
---|
413 | * ... expected code path ...
|
---|
414 | * }
|
---|
415 | *
|
---|
416 | * if (NS_UNLIKELY(v == 0)) {
|
---|
417 | * ... non-expected code path ...
|
---|
418 | * }
|
---|
419 | *
|
---|
420 | */
|
---|
421 |
|
---|
422 | #if defined(__GNUC__) && (__GNUC__ > 2)
|
---|
423 | #define NS_LIKELY(x) (__builtin_expect((x), 1))
|
---|
424 | #define NS_UNLIKELY(x) (__builtin_expect((x), 0))
|
---|
425 | #else
|
---|
426 | #define NS_LIKELY(x) (x)
|
---|
427 | #define NS_UNLIKELY(x) (x)
|
---|
428 | #endif
|
---|
429 |
|
---|
430 | #endif /* nscore_h___ */
|
---|
431 |
|
---|