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 
00040 /**
00041  * Incorporate the core NSPR data types which XPCOM uses.
00042  */
00043 #include "prtypes.h"
00044 
00045 /* Core XPCOM declarations. */
00046 
00047 /**
00048  * Macros defining the target platform...
00049  */
00050 #ifdef _WIN32
00051 #define NS_WIN32 1
00052 
00053 #elif defined(__unix)
00054 #define NS_UNIX 1
00055 
00056 #elif defined(XP_OS2)
00057 #define NS_OS2 1
00058 #endif
00059 /*----------------------------------------------------------------------*/
00060 /* Import/export defines */
00061 
00062 /**
00063  * Using the visibility("hidden") attribute allows the compiler to use
00064  * PC-relative addressing to call this function.  If a function does not
00065  * access any global data, and does not call any methods which are not either
00066  * file-local or hidden, then on ELF systems we avoid loading the address of
00067  * the PLT into a register at the start of the function, which reduces code
00068  * size and frees up a register for general use.
00069  *
00070  * As a general rule, this should be used for any non-exported symbol
00071  * (including virtual method implementations).  NS_IMETHOD uses this by
00072  * default; if you need to have your NS_IMETHOD functions exported, you can
00073  * wrap your class as follows:
00074  *
00075  * #undef  IMETHOD_VISIBILITY
00076  * #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
00077  *
00078  * class Foo {
00079  * ...
00080  * };
00081  *
00082  * #undef  IMETHOD_VISIBILITY
00083  * #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
00084  *
00085  * Don't forget to change the visibility back to hidden before the end
00086  * of a header!
00087  *
00088  * Other examples:
00089  *
00090  * NS_HIDDEN_(int) someMethod();
00091  * SomeCtor() NS_HIDDEN;
00092  */
00093 
00094 #ifdef HAVE_VISIBILITY_HIDDEN_ATTRIBUTE
00095 #define NS_VISIBILITY_HIDDEN   __attribute__ ((visibility ("hidden")))
00096 #else
00097 #define NS_VISIBILITY_HIDDEN
00098 #endif
00099 
00100 #if defined(HAVE_VISIBILITY_ATTRIBUTE)
00101 #define NS_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
00102 #else
00103 #define NS_VISIBILITY_DEFAULT
00104 #endif
00105 
00106 #define NS_HIDDEN_(type)   NS_VISIBILITY_HIDDEN type
00107 #define NS_EXTERNAL_VIS_(type) NS_VISIBILITY_DEFAULT type
00108 
00109 #define NS_HIDDEN           NS_VISIBILITY_HIDDEN
00110 #define NS_EXTERNAL_VIS     NS_VISIBILITY_DEFAULT
00111 
00112 #undef  IMETHOD_VISIBILITY
00113 #define IMETHOD_VISIBILITY  NS_VISIBILITY_HIDDEN
00114 
00115 /**
00116  * Mark a function as using a potentially non-standard function calling
00117  * convention.  This can be used on functions that are called very
00118  * frequently, to reduce the overhead of the function call.  It is still worth
00119  * using the macro for C++ functions which take no parameters since it allows
00120  * passing |this| in a register.
00121  *
00122  *  - Do not use this on any scriptable interface method since xptcall won't be
00123  *    aware of the different calling convention.
00124  *  - This must appear on the declaration, not the definition.
00125  *  - Adding this to a public function _will_ break binary compatibility.
00126  *  - This may be used on virtual functions but you must ensure it is applied
00127  *    to all implementations - the compiler will _not_ warn but it will crash.
00128  *  - This has no effect for inline functions or functions which take a
00129  *    variable number of arguments.
00130  *
00131  * Examples: int NS_FASTCALL func1(char *foo);
00132  *           NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
00133  */
00134 
00135 #if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
00136 #define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
00137 #else
00138 #define NS_FASTCALL
00139 #endif
00140 
00141 /*
00142  * NS_DEFCALL undoes the effect of a global regparm/stdcall setting
00143  * so that xptcall works correctly.
00144  */
00145 #if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
00146 #define NS_DEFCALL __attribute__ ((regparm (0), cdecl))
00147 #else
00148 #define NS_DEFCALL
00149 #endif
00150 
00151 #ifdef NS_WIN32
00152 
00153 #define NS_IMPORT __declspec(dllimport)
00154 #define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
00155 #define NS_EXPORT __declspec(dllexport)
00156 #define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
00157 #define NS_IMETHOD_(type) virtual type __stdcall
00158 #define NS_IMETHODIMP_(type) type __stdcall
00159 #define NS_METHOD_(type) type __stdcall
00160 #define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
00161 #define NS_STDCALL __stdcall
00162 
00163 /*
00164   These are needed to mark static members in exported classes, due to
00165   gcc bug XXX insert bug# here.
00166  */
00167 
00168 #define NS_EXPORT_STATIC_MEMBER_(type) type
00169 #define NS_IMPORT_STATIC_MEMBER_(type) type
00170 
00171 #else
00172 
00173 #define NS_IMPORT NS_EXTERNAL_VIS
00174 #define NS_IMPORT_(type) NS_EXTERNAL_VIS_(type)
00175 #define NS_EXPORT NS_EXTERNAL_VIS
00176 #define NS_EXPORT_(type) NS_EXTERNAL_VIS_(type)
00177 #define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
00178 #define NS_IMETHODIMP_(type) type
00179 #define NS_METHOD_(type) type
00180 #define NS_CALLBACK_(_type, _name) _type (* _name)
00181 #define NS_STDCALL
00182 #define NS_EXPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
00183 #define NS_IMPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
00184 
00185 #endif
00186 
00187 /**
00188  * Macro for creating typedefs for pointer-to-member types which are
00189  * declared with stdcall.  It is important to use this for any type which is
00190  * declared as stdcall (i.e. NS_IMETHOD).  For example, instead of writing:
00191  *
00192  *  typedef nsresult (nsIFoo::*someType)(nsISupports* arg);
00193  *
00194  *  you should write:
00195  *
00196  *  typedef
00197  *  NS_STDCALL_FUNCPROTO(nsresult, someType, nsIFoo, typeFunc, (nsISupports*));
00198  *
00199  *  where nsIFoo::typeFunc is any method declared as
00200  *  NS_IMETHOD typeFunc(nsISupports*);
00201  *
00202  *  XXX this can be simplified to always use the non-typeof implementation
00203  *  when http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11893 is fixed.
00204  */
00205 
00206 #ifdef __GNUC__
00207 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
00208   typeof(&class::func) name
00209 #else
00210 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
00211   ret (NS_STDCALL class::*name) args
00212 #endif
00213 
00214 /**
00215  * Generic API modifiers which return the standard XPCOM nsresult type
00216  */
00217 #define NS_IMETHOD          NS_IMETHOD_(nsresult)
00218 #define NS_IMETHODIMP       NS_IMETHODIMP_(nsresult)
00219 #define NS_METHOD           NS_METHOD_(nsresult)
00220 #define NS_CALLBACK(_name)  NS_CALLBACK_(nsresult, _name)
00221 
00222 /**
00223  * Import/Export macros for XPCOM APIs
00224  */
00225 
00226 #ifdef _IMPL_NS_COM
00227 #define NS_COM NS_EXPORT
00228 #elif  defined(_IMPL_NS_COM_OFF)
00229 #define NS_COM
00230 #elif  defined(XPCOM_GLUE)
00231 #define NS_COM
00232 #else
00233 #define NS_COM NS_IMPORT
00234 #endif
00235 
00236 #ifdef MOZILLA_INTERNAL_API
00237 #  define NS_COM_GLUE NS_COM
00238    /*
00239      The frozen string API has different definitions of nsAC?String
00240      classes than the internal API. On systems that explicitly declare
00241      dllexport symbols this is not a problem, but on ELF systems
00242      internal symbols can accidentally "shine through"; we rename the
00243      internal classes to avoid symbol conflicts.
00244    */
00245 #  define nsAString nsAString_internal
00246 #  define nsACString nsACString_internal
00247 #else
00248 #  define NS_COM_GLUE
00249 #endif
00250 
00251 
00252 /**
00253  * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
00254  * xpidl can determine that the interface can't contain a constructor.
00255  * This results in some space savings and possible runtime savings -
00256  * see bug 49416.  We undefine it first, as xpidl-generated headers
00257  * define it for IDL uses that don't include this file.
00258  */
00259 #ifdef NS_NO_VTABLE
00260 #undef NS_NO_VTABLE
00261 #endif
00262 #if defined(_MSC_VER) && _MSC_VER >= 1100
00263 #define NS_NO_VTABLE __declspec(novtable)
00264 #else
00265 #define NS_NO_VTABLE
00266 #endif
00267 
00268 
00269 /**
00270  * Generic XPCOM result data type
00271  */
00272 typedef PRUint32 nsresult;
00273 
00274 /**
00275  * The preferred symbol for null.
00276  */
00277 #define nsnull 0
00278 
00279 #include "nsError.h"
00280 
00281 /* ------------------------------------------------------------------------ */
00282 /* Casting macros for hiding C++ features from older compilers */
00283 
00284   /*
00285     All our compiler support template specialization, but not all support the
00286     |template <>| notation.  The compiler that don't understand this notation
00287     just omit it for specialization.
00288 
00289     Need to add an autoconf test for this.
00290   */
00291 
00292   /* under Metrowerks (Mac), we don't have autoconf yet */
00293 #ifdef __MWERKS__
00294   #define HAVE_CPP_PARTIAL_SPECIALIZATION
00295   #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
00296 
00297   #define HAVE_CPP_ACCESS_CHANGING_USING
00298   #define HAVE_CPP_AMBIGUITY_RESOLVING_USING
00299   #define HAVE_CPP_EXPLICIT
00300   #define HAVE_CPP_TYPENAME
00301   #define HAVE_CPP_BOOL
00302   #define HAVE_CPP_NAMESPACE_STD
00303   #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
00304   #define HAVE_CPP_2BYTE_WCHAR_T
00305 #endif
00306 
00307   /* under VC++ (Windows), we don't have autoconf yet */
00308 #if defined(_MSC_VER) && (_MSC_VER>=1100)
00309   /* VC++ 5.0 and greater implement template specialization, 4.2 is unknown */
00310   #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
00311 
00312   #define HAVE_CPP_EXPLICIT
00313   #define HAVE_CPP_TYPENAME
00314   #define HAVE_CPP_ACCESS_CHANGING_USING
00315 
00316   #if (_MSC_VER==1100)
00317       /* VC++5.0 has an internal compiler error (sometimes) without this */
00318     #undef HAVE_CPP_ACCESS_CHANGING_USING
00319   #endif
00320 
00321   #define HAVE_CPP_NAMESPACE_STD
00322   #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
00323   #define HAVE_CPP_2BYTE_WCHAR_T
00324 #endif
00325 
00326 #ifndef __PRUNICHAR__
00327 #define __PRUNICHAR__
00328   /* For now, don't use wchar_t on Unix because it breaks the Netscape
00329    * commercial build.  When this is fixed there will be no need for the
00330    * |NS_REINTERPRET_CAST| in nsLiteralString.h either.
00331    */
00332   #if defined(HAVE_CPP_2BYTE_WCHAR_T) && defined(NS_WIN32)
00333     typedef wchar_t PRUnichar;
00334   #else
00335     typedef PRUint16 PRUnichar;
00336   #endif
00337 #endif
00338 
00339   /*
00340     If the compiler doesn't support |explicit|, we'll just make it go away, trusting
00341     that the builds under compilers that do have it will keep us on the straight and narrow.
00342   */
00343 #ifndef HAVE_CPP_EXPLICIT
00344   #define explicit
00345 #endif
00346 
00347 #ifndef HAVE_CPP_TYPENAME
00348   #define typename
00349 #endif
00350 
00351 #ifdef HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
00352   #define NS_SPECIALIZE_TEMPLATE  template <>
00353 #else
00354   #define NS_SPECIALIZE_TEMPLATE
00355 #endif
00356 
00357 /* unix and beos now determine this automatically */
00358 #if ! defined XP_UNIX && ! defined XP_BEOS && !defined(XP_OS2)
00359 #ifndef HAVE_CPP_NEW_CASTS
00360 #define HAVE_CPP_NEW_CASTS 1 /* we'll be optimistic. */
00361 #endif
00362 #endif
00363 
00364 #if defined(HAVE_CPP_NEW_CASTS)
00365 #define NS_STATIC_CAST(__type, __ptr)      static_cast< __type >(__ptr)
00366 #define NS_CONST_CAST(__type, __ptr)       const_cast< __type >(__ptr)
00367 
00368 #define NS_REINTERPRET_POINTER_CAST(__type, __ptr)    reinterpret_cast< __type >(__ptr)
00369 #define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) reinterpret_cast< __type >(__obj)
00370 #define NS_REINTERPRET_CAST(__type, __expr)           reinterpret_cast< __type >(__expr)
00371 
00372 #else
00373 #define NS_STATIC_CAST(__type, __ptr)      ((__type)(__ptr))
00374 #define NS_CONST_CAST(__type, __ptr)       ((__type)(__ptr))
00375 
00376 #define NS_REINTERPRET_POINTER_CAST(__type, __ptr)     ((__type)((void*)(__ptr)))
00377 #define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj)  ((__type)(__obj))
00378 
00379   /* Note: the following is only appropriate for pointers. */
00380 #define NS_REINTERPRET_CAST(__type, __expr)            NS_REINTERPRET_POINTER_CAST(__type, __expr)
00381   /*
00382     Why cast to a |void*| first?  Well, when old-style casting from
00383     a pointer to a base to a pointer to a derived class, the cast will be
00384     ambiguous if the source pointer type appears multiple times in the
00385     destination, e.g.,
00386     
00387       class Base {};
00388       class Derived : public Base, public Base {};
00389       
00390       void foo( Base* b )
00391         {
00392           ((Derived*)b)->some_derived_member ... // Error: Ambiguous, expand from which |Base|?
00393         }
00394 
00395     an old-style cast (like |static_cast|) will change the pointer, but
00396     here, doesn't know how.  The cast to |void*| prevents it from thinking
00397     it needs to expand the original pointer.
00398 
00399     The cost is, |NS_REINTERPRET_CAST| is no longer appropriate for non-pointer
00400     conversions.  Also, mis-applying |NS_REINTERPRET_CAST| to cast |this| to something
00401     will still expand the pointer to the outer object in standards complying compilers.
00402   */
00403 
00404   /*
00405     No sense in making an NS_DYNAMIC_CAST() macro: you can't duplicate
00406     the semantics. So if you want to dynamic_cast, then just use it
00407     "straight", no macro.
00408   */
00409 #endif
00410  
00411 /* 
00412  * Use these macros to do 64bit safe pointer conversions.
00413  */
00414 
00415 #define NS_PTR_TO_INT32(x)  ((PRInt32)  (PRWord) (x))
00416 #define NS_PTR_TO_UINT32(x) ((PRUint32) (PRWord) (x))
00417 #define NS_INT32_TO_PTR(x)  ((void *)   (PRWord) (x))
00418 
00419 /*
00420  * Use NS_STRINGIFY to form a string literal from the value of a macro.
00421  */
00422 #define NS_STRINGIFY_HELPER(x_) #x_
00423 #define NS_STRINGIFY(x_) NS_STRINGIFY_HELPER(x_)
00424 
00425 /*
00426  * These macros allow you to give a hint to the compiler about branch
00427  * probability so that it can better optimize.  Use them like this:
00428  *
00429  *  if (NS_LIKELY(v == 1)) {
00430  *    ... expected code path ...
00431  *  }
00432  *
00433  *  if (NS_UNLIKELY(v == 0)) {
00434  *    ... non-expected code path ...
00435  *  }
00436  *
00437  */
00438 
00439 #if defined(__GNUC__) && (__GNUC__ > 2)
00440 #define NS_LIKELY(x)    (__builtin_expect((x), 1))
00441 #define NS_UNLIKELY(x)  (__builtin_expect((x), 0))
00442 #else
00443 #define NS_LIKELY(x)    (x)
00444 #define NS_UNLIKELY(x)  (x)
00445 #endif
00446 
00447 #endif /* nscore_h___ */
Generated by  doxygen 1.6.2-20100208