xpcom/base/nsError.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/base/nsError.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,250 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsError_h__
    1.10 +#define nsError_h__
    1.11 +
    1.12 +#include "mozilla/Likely.h"
    1.13 +#include "mozilla/TypedEnum.h"
    1.14 +
    1.15 +#include <stdint.h>
    1.16 +
    1.17 +/*
    1.18 + * To add error code to your module, you need to do the following:
    1.19 + *
    1.20 + * 1) Add a module offset code.  Add yours to the bottom of the list
    1.21 + *    right below this comment, adding 1.
    1.22 + *
    1.23 + * 2) In your module, define a header file which uses one of the
    1.24 + *    NE_ERROR_GENERATExxxxxx macros.  Some examples below:
    1.25 + *
    1.26 + *    #define NS_ERROR_MYMODULE_MYERROR1 NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR,NS_ERROR_MODULE_MYMODULE,1)
    1.27 + *    #define NS_ERROR_MYMODULE_MYERROR2 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_MYMODULE,2)
    1.28 + *    #define NS_ERROR_MYMODULE_MYERROR3 NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_MYMODULE,3)
    1.29 + *
    1.30 + */
    1.31 +
    1.32 +
    1.33 +/**
    1.34 + * @name Standard Module Offset Code. Each Module should identify a unique number
    1.35 + *       and then all errors associated with that module become offsets from the
    1.36 + *       base associated with that module id. There are 16 bits of code bits for
    1.37 + *       each module.
    1.38 + */
    1.39 +
    1.40 +#define NS_ERROR_MODULE_XPCOM      1
    1.41 +#define NS_ERROR_MODULE_BASE       2
    1.42 +#define NS_ERROR_MODULE_GFX        3
    1.43 +#define NS_ERROR_MODULE_WIDGET     4
    1.44 +#define NS_ERROR_MODULE_CALENDAR   5
    1.45 +#define NS_ERROR_MODULE_NETWORK    6
    1.46 +#define NS_ERROR_MODULE_PLUGINS    7
    1.47 +#define NS_ERROR_MODULE_LAYOUT     8
    1.48 +#define NS_ERROR_MODULE_HTMLPARSER 9
    1.49 +#define NS_ERROR_MODULE_RDF        10
    1.50 +#define NS_ERROR_MODULE_UCONV      11
    1.51 +#define NS_ERROR_MODULE_REG        12
    1.52 +#define NS_ERROR_MODULE_FILES      13
    1.53 +#define NS_ERROR_MODULE_DOM        14
    1.54 +#define NS_ERROR_MODULE_IMGLIB     15
    1.55 +#define NS_ERROR_MODULE_MAILNEWS   16
    1.56 +#define NS_ERROR_MODULE_EDITOR     17
    1.57 +#define NS_ERROR_MODULE_XPCONNECT  18
    1.58 +#define NS_ERROR_MODULE_PROFILE    19
    1.59 +#define NS_ERROR_MODULE_LDAP       20
    1.60 +#define NS_ERROR_MODULE_SECURITY   21
    1.61 +#define NS_ERROR_MODULE_DOM_XPATH  22
    1.62 +/* 23 used to be NS_ERROR_MODULE_DOM_RANGE (see bug 711047) */
    1.63 +#define NS_ERROR_MODULE_URILOADER  24
    1.64 +#define NS_ERROR_MODULE_CONTENT    25
    1.65 +#define NS_ERROR_MODULE_PYXPCOM    26
    1.66 +#define NS_ERROR_MODULE_XSLT       27
    1.67 +#define NS_ERROR_MODULE_IPC        28
    1.68 +#define NS_ERROR_MODULE_SVG        29
    1.69 +#define NS_ERROR_MODULE_STORAGE    30
    1.70 +#define NS_ERROR_MODULE_SCHEMA     31
    1.71 +#define NS_ERROR_MODULE_DOM_FILE   32
    1.72 +#define NS_ERROR_MODULE_DOM_INDEXEDDB 33
    1.73 +#define NS_ERROR_MODULE_DOM_FILEHANDLE 34
    1.74 +#define NS_ERROR_MODULE_SIGNED_JAR 35
    1.75 +#define NS_ERROR_MODULE_DOM_FILESYSTEM 36
    1.76 +
    1.77 +/* NS_ERROR_MODULE_GENERAL should be used by modules that do not
    1.78 + * care if return code values overlap. Callers of methods that
    1.79 + * return such codes should be aware that they are not
    1.80 + * globally unique. Implementors should be careful about blindly
    1.81 + * returning codes from other modules that might also use
    1.82 + * the generic base.
    1.83 + */
    1.84 +#define NS_ERROR_MODULE_GENERAL    51
    1.85 +
    1.86 +/**
    1.87 + * @name Severity Code.  This flag identifies the level of warning
    1.88 + */
    1.89 +
    1.90 +#define NS_ERROR_SEVERITY_SUCCESS       0
    1.91 +#define NS_ERROR_SEVERITY_ERROR         1
    1.92 +
    1.93 +/**
    1.94 + * @name Mozilla Code.  This flag separates consumers of mozilla code
    1.95 + *       from the native platform
    1.96 + */
    1.97 +
    1.98 +#define NS_ERROR_MODULE_BASE_OFFSET 0x45
    1.99 +
   1.100 +/* Helpers for defining our enum, to be undef'd later */
   1.101 +#define SUCCESS_OR_FAILURE(sev, module, code) \
   1.102 +  ((uint32_t)(sev) << 31) | \
   1.103 +  ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \
   1.104 +  (uint32_t)(code)
   1.105 +#define SUCCESS(code) \
   1.106 +  SUCCESS_OR_FAILURE(NS_ERROR_SEVERITY_SUCCESS, MODULE, code)
   1.107 +#define FAILURE(code) \
   1.108 +  SUCCESS_OR_FAILURE(NS_ERROR_SEVERITY_ERROR, MODULE, code)
   1.109 +
   1.110 +/**
   1.111 + * @name Standard return values
   1.112 + */
   1.113 +
   1.114 +/*@{*/
   1.115 +
   1.116 +/* Unfortunately, our workaround for compilers that don't support enum class
   1.117 + * doesn't really work for nsresult.  We need constants like NS_OK with type
   1.118 + * nsresult, but they can't be used in (e.g.) switch cases if they're objects.
   1.119 + * But if we define them to be of type nsresult::Enum instead, that causes
   1.120 + *   return foo ? F() : NS_ERROR_FAILURE;
   1.121 + * to fail, because nsresult and nsresult::Enum are two distinct types and
   1.122 + * either can be converted to the other, so it's ambiguous.  So we have to fall
   1.123 + * back to a regular enum.
   1.124 + */
   1.125 +#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
   1.126 +  typedef enum class tag_nsresult : uint32_t
   1.127 +  {
   1.128 +    #undef ERROR
   1.129 +    #define ERROR(key, val) key = val
   1.130 +    #include "ErrorList.h"
   1.131 +    #undef ERROR
   1.132 +  } nsresult;
   1.133 +
   1.134 +  /*
   1.135 +   * enum classes don't place their initializers in the global scope, so we need
   1.136 +   * #define's for compatibility with old code.
   1.137 +   */
   1.138 +  #include "ErrorListCxxDefines.h"
   1.139 +#elif defined(MOZ_HAVE_CXX11_ENUM_TYPE)
   1.140 +  typedef enum tag_nsresult : uint32_t
   1.141 +  {
   1.142 +    #undef ERROR
   1.143 +    #define ERROR(key, val) key = val
   1.144 +    #include "ErrorList.h"
   1.145 +    #undef ERROR
   1.146 +  } nsresult;
   1.147 +#elif defined(__cplusplus)
   1.148 +  /*
   1.149 +   * We're C++ in an old compiler lacking enum classes *and* typed enums (likely
   1.150 +   * gcc < 4.5.1 as clang/MSVC have long supported one or both), or compiler
   1.151 +   * support is unknown.  Yet nsresult must have unsigned 32-bit representation.
   1.152 +   * So just make it a typedef, and implement the constants with global consts.
   1.153 +   */
   1.154 +  typedef uint32_t nsresult;
   1.155 +
   1.156 +  const nsresult
   1.157 +  #undef ERROR
   1.158 +  #define ERROR(key, val) key = val
   1.159 +  #include "ErrorList.h"
   1.160 +  #undef ERROR
   1.161 +    ;
   1.162 +#else
   1.163 +  /*
   1.164 +   * C doesn't have any way to fix the type underlying an enum, and enum
   1.165 +   * initializers can't have values outside the range of 'int'.  So typedef
   1.166 +   * nsresult to the correct unsigned type, and fall back to using #defines for
   1.167 +   * all error constants.
   1.168 +   */
   1.169 +  typedef uint32_t nsresult;
   1.170 +  #include "ErrorListCDefines.h"
   1.171 +#endif
   1.172 +
   1.173 +#undef SUCCESS_OR_FAILURE
   1.174 +#undef SUCCESS
   1.175 +#undef FAILURE
   1.176 +
   1.177 +/**
   1.178 + * @name Standard Error Handling Macros
   1.179 + * @return 0 or 1 (false/true with bool type for C++)
   1.180 + */
   1.181 +
   1.182 +#ifdef __cplusplus
   1.183 +inline uint32_t NS_FAILED_impl(nsresult _nsresult) {
   1.184 +  return static_cast<uint32_t>(_nsresult) & 0x80000000;
   1.185 +}
   1.186 +#define NS_FAILED(_nsresult)    ((bool)MOZ_UNLIKELY(NS_FAILED_impl(_nsresult)))
   1.187 +#define NS_SUCCEEDED(_nsresult) ((bool)MOZ_LIKELY(!NS_FAILED_impl(_nsresult)))
   1.188 +
   1.189 +/* Check that our enum type is actually uint32_t as expected */
   1.190 +static_assert(((nsresult)0) < ((nsresult)-1),
   1.191 +              "nsresult must be an unsigned type");
   1.192 +static_assert(sizeof(nsresult) == sizeof(uint32_t),
   1.193 +              "nsresult must be 32 bits");
   1.194 +#else
   1.195 +#define NS_FAILED_impl(_nsresult) ((_nsresult) & 0x80000000)
   1.196 +#define NS_FAILED(_nsresult)    (MOZ_UNLIKELY(NS_FAILED_impl(_nsresult)))
   1.197 +#define NS_SUCCEEDED(_nsresult) (MOZ_LIKELY(!NS_FAILED_impl(_nsresult)))
   1.198 +#endif
   1.199 +
   1.200 +/**
   1.201 + * @name Standard Error Generating Macros
   1.202 + */
   1.203 +
   1.204 +#define NS_ERROR_GENERATE(sev, module, code) \
   1.205 +    (nsresult)(((uint32_t)(sev) << 31) | \
   1.206 +               ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \
   1.207 +               ((uint32_t)(code)))
   1.208 +
   1.209 +#define NS_ERROR_GENERATE_SUCCESS(module, code) \
   1.210 +  NS_ERROR_GENERATE(NS_ERROR_SEVERITY_SUCCESS, module, code)
   1.211 +
   1.212 +#define NS_ERROR_GENERATE_FAILURE(module, code) \
   1.213 +  NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR, module, code)
   1.214 +
   1.215 + /*
   1.216 +  * This will return the nsresult corresponding to the most recent NSPR failure
   1.217 +  * returned by PR_GetError.
   1.218 +  *
   1.219 +  ***********************************************************************
   1.220 +  *      Do not depend on this function. It will be going away!
   1.221 +  ***********************************************************************
   1.222 +  */
   1.223 +extern nsresult
   1.224 +NS_ErrorAccordingToNSPR();
   1.225 +
   1.226 +
   1.227 +/**
   1.228 + * @name Standard Macros for retrieving error bits
   1.229 + */
   1.230 +
   1.231 +#ifdef __cplusplus
   1.232 +inline uint16_t NS_ERROR_GET_CODE(nsresult err) {
   1.233 +  return uint32_t(err) & 0xffff;
   1.234 +}
   1.235 +inline uint16_t NS_ERROR_GET_MODULE(nsresult err) {
   1.236 +  return ((uint32_t(err) >> 16) - NS_ERROR_MODULE_BASE_OFFSET) & 0x1fff;
   1.237 +}
   1.238 +inline bool NS_ERROR_GET_SEVERITY(nsresult err) {
   1.239 +  return uint32_t(err) >> 31;
   1.240 +}
   1.241 +#else
   1.242 +#define NS_ERROR_GET_CODE(err)     ((err) & 0xffff)
   1.243 +#define NS_ERROR_GET_MODULE(err)   ((((err) >> 16) - NS_ERROR_MODULE_BASE_OFFSET) & 0x1fff)
   1.244 +#define NS_ERROR_GET_SEVERITY(err) (((err) >> 31) & 0x1)
   1.245 +#endif
   1.246 +
   1.247 +
   1.248 +#ifdef _MSC_VER
   1.249 +#pragma warning(disable: 4251) /* 'nsCOMPtr<class nsIInputStream>' needs to have dll-interface to be used by clients of class 'nsInputStream' */
   1.250 +#pragma warning(disable: 4275) /* non dll-interface class 'nsISupports' used as base for dll-interface class 'nsIRDFNode' */
   1.251 +#endif
   1.252 +
   1.253 +#endif

mercurial