mfbt/UniquePtrExtensions.h
author Jorg K <mozilla@jorgk.com>
Mon, 23 Nov 2015 11:04:26 -0800
changeset 273865 0c34cc2dc429d2ea06415dc977def64d1a7ac72a
parent 271447 9bfc35f6d7abc7c22dd6562b645d19238e12865d
child 286335 6d7c88d923886811a7d3608ec2f4b700cc30ba85
permissions -rw-r--r--
Bug 938991 - text/rtf support for clipboard data. r=enndeakin

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

/* Useful extensions to UniquePtr. */

#ifndef mozilla_UniquePtrExtensions_h
#define mozilla_UniquePtrExtensions_h

#include "mozilla/fallible.h"
#include "mozilla/UniquePtr.h"

namespace mozilla {

/**
 * MakeUniqueFallible works exactly like MakeUnique, except that the memory
 * allocation performed is done fallibly, i.e. it can return nullptr.
 */
template<typename T, typename... Args>
typename detail::UniqueSelector<T>::SingleObject
MakeUniqueFallible(Args&&... aArgs)
{
  return UniquePtr<T>(new (fallible) T(Forward<Args>(aArgs)...));
}

template<typename T>
typename detail::UniqueSelector<T>::UnknownBound
MakeUniqueFallible(decltype(sizeof(int)) aN)
{
  typedef typename RemoveExtent<T>::Type ArrayType;
  return UniquePtr<T>(new (fallible) ArrayType[aN]());
}

template<typename T, typename... Args>
typename detail::UniqueSelector<T>::KnownBound
MakeUniqueFallible(Args&&... aArgs) = delete;

} // namespace mozilla

#endif // mozilla_UniquePtrExtensions_h