webdriver: Implement Fullscreen command support (#100)
[gecko.git] / mfbt / ResultExtensions.h
blobb47509b6a32a9f30dee0c4ce200099ecf1551da8
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"
13 #include "nscore.h"
14 #include "prtypes.h"
16 namespace mozilla {
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>
23 nsresult mErrorValue;
25 template<typename V, typename E2> friend class Result;
27 public:
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"
43 namespace mozilla {
45 inline Result<Ok, nsresult>
46 ToResult(nsresult aValue)
48 if (NS_FAILED(aValue)) {
49 return Err(aValue);
51 return Ok();
54 inline Result<Ok, nsresult>
55 ToResult(PRStatus aValue)
57 if (aValue == PR_SUCCESS) {
58 return Ok();
60 return Err(NS_ERROR_FAILURE);
63 } // namespace mozilla
65 #endif // mozilla_ResultExtensions_h