summaryrefslogtreecommitdiffstats
path: root/xpcom/base/nsError.h
blob: 040cd4612c2d73e30daa23f14fe41b2a9f92055a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsError_h__
#define nsError_h__

#ifndef __cplusplus
#  error nsError.h no longer supports C sources
#endif

#include "mozilla/Attributes.h"
#include "mozilla/Likely.h"

#include <stdint.h>

#define NS_ERROR_SEVERITY_SUCCESS 0
#define NS_ERROR_SEVERITY_ERROR 1

#include "ErrorList.h"  // IWYU pragma: export

/**
 * @name Standard Error Handling Macros
 * @return 0 or 1 (false/true with bool type for C++)
 */

inline uint32_t NS_FAILED_impl(nsresult aErr) {
  return static_cast<uint32_t>(aErr) & 0x80000000;
}
#define NS_FAILED(_nsresult) ((bool)MOZ_UNLIKELY(NS_FAILED_impl(_nsresult)))
#define NS_SUCCEEDED(_nsresult) ((bool)MOZ_LIKELY(!NS_FAILED_impl(_nsresult)))

/* Check that our enum type is actually uint32_t as expected */
static_assert(((nsresult)0) < ((nsresult)-1),
              "nsresult must be an unsigned type");
static_assert(sizeof(nsresult) == sizeof(uint32_t), "nsresult must be 32 bits");

#define MOZ_ALWAYS_SUCCEEDS(expr) MOZ_ALWAYS_TRUE(NS_SUCCEEDED(expr))

/**
 * @name Standard Error Generating Macros
 */

#define NS_ERROR_GENERATE(sev, module, code)                            \
  (nsresult)(((uint32_t)(sev) << 31) |                                  \
             ((uint32_t)(module + NS_ERROR_MODULE_BASE_OFFSET) << 16) | \
             ((uint32_t)(code)))

#define NS_ERROR_GENERATE_SUCCESS(module, code) \
  NS_ERROR_GENERATE(NS_ERROR_SEVERITY_SUCCESS, module, code)

#define NS_ERROR_GENERATE_FAILURE(module, code) \
  NS_ERROR_GENERATE(NS_ERROR_SEVERITY_ERROR, module, code)

/*
 * This will return the nsresult corresponding to the most recent NSPR failure
 * returned by PR_GetError.
 *
 ***********************************************************************
 *      Do not depend on this function. It will be going away!
 ***********************************************************************
 */
extern nsresult NS_ErrorAccordingToNSPR();

/**
 * @name Standard Macros for retrieving error bits
 */

inline constexpr uint16_t NS_ERROR_GET_CODE(nsresult aErr) {
  return uint32_t(aErr) & 0xffff;
}
inline constexpr uint16_t NS_ERROR_GET_MODULE(nsresult aErr) {
  return ((uint32_t(aErr) >> 16) - NS_ERROR_MODULE_BASE_OFFSET) & 0x1fff;
}
inline bool NS_ERROR_GET_SEVERITY(nsresult aErr) {
  return uint32_t(aErr) >> 31;
}

#ifdef _MSC_VER
#  pragma warning(disable : 4251) /* 'nsCOMPtr<class nsIInputStream>' needs to \
                                     have dll-interface to be used by clients  \
                                     of class 'nsInputStream' */
#  pragma warning(                                                          \
      disable : 4275) /* non dll-interface class 'nsISupports' used as base \
                         for dll-interface class 'nsIRDFNode' */
#endif

#endif