Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 : #ifndef MediaResult_h_
8 : #define MediaResult_h_
9 :
10 : #include "nsString.h" // Required before 'mozilla/ErrorNames.h'!?
11 : #include "mozilla/ErrorNames.h"
12 : #include "nsError.h"
13 : #include "nsPrintfCString.h"
14 :
15 : // MediaResult can be used interchangeably with nsresult.
16 : // It allows to store extra information such as where the error occurred.
17 : // While nsresult is typically passed by value; due to its potential size, using
18 : // MediaResult const references is recommended.
19 : namespace mozilla {
20 :
21 0 : class MediaResult
22 : {
23 : public:
24 0 : MOZ_IMPLICIT MediaResult(nsresult aResult)
25 0 : : mCode(aResult)
26 : {
27 0 : }
28 0 : MediaResult(nsresult aResult, const nsACString& aMessage)
29 0 : : mCode(aResult)
30 0 : , mMessage(aMessage)
31 : {
32 0 : }
33 0 : MediaResult(nsresult aResult, const char* aMessage)
34 0 : : mCode(aResult)
35 0 : , mMessage(aMessage)
36 : {
37 0 : }
38 0 : MediaResult(const MediaResult& aOther) = default;
39 0 : MediaResult(MediaResult&& aOther) = default;
40 : MediaResult& operator=(const MediaResult& aOther) = default;
41 : MediaResult& operator=(MediaResult&& aOther) = default;
42 :
43 0 : nsresult Code() const { return mCode; }
44 0 : const nsCString& Message() const { return mMessage; }
45 :
46 : // Interoperations with nsresult.
47 0 : bool operator==(nsresult aResult) const { return aResult == mCode; }
48 0 : bool operator!=(nsresult aResult) const { return aResult != mCode; }
49 0 : operator nsresult () const { return mCode; }
50 :
51 0 : nsCString Description() const
52 : {
53 0 : if (NS_SUCCEEDED(mCode)) {
54 0 : return nsCString();
55 : }
56 0 : nsCString name;
57 0 : GetErrorName(mCode, static_cast<nsACString&>(name));
58 0 : return nsPrintfCString("%s (0x%08" PRIx32 ")%s%s",
59 : name.get(),
60 0 : static_cast<uint32_t>(mCode),
61 0 : mMessage.IsEmpty() ? "" : " - ",
62 0 : mMessage.get());
63 : }
64 :
65 : private:
66 : nsresult mCode;
67 : nsCString mMessage;
68 : };
69 :
70 : #ifdef _MSC_VER
71 : #define RESULT_DETAIL(arg, ...) nsPrintfCString("%s: " arg, __FUNCSIG__, ##__VA_ARGS__)
72 : #else
73 : #define RESULT_DETAIL(arg, ...) nsPrintfCString("%s: " arg, __PRETTY_FUNCTION__, ##__VA_ARGS__)
74 : #endif
75 :
76 : } // namespace mozilla
77 : #endif // MediaResult_h_
|