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/. */
7 /* Extensions to the Result type to enable simpler handling of XPCOM/NSPR results. */
9 #ifndef mozilla_ResultExtensions_h
10 #define mozilla_ResultExtensions_h
12 #include "mozilla/Assertions.h"
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.
21 class MOZ_MUST_USE_TYPE GenericErrorResult
<nsresult
>
25 template<typename V
, typename E2
> friend class Result
;
28 explicit GenericErrorResult(nsresult aErrorValue
) : mErrorValue(aErrorValue
)
30 MOZ_ASSERT(NS_FAILED(aErrorValue
));
33 operator nsresult() { return mErrorValue
; }
36 // Allow MOZ_TRY to handle `PRStatus` values.
37 inline Result
<Ok
, nsresult
> ToResult(PRStatus aValue
);
39 } // namespace mozilla
41 #include "mozilla/Result.h"
45 inline Result
<Ok
, nsresult
>
46 ToResult(nsresult aValue
)
48 if (NS_FAILED(aValue
)) {
54 inline Result
<Ok
, nsresult
>
55 ToResult(PRStatus aValue
)
57 if (aValue
== PR_SUCCESS
) {
60 return Err(NS_ERROR_FAILURE
);
63 } // namespace mozilla
65 #endif // mozilla_ResultExtensions_h