dom/media/MediaResult.h
author Jean-Yves Avenard <jyavenard@mozilla.com>
Tue, 20 Sep 2016 15:50:31 +1000
changeset 415337 aca6e11fd255c3f29f09cfd4784dced3f6d9b66c
parent 414631 e628ce8270d75532eea7173b14bfa99e6a3ff81c
child 450653 711ae2e164b9b20bf23265786171dc419a851bc7
permissions -rw-r--r--
Bug 1303673: P1. Add RESULT_DETAIL convenience macro. r?cpearce MozReview-Commit-ID: 12YaMnNsvNI

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MediaResult_h_
#define MediaResult_h_

#include "nsError.h"
#include "nsPrintfCString.h"

// MediaResult can be used interchangeably with nsresult.
// It allows to store extra information such as where the error occurred.
// While nsresult is typically passed by value; due to its potential size, using
// MediaResult const references is recommended.
namespace mozilla {

class MediaResult
{
public:
  MOZ_IMPLICIT MediaResult(nsresult aResult)
    : mCode(aResult)
  {
  }
  MediaResult(nsresult aResult, const nsACString& aMessage)
    : mCode(aResult)
    , mMessage(aMessage)
  {
  }
  MediaResult(nsresult aResult, const char* aMessage)
    : mCode(aResult)
    , mMessage(aMessage)
  {
  }
  MediaResult(const MediaResult& aOther) = default;
  MediaResult(MediaResult&& aOther) = default;
  MediaResult& operator=(const MediaResult& aOther) = default;
  MediaResult& operator=(MediaResult&& aOther) = default;

  nsresult Code() const { return mCode; }
  const nsCString& Message() const { return mMessage; }

  // Interoperations with nsresult.
  bool operator==(nsresult aResult) const { return aResult == mCode; }
  bool operator!=(nsresult aResult) const { return aResult != mCode; }
  operator nsresult () const { return mCode; }

  nsCString Description() const
  {
    if (NS_SUCCEEDED(mCode)) {
      return nsCString();
    }
    return nsPrintfCString("0x%08x: %s", mCode, mMessage.get());
  }

private:
  nsresult mCode;
  nsCString mMessage;
};

#define RESULT_DETAIL(arg, ...) nsPrintfCString("%s: " arg, __func__, ##__VA_ARGS__)

} // namespace mozilla
#endif // MediaResult_h_