|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsError_h__ |
|
7 #define nsError_h__ |
|
8 |
|
9 #include "mozilla/Likely.h" |
|
10 #include "mozilla/TypedEnum.h" |
|
11 |
|
12 #include <stdint.h> |
|
13 |
|
14 /* |
|
15 * To add error code to your module, you need to do the following: |
|
16 * |
|
17 * 1) Add a module offset code. Add yours to the bottom of the list |
|
18 * right below this comment, adding 1. |
|
19 * |
|
20 * 2) In your module, define a header file which uses one of the |
|
21 * NE_ERROR_GENERATExxxxxx macros. Some examples below: |
|
22 * |
|
23 * #define NS_ERROR_MYMODULE_MYERROR1 NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR,NS_ERROR_MODULE_MYMODULE,1) |
|
24 * #define NS_ERROR_MYMODULE_MYERROR2 NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_MYMODULE,2) |
|
25 * #define NS_ERROR_MYMODULE_MYERROR3 NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_MYMODULE,3) |
|
26 * |
|
27 */ |
|
28 |
|
29 |
|
30 /** |
|
31 * @name Standard Module Offset Code. Each Module should identify a unique number |
|
32 * and then all errors associated with that module become offsets from the |
|
33 * base associated with that module id. There are 16 bits of code bits for |
|
34 * each module. |
|
35 */ |
|
36 |
|
37 #define NS_ERROR_MODULE_XPCOM 1 |
|
38 #define NS_ERROR_MODULE_BASE 2 |
|
39 #define NS_ERROR_MODULE_GFX 3 |
|
40 #define NS_ERROR_MODULE_WIDGET 4 |
|
41 #define NS_ERROR_MODULE_CALENDAR 5 |
|
42 #define NS_ERROR_MODULE_NETWORK 6 |
|
43 #define NS_ERROR_MODULE_PLUGINS 7 |
|
44 #define NS_ERROR_MODULE_LAYOUT 8 |
|
45 #define NS_ERROR_MODULE_HTMLPARSER 9 |
|
46 #define NS_ERROR_MODULE_RDF 10 |
|
47 #define NS_ERROR_MODULE_UCONV 11 |
|
48 #define NS_ERROR_MODULE_REG 12 |
|
49 #define NS_ERROR_MODULE_FILES 13 |
|
50 #define NS_ERROR_MODULE_DOM 14 |
|
51 #define NS_ERROR_MODULE_IMGLIB 15 |
|
52 #define NS_ERROR_MODULE_MAILNEWS 16 |
|
53 #define NS_ERROR_MODULE_EDITOR 17 |
|
54 #define NS_ERROR_MODULE_XPCONNECT 18 |
|
55 #define NS_ERROR_MODULE_PROFILE 19 |
|
56 #define NS_ERROR_MODULE_LDAP 20 |
|
57 #define NS_ERROR_MODULE_SECURITY 21 |
|
58 #define NS_ERROR_MODULE_DOM_XPATH 22 |
|
59 /* 23 used to be NS_ERROR_MODULE_DOM_RANGE (see bug 711047) */ |
|
60 #define NS_ERROR_MODULE_URILOADER 24 |
|
61 #define NS_ERROR_MODULE_CONTENT 25 |
|
62 #define NS_ERROR_MODULE_PYXPCOM 26 |
|
63 #define NS_ERROR_MODULE_XSLT 27 |
|
64 #define NS_ERROR_MODULE_IPC 28 |
|
65 #define NS_ERROR_MODULE_SVG 29 |
|
66 #define NS_ERROR_MODULE_STORAGE 30 |
|
67 #define NS_ERROR_MODULE_SCHEMA 31 |
|
68 #define NS_ERROR_MODULE_DOM_FILE 32 |
|
69 #define NS_ERROR_MODULE_DOM_INDEXEDDB 33 |
|
70 #define NS_ERROR_MODULE_DOM_FILEHANDLE 34 |
|
71 #define NS_ERROR_MODULE_SIGNED_JAR 35 |
|
72 #define NS_ERROR_MODULE_DOM_FILESYSTEM 36 |
|
73 |
|
74 /* NS_ERROR_MODULE_GENERAL should be used by modules that do not |
|
75 * care if return code values overlap. Callers of methods that |
|
76 * return such codes should be aware that they are not |
|
77 * globally unique. Implementors should be careful about blindly |
|
78 * returning codes from other modules that might also use |
|
79 * the generic base. |
|
80 */ |
|
81 #define NS_ERROR_MODULE_GENERAL 51 |
|
82 |
|
83 /** |
|
84 * @name Severity Code. This flag identifies the level of warning |
|
85 */ |
|
86 |
|
87 #define NS_ERROR_SEVERITY_SUCCESS 0 |
|
88 #define NS_ERROR_SEVERITY_ERROR 1 |
|
89 |
|
90 /** |
|
91 * @name Mozilla Code. This flag separates consumers of mozilla code |
|
92 * from the native platform |
|
93 */ |
|
94 |
|
95 #define NS_ERROR_MODULE_BASE_OFFSET 0x45 |
|
96 |
|
97 /* Helpers for defining our enum, to be undef'd later */ |
|
98 #define SUCCESS_OR_FAILURE(sev, module, code) \ |
|
99 ((uint32_t)(sev) << 31) | \ |
|
100 ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \ |
|
101 (uint32_t)(code) |
|
102 #define SUCCESS(code) \ |
|
103 SUCCESS_OR_FAILURE(NS_ERROR_SEVERITY_SUCCESS, MODULE, code) |
|
104 #define FAILURE(code) \ |
|
105 SUCCESS_OR_FAILURE(NS_ERROR_SEVERITY_ERROR, MODULE, code) |
|
106 |
|
107 /** |
|
108 * @name Standard return values |
|
109 */ |
|
110 |
|
111 /*@{*/ |
|
112 |
|
113 /* Unfortunately, our workaround for compilers that don't support enum class |
|
114 * doesn't really work for nsresult. We need constants like NS_OK with type |
|
115 * nsresult, but they can't be used in (e.g.) switch cases if they're objects. |
|
116 * But if we define them to be of type nsresult::Enum instead, that causes |
|
117 * return foo ? F() : NS_ERROR_FAILURE; |
|
118 * to fail, because nsresult and nsresult::Enum are two distinct types and |
|
119 * either can be converted to the other, so it's ambiguous. So we have to fall |
|
120 * back to a regular enum. |
|
121 */ |
|
122 #if defined(MOZ_HAVE_CXX11_STRONG_ENUMS) |
|
123 typedef enum class tag_nsresult : uint32_t |
|
124 { |
|
125 #undef ERROR |
|
126 #define ERROR(key, val) key = val |
|
127 #include "ErrorList.h" |
|
128 #undef ERROR |
|
129 } nsresult; |
|
130 |
|
131 /* |
|
132 * enum classes don't place their initializers in the global scope, so we need |
|
133 * #define's for compatibility with old code. |
|
134 */ |
|
135 #include "ErrorListCxxDefines.h" |
|
136 #elif defined(MOZ_HAVE_CXX11_ENUM_TYPE) |
|
137 typedef enum tag_nsresult : uint32_t |
|
138 { |
|
139 #undef ERROR |
|
140 #define ERROR(key, val) key = val |
|
141 #include "ErrorList.h" |
|
142 #undef ERROR |
|
143 } nsresult; |
|
144 #elif defined(__cplusplus) |
|
145 /* |
|
146 * We're C++ in an old compiler lacking enum classes *and* typed enums (likely |
|
147 * gcc < 4.5.1 as clang/MSVC have long supported one or both), or compiler |
|
148 * support is unknown. Yet nsresult must have unsigned 32-bit representation. |
|
149 * So just make it a typedef, and implement the constants with global consts. |
|
150 */ |
|
151 typedef uint32_t nsresult; |
|
152 |
|
153 const nsresult |
|
154 #undef ERROR |
|
155 #define ERROR(key, val) key = val |
|
156 #include "ErrorList.h" |
|
157 #undef ERROR |
|
158 ; |
|
159 #else |
|
160 /* |
|
161 * C doesn't have any way to fix the type underlying an enum, and enum |
|
162 * initializers can't have values outside the range of 'int'. So typedef |
|
163 * nsresult to the correct unsigned type, and fall back to using #defines for |
|
164 * all error constants. |
|
165 */ |
|
166 typedef uint32_t nsresult; |
|
167 #include "ErrorListCDefines.h" |
|
168 #endif |
|
169 |
|
170 #undef SUCCESS_OR_FAILURE |
|
171 #undef SUCCESS |
|
172 #undef FAILURE |
|
173 |
|
174 /** |
|
175 * @name Standard Error Handling Macros |
|
176 * @return 0 or 1 (false/true with bool type for C++) |
|
177 */ |
|
178 |
|
179 #ifdef __cplusplus |
|
180 inline uint32_t NS_FAILED_impl(nsresult _nsresult) { |
|
181 return static_cast<uint32_t>(_nsresult) & 0x80000000; |
|
182 } |
|
183 #define NS_FAILED(_nsresult) ((bool)MOZ_UNLIKELY(NS_FAILED_impl(_nsresult))) |
|
184 #define NS_SUCCEEDED(_nsresult) ((bool)MOZ_LIKELY(!NS_FAILED_impl(_nsresult))) |
|
185 |
|
186 /* Check that our enum type is actually uint32_t as expected */ |
|
187 static_assert(((nsresult)0) < ((nsresult)-1), |
|
188 "nsresult must be an unsigned type"); |
|
189 static_assert(sizeof(nsresult) == sizeof(uint32_t), |
|
190 "nsresult must be 32 bits"); |
|
191 #else |
|
192 #define NS_FAILED_impl(_nsresult) ((_nsresult) & 0x80000000) |
|
193 #define NS_FAILED(_nsresult) (MOZ_UNLIKELY(NS_FAILED_impl(_nsresult))) |
|
194 #define NS_SUCCEEDED(_nsresult) (MOZ_LIKELY(!NS_FAILED_impl(_nsresult))) |
|
195 #endif |
|
196 |
|
197 /** |
|
198 * @name Standard Error Generating Macros |
|
199 */ |
|
200 |
|
201 #define NS_ERROR_GENERATE(sev, module, code) \ |
|
202 (nsresult)(((uint32_t)(sev) << 31) | \ |
|
203 ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \ |
|
204 ((uint32_t)(code))) |
|
205 |
|
206 #define NS_ERROR_GENERATE_SUCCESS(module, code) \ |
|
207 NS_ERROR_GENERATE(NS_ERROR_SEVERITY_SUCCESS, module, code) |
|
208 |
|
209 #define NS_ERROR_GENERATE_FAILURE(module, code) \ |
|
210 NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR, module, code) |
|
211 |
|
212 /* |
|
213 * This will return the nsresult corresponding to the most recent NSPR failure |
|
214 * returned by PR_GetError. |
|
215 * |
|
216 *********************************************************************** |
|
217 * Do not depend on this function. It will be going away! |
|
218 *********************************************************************** |
|
219 */ |
|
220 extern nsresult |
|
221 NS_ErrorAccordingToNSPR(); |
|
222 |
|
223 |
|
224 /** |
|
225 * @name Standard Macros for retrieving error bits |
|
226 */ |
|
227 |
|
228 #ifdef __cplusplus |
|
229 inline uint16_t NS_ERROR_GET_CODE(nsresult err) { |
|
230 return uint32_t(err) & 0xffff; |
|
231 } |
|
232 inline uint16_t NS_ERROR_GET_MODULE(nsresult err) { |
|
233 return ((uint32_t(err) >> 16) - NS_ERROR_MODULE_BASE_OFFSET) & 0x1fff; |
|
234 } |
|
235 inline bool NS_ERROR_GET_SEVERITY(nsresult err) { |
|
236 return uint32_t(err) >> 31; |
|
237 } |
|
238 #else |
|
239 #define NS_ERROR_GET_CODE(err) ((err) & 0xffff) |
|
240 #define NS_ERROR_GET_MODULE(err) ((((err) >> 16) - NS_ERROR_MODULE_BASE_OFFSET) & 0x1fff) |
|
241 #define NS_ERROR_GET_SEVERITY(err) (((err) >> 31) & 0x1) |
|
242 #endif |
|
243 |
|
244 |
|
245 #ifdef _MSC_VER |
|
246 #pragma warning(disable: 4251) /* 'nsCOMPtr<class nsIInputStream>' needs to have dll-interface to be used by clients of class 'nsInputStream' */ |
|
247 #pragma warning(disable: 4275) /* non dll-interface class 'nsISupports' used as base for dll-interface class 'nsIRDFNode' */ |
|
248 #endif |
|
249 |
|
250 #endif |