js/xpconnect/src/XPCException.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:a3fa46a3752c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /* An implementaion of nsIException. */
8
9 #include "xpcprivate.h"
10 #include "nsError.h"
11
12 /***************************************************************************/
13 /* Quick and dirty mapping of well known result codes to strings. We only
14 * call this when building an exception object, so iterating the short array
15 * is not too bad.
16 *
17 * It sure would be nice to have exceptions declared in idl and available
18 * in some more global way at runtime.
19 */
20
21 static const struct ResultMap
22 {nsresult rv; const char* name; const char* format;} map[] = {
23 #define XPC_MSG_DEF(val, format) \
24 {(val), #val, format},
25 #include "xpc.msg"
26 #undef XPC_MSG_DEF
27 {NS_OK,0,0} // sentinel to mark end of array
28 };
29
30 #define RESULT_COUNT ((sizeof(map) / sizeof(map[0]))-1)
31
32 // static
33 bool
34 nsXPCException::NameAndFormatForNSResult(nsresult rv,
35 const char** name,
36 const char** format)
37 {
38
39 for (const ResultMap* p = map; p->name; p++) {
40 if (rv == p->rv) {
41 if (name) *name = p->name;
42 if (format) *format = p->format;
43 return true;
44 }
45 }
46 return false;
47 }
48
49 // static
50 const void*
51 nsXPCException::IterateNSResults(nsresult* rv,
52 const char** name,
53 const char** format,
54 const void** iterp)
55 {
56 const ResultMap* p = (const ResultMap*) *iterp;
57 if (!p)
58 p = map;
59 else
60 p++;
61 if (!p->name)
62 p = nullptr;
63 else {
64 if (rv)
65 *rv = p->rv;
66 if (name)
67 *name = p->name;
68 if (format)
69 *format = p->format;
70 }
71 *iterp = p;
72 return p;
73 }
74
75 // static
76 uint32_t
77 nsXPCException::GetNSResultCount()
78 {
79 return RESULT_COUNT;
80 }

mercurial