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 "mozilla/TimeStamp.h"
13 #include "nsError.h"
14 #include "nsPrintfCString.h"
15 
16 // MediaResult can be used interchangeably with nsresult.
17 // It allows to store extra information such as where the error occurred.
18 // While nsresult is typically passed by value; due to its potential size, using
19 // MediaResult const references is recommended.
20 namespace mozilla {
21 
22 class MediaResult {
23  public:
MediaResult()24   MediaResult() : mCode(NS_OK) {}
MediaResult(nsresult aResult)25   MOZ_IMPLICIT MediaResult(nsresult aResult) : mCode(aResult) {}
MediaResult(nsresult aResult,const nsACString & aMessage)26   MediaResult(nsresult aResult, const nsACString& aMessage)
27       : mCode(aResult), mMessage(aMessage) {}
MediaResult(nsresult aResult,const char * aMessage)28   MediaResult(nsresult aResult, const char* aMessage)
29       : mCode(aResult), mMessage(aMessage) {}
30   MediaResult(const MediaResult& aOther) = default;
31   MediaResult(MediaResult&& aOther) = default;
32   MediaResult& operator=(const MediaResult& aOther) = default;
33   MediaResult& operator=(MediaResult&& aOther) = default;
34 
Code()35   nsresult Code() const { return mCode; }
ErrorName()36   nsCString ErrorName() const {
37     nsCString name;
38     GetErrorName(mCode, name);
39     return name;
40   }
41 
Message()42   const nsCString& Message() const { return mMessage; }
43 
44   // Interoperations with nsresult.
45   bool operator==(nsresult aResult) const { return aResult == mCode; }
46   bool operator!=(nsresult aResult) const { return aResult != mCode; }
nsresult()47   operator nsresult() const { return mCode; }
48 
Description()49   nsCString Description() const {
50     if (NS_SUCCEEDED(mCode)) {
51       return nsCString();
52     }
53     return nsPrintfCString("%s (0x%08" PRIx32 ")%s%s", ErrorName().get(),
54                            static_cast<uint32_t>(mCode),
55                            mMessage.IsEmpty() ? "" : " - ", mMessage.get());
56   }
57 
58  private:
59   nsresult mCode;
60   nsCString mMessage;
61 };
62 
63 #ifdef _MSC_VER
64 #  define RESULT_DETAIL(arg, ...) \
65     nsPrintfCString("%s: " arg, __FUNCSIG__, ##__VA_ARGS__)
66 #else
67 #  define RESULT_DETAIL(arg, ...) \
68     nsPrintfCString("%s: " arg, __PRETTY_FUNCTION__, ##__VA_ARGS__)
69 #endif
70 
71 }  // namespace mozilla
72 #endif  // MediaResult_h_
73