Back to index

lightning-sunbird  0.9+nobinonly
nscore.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef nscore_h___
00038 #define nscore_h___
00039 
00044 #ifndef _XPCOM_CONFIG_H_
00045 #include "xpcom-config.h"
00046 #endif
00047 
00051 #include "prtypes.h"
00052 
00053 /* Core XPCOM declarations. */
00054 
00058 #ifdef _WIN32
00059 #define NS_WIN32 1
00060 
00061 #elif defined(__unix)
00062 #define NS_UNIX 1
00063 
00064 #elif defined(XP_OS2)
00065 #define NS_OS2 1
00066 #endif
00067 /*----------------------------------------------------------------------*/
00068 /* Import/export defines */
00069 
00102 #ifdef HAVE_VISIBILITY_HIDDEN_ATTRIBUTE
00103 #define NS_VISIBILITY_HIDDEN   __attribute__ ((visibility ("hidden")))
00104 #else
00105 #define NS_VISIBILITY_HIDDEN
00106 #endif
00107 
00108 #if defined(HAVE_VISIBILITY_ATTRIBUTE)
00109 #define NS_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
00110 #else
00111 #define NS_VISIBILITY_DEFAULT
00112 #endif
00113 
00114 #define NS_HIDDEN_(type)   NS_VISIBILITY_HIDDEN type
00115 #define NS_EXTERNAL_VIS_(type) NS_VISIBILITY_DEFAULT type
00116 
00117 #define NS_HIDDEN           NS_VISIBILITY_HIDDEN
00118 #define NS_EXTERNAL_VIS     NS_VISIBILITY_DEFAULT
00119 
00120 #undef  IMETHOD_VISIBILITY
00121 #define IMETHOD_VISIBILITY  NS_VISIBILITY_HIDDEN
00122 
00143 #if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
00144 #define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
00145 #else
00146 #define NS_FASTCALL
00147 #endif
00148 
00149 /*
00150  * NS_DEFCALL undoes the effect of a global regparm/stdcall setting
00151  * so that xptcall works correctly.
00152  */
00153 #if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
00154 #define NS_DEFCALL __attribute__ ((regparm (0), cdecl))
00155 #else
00156 #define NS_DEFCALL
00157 #endif
00158 
00159 #ifdef NS_WIN32
00160 
00161 #define NS_IMPORT __declspec(dllimport)
00162 #define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
00163 #define NS_EXPORT __declspec(dllexport)
00164 #define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
00165 #define NS_IMETHOD_(type) virtual type __stdcall
00166 #define NS_IMETHODIMP_(type) type __stdcall
00167 #define NS_METHOD_(type) type __stdcall
00168 #define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
00169 #define NS_STDCALL __stdcall
00170 
00171 /*
00172   These are needed to mark static members in exported classes, due to
00173   gcc bug XXX insert bug# here.
00174  */
00175 
00176 #define NS_EXPORT_STATIC_MEMBER_(type) type
00177 #define NS_IMPORT_STATIC_MEMBER_(type) type
00178 
00179 #else
00180 
00181 #define NS_IMPORT NS_EXTERNAL_VIS
00182 #define NS_IMPORT_(type) NS_EXTERNAL_VIS_(type)
00183 #define NS_EXPORT NS_EXTERNAL_VIS
00184 #define NS_EXPORT_(type) NS_EXTERNAL_VIS_(type)
00185 #define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
00186 #define NS_IMETHODIMP_(type) type
00187 #define NS_METHOD_(type) type
00188 #define NS_CALLBACK_(_type, _name) _type (* _name)
00189 #define NS_STDCALL
00190 #define NS_EXPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
00191 #define NS_IMPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
00192 
00193 #endif
00194 
00214 #ifdef __GNUC__
00215 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
00216   typeof(&class::func) name
00217 #else
00218 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
00219   ret (NS_STDCALL class::*name) args
00220 #endif
00221 
00225 #define NS_IMETHOD          NS_IMETHOD_(nsresult)
00226 #define NS_IMETHODIMP       NS_IMETHODIMP_(nsresult)
00227 #define NS_METHOD           NS_METHOD_(nsresult)
00228 #define NS_CALLBACK(_name)  NS_CALLBACK_(nsresult, _name)
00229 
00234 #ifdef _IMPL_NS_COM
00235 #define NS_COM NS_EXPORT
00236 #elif  defined(_IMPL_NS_COM_OFF)
00237 #define NS_COM
00238 #elif  defined(XPCOM_GLUE)
00239 #define NS_COM
00240 #else
00241 #define NS_COM NS_IMPORT
00242 #endif
00243 
00244 #ifdef MOZILLA_INTERNAL_API
00245 #  define NS_COM_GLUE NS_COM
00246    /*
00247      The frozen string API has different definitions of nsAC?String
00248      classes than the internal API. On systems that explicitly declare
00249      dllexport symbols this is not a problem, but on ELF systems
00250      internal symbols can accidentally "shine through"; we rename the
00251      internal classes to avoid symbol conflicts.
00252    */
00253 #  define nsAString nsAString_internal
00254 #  define nsACString nsACString_internal
00255 #else
00256 #  define NS_COM_GLUE
00257 #endif
00258 
00259 
00267 #ifdef NS_NO_VTABLE
00268 #undef NS_NO_VTABLE
00269 #endif
00270 #if defined(_MSC_VER) && _MSC_VER >= 1100
00271 #define NS_NO_VTABLE __declspec(novtable)
00272 #else
00273 #define NS_NO_VTABLE
00274 #endif
00275 
00276 
00280 typedef PRUint32 nsresult;
00281 
00285 #define nsnull 0
00286 
00287 #include "nsError.h"
00288 
00289 /* ------------------------------------------------------------------------ */
00290 /* Casting macros for hiding C++ features from older compilers */
00291 
00292   /*
00293     All our compiler support template specialization, but not all support the
00294     |template <>| notation.  The compiler that don't understand this notation
00295     just omit it for specialization.
00296 
00297     Need to add an autoconf test for this.
00298   */
00299 
00300   /* under Metrowerks (Mac), we don't have autoconf yet */
00301 #ifdef __MWERKS__
00302   #define HAVE_CPP_PARTIAL_SPECIALIZATION
00303   #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
00304 
00305   #define HAVE_CPP_ACCESS_CHANGING_USING
00306   #define HAVE_CPP_AMBIGUITY_RESOLVING_USING
00307   #define HAVE_CPP_EXPLICIT
00308   #define HAVE_CPP_TYPENAME
00309   #define HAVE_CPP_BOOL
00310   #define HAVE_CPP_NAMESPACE_STD
00311   #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
00312   #define HAVE_CPP_2BYTE_WCHAR_T
00313 #endif
00314 
00315   /* under VC++ (Windows), we don't have autoconf yet */
00316 #if defined(_MSC_VER) && (_MSC_VER>=1100)
00317   /* VC++ 5.0 and greater implement template specialization, 4.2 is unknown */
00318   #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
00319 
00320   #define HAVE_CPP_EXPLICIT
00321   #define HAVE_CPP_TYPENAME
00322   #define HAVE_CPP_ACCESS_CHANGING_USING
00323 
00324   #if (_MSC_VER==1100)
00325       /* VC++5.0 has an internal compiler error (sometimes) without this */
00326     #undef HAVE_CPP_ACCESS_CHANGING_USING
00327   #endif
00328 
00329   #define HAVE_CPP_NAMESPACE_STD
00330   #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
00331   #define HAVE_CPP_2BYTE_WCHAR_T
00332 #endif
00333 
00334 #ifndef __PRUNICHAR__
00335 #define __PRUNICHAR__
00336   /* For now, don't use wchar_t on Unix because it breaks the Netscape
00337    * commercial build.  When this is fixed there will be no need for the
00338    * |NS_REINTERPRET_CAST| in nsLiteralString.h either.
00339    */
00340   #if defined(HAVE_CPP_2BYTE_WCHAR_T) && defined(NS_WIN32)
00341     typedef wchar_t PRUnichar;
00342   #else
00343     typedef PRUint16 PRUnichar;
00344   #endif
00345 #endif
00346 
00347   /*
00348     If the compiler doesn't support |explicit|, we'll just make it go away, trusting
00349     that the builds under compilers that do have it will keep us on the straight and narrow.
00350   */
00351 #ifndef HAVE_CPP_EXPLICIT
00352   #define explicit
00353 #endif
00354 
00355 #ifndef HAVE_CPP_TYPENAME
00356   #define typename
00357 #endif
00358 
00359 #ifdef HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
00360   #define NS_SPECIALIZE_TEMPLATE  template <>
00361 #else
00362   #define NS_SPECIALIZE_TEMPLATE
00363 #endif
00364 
00365 /* unix and beos now determine this automatically */
00366 #if ! defined XP_UNIX && ! defined XP_BEOS && !defined(XP_OS2)
00367 #ifndef HAVE_CPP_NEW_CASTS
00368 #define HAVE_CPP_NEW_CASTS 1 /* we'll be optimistic. */
00369 #endif
00370 #endif
00371 
00372 #if defined(HAVE_CPP_NEW_CASTS)
00373 #define NS_STATIC_CAST(__type, __ptr)      static_cast< __type >(__ptr)
00374 #define NS_CONST_CAST(__type, __ptr)       const_cast< __type >(__ptr)
00375 
00376 #define NS_REINTERPRET_POINTER_CAST(__type, __ptr)    reinterpret_cast< __type >(__ptr)
00377 #define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) reinterpret_cast< __type >(__obj)
00378 #define NS_REINTERPRET_CAST(__type, __expr)           reinterpret_cast< __type >(__expr)
00379 
00380 #else
00381 #define NS_STATIC_CAST(__type, __ptr)      ((__type)(__ptr))
00382 #define NS_CONST_CAST(__type, __ptr)       ((__type)(__ptr))
00383 
00384 #define NS_REINTERPRET_POINTER_CAST(__type, __ptr)     ((__type)((void*)(__ptr)))
00385 #define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj)  ((__type)(__obj))
00386 
00387   /* Note: the following is only appropriate for pointers. */
00388 #define NS_REINTERPRET_CAST(__type, __expr)            NS_REINTERPRET_POINTER_CAST(__type, __expr)
00389   /*
00390     Why cast to a |void*| first?  Well, when old-style casting from
00391     a pointer to a base to a pointer to a derived class, the cast will be
00392     ambiguous if the source pointer type appears multiple times in the
00393     destination, e.g.,
00394     
00395       class Base {};
00396       class Derived : public Base, public Base {};
00397       
00398       void foo( Base* b )
00399         {
00400           ((Derived*)b)->some_derived_member ... // Error: Ambiguous, expand from which |Base|?
00401         }
00402 
00403     an old-style cast (like |static_cast|) will change the pointer, but
00404     here, doesn't know how.  The cast to |void*| prevents it from thinking
00405     it needs to expand the original pointer.
00406 
00407     The cost is, |NS_REINTERPRET_CAST| is no longer appropriate for non-pointer
00408     conversions.  Also, mis-applying |NS_REINTERPRET_CAST| to cast |this| to something
00409     will still expand the pointer to the outer object in standards complying compilers.
00410   */
00411 
00412   /*
00413     No sense in making an NS_DYNAMIC_CAST() macro: you can't duplicate
00414     the semantics. So if you want to dynamic_cast, then just use it
00415     "straight", no macro.
00416   */
00417 #endif
00418  
00419 /* 
00420  * Use these macros to do 64bit safe pointer conversions.
00421  */
00422 
00423 #define NS_PTR_TO_INT32(x)  ((PRInt32)  (PRWord) (x))
00424 #define NS_PTR_TO_UINT32(x) ((PRUint32) (PRWord) (x))
00425 #define NS_INT32_TO_PTR(x)  ((void *)   (PRWord) (x))
00426 
00427 /*
00428  * Use NS_STRINGIFY to form a string literal from the value of a macro.
00429  */
00430 #define NS_STRINGIFY_HELPER(x_) #x_
00431 #define NS_STRINGIFY(x_) NS_STRINGIFY_HELPER(x_)
00432 
00433 /*
00434  * These macros allow you to give a hint to the compiler about branch
00435  * probability so that it can better optimize.  Use them like this:
00436  *
00437  *  if (NS_LIKELY(v == 1)) {
00438  *    ... expected code path ...
00439  *  }
00440  *
00441  *  if (NS_UNLIKELY(v == 0)) {
00442  *    ... non-expected code path ...
00443  *  }
00444  *
00445  */
00446 
00447 #if defined(__GNUC__) && (__GNUC__ > 2)
00448 #define NS_LIKELY(x)    (__builtin_expect((x), 1))
00449 #define NS_UNLIKELY(x)  (__builtin_expect((x), 0))
00450 #else
00451 #define NS_LIKELY(x)    (x)
00452 #define NS_UNLIKELY(x)  (x)
00453 #endif
00454 
00455 #endif /* nscore_h___ */