UniquePtr.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef js_UniquePtr_h
  6. #define js_UniquePtr_h
  7. #include "mozilla/UniquePtr.h"
  8. #include "js/Utility.h"
  9. namespace js {
  10. // Replacement for mozilla::UniquePtr that defaults to js::DefaultDelete.
  11. template <typename T, typename D = JS::DeletePolicy<T>>
  12. using UniquePtr = mozilla::UniquePtr<T, D>;
  13. namespace detail {
  14. template<typename T>
  15. struct UniqueSelector
  16. {
  17. typedef UniquePtr<T> SingleObject;
  18. };
  19. template<typename T>
  20. struct UniqueSelector<T[]>
  21. {
  22. typedef UniquePtr<T[]> UnknownBound;
  23. };
  24. template<typename T, decltype(sizeof(int)) N>
  25. struct UniqueSelector<T[N]>
  26. {
  27. typedef UniquePtr<T[N]> KnownBound;
  28. };
  29. } // namespace detail
  30. // Replacement for mozilla::MakeUnique that correctly calls js_new and produces
  31. // a js::UniquePtr.
  32. template<typename T, typename... Args>
  33. typename detail::UniqueSelector<T>::SingleObject
  34. MakeUnique(Args&&... aArgs)
  35. {
  36. return UniquePtr<T>(js_new<T>(mozilla::Forward<Args>(aArgs)...));
  37. }
  38. template<typename T>
  39. typename detail::UniqueSelector<T>::UnknownBound
  40. MakeUnique(decltype(sizeof(int)) aN) = delete;
  41. template<typename T, typename... Args>
  42. typename detail::UniqueSelector<T>::KnownBound
  43. MakeUnique(Args&&... aArgs) = delete;
  44. } // namespace js
  45. #endif /* js_UniquePtr_h */