Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/ResultExtensions.h
Line
Count
Source (jump to first uncovered line)
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
/* Extensions to the Result type to enable simpler handling of XPCOM/NSPR results. */
8
9
#ifndef mozilla_ResultExtensions_h
10
#define mozilla_ResultExtensions_h
11
12
#include "mozilla/Assertions.h"
13
#include "nscore.h"
14
#include "prtypes.h"
15
16
namespace mozilla {
17
18
// Allow nsresult errors to automatically convert to nsresult values, so MOZ_TRY
19
// can be used in XPCOM methods with Result<T, nserror> results.
20
template <>
21
class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>
22
{
23
  nsresult mErrorValue;
24
25
  template<typename V, typename E2> friend class Result;
26
27
public:
28
  explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue)
29
9
  {
30
9
    MOZ_ASSERT(NS_FAILED(aErrorValue));
31
9
  }
32
33
0
  operator nsresult() { return mErrorValue; }
34
};
35
36
// Allow MOZ_TRY to handle `PRStatus` values.
37
inline Result<Ok, nsresult> ToResult(PRStatus aValue);
38
39
} // namespace mozilla
40
41
#include "mozilla/Result.h"
42
43
namespace mozilla {
44
45
inline Result<Ok, nsresult>
46
ToResult(nsresult aValue)
47
190
{
48
190
  if (NS_FAILED(aValue)) {
49
0
    return Err(aValue);
50
0
  }
51
190
  return Ok();
52
190
}
53
54
inline Result<Ok, nsresult>
55
ToResult(PRStatus aValue)
56
0
{
57
0
  if (aValue == PR_SUCCESS) {
58
0
    return Ok();
59
0
  }
60
0
  return Err(NS_ERROR_FAILURE);
61
0
}
62
63
} // namespace mozilla
64
65
#endif // mozilla_ResultExtensions_h