UniquePtr.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  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. /* Smart pointer managing sole ownership of a resource. */
  7. #ifndef mozilla_UniquePtr_h
  8. #define mozilla_UniquePtr_h
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/Compiler.h"
  12. #include "mozilla/Move.h"
  13. #include "mozilla/Pair.h"
  14. #include "mozilla/TypeTraits.h"
  15. namespace mozilla {
  16. template<typename T> class DefaultDelete;
  17. template<typename T, class D = DefaultDelete<T>> class UniquePtr;
  18. } // namespace mozilla
  19. namespace mozilla {
  20. namespace detail {
  21. struct HasPointerTypeHelper
  22. {
  23. template <class U> static double Test(...);
  24. template <class U> static char Test(typename U::pointer* = 0);
  25. };
  26. template <class T>
  27. class HasPointerType : public IntegralConstant<bool, sizeof(HasPointerTypeHelper::Test<T>(0)) == 1>
  28. {
  29. };
  30. template <class T, class D, bool = HasPointerType<D>::value>
  31. struct PointerTypeImpl
  32. {
  33. typedef typename D::pointer Type;
  34. };
  35. template <class T, class D>
  36. struct PointerTypeImpl<T, D, false>
  37. {
  38. typedef T* Type;
  39. };
  40. template <class T, class D>
  41. struct PointerType
  42. {
  43. typedef typename PointerTypeImpl<T, typename RemoveReference<D>::Type>::Type Type;
  44. };
  45. } // namespace detail
  46. /**
  47. * UniquePtr is a smart pointer that wholly owns a resource. Ownership may be
  48. * transferred out of a UniquePtr through explicit action, but otherwise the
  49. * resource is destroyed when the UniquePtr is destroyed.
  50. *
  51. * UniquePtr is similar to C++98's std::auto_ptr, but it improves upon auto_ptr
  52. * in one crucial way: it's impossible to copy a UniquePtr. Copying an auto_ptr
  53. * obviously *can't* copy ownership of its singly-owned resource. So what
  54. * happens if you try to copy one? Bizarrely, ownership is implicitly
  55. * *transferred*, preserving single ownership but breaking code that assumes a
  56. * copy of an object is identical to the original. (This is why auto_ptr is
  57. * prohibited in STL containers.)
  58. *
  59. * UniquePtr solves this problem by being *movable* rather than copyable.
  60. * Instead of passing a |UniquePtr u| directly to the constructor or assignment
  61. * operator, you pass |Move(u)|. In doing so you indicate that you're *moving*
  62. * ownership out of |u|, into the target of the construction/assignment. After
  63. * the transfer completes, |u| contains |nullptr| and may be safely destroyed.
  64. * This preserves single ownership but also allows UniquePtr to be moved by
  65. * algorithms that have been made move-safe. (Note: if |u| is instead a
  66. * temporary expression, don't use |Move()|: just pass the expression, because
  67. * it's already move-ready. For more information see Move.h.)
  68. *
  69. * UniquePtr is also better than std::auto_ptr in that the deletion operation is
  70. * customizable. An optional second template parameter specifies a class that
  71. * (through its operator()(T*)) implements the desired deletion policy. If no
  72. * policy is specified, mozilla::DefaultDelete<T> is used -- which will either
  73. * |delete| or |delete[]| the resource, depending whether the resource is an
  74. * array. Custom deletion policies ideally should be empty classes (no member
  75. * fields, no member fields in base classes, no virtual methods/inheritance),
  76. * because then UniquePtr can be just as efficient as a raw pointer.
  77. *
  78. * Use of UniquePtr proceeds like so:
  79. *
  80. * UniquePtr<int> g1; // initializes to nullptr
  81. * g1.reset(new int); // switch resources using reset()
  82. * g1 = nullptr; // clears g1, deletes the int
  83. *
  84. * UniquePtr<int> g2(new int); // owns that int
  85. * int* p = g2.release(); // g2 leaks its int -- still requires deletion
  86. * delete p; // now freed
  87. *
  88. * struct S { int x; S(int x) : x(x) {} };
  89. * UniquePtr<S> g3, g4(new S(5));
  90. * g3 = Move(g4); // g3 owns the S, g4 cleared
  91. * S* p = g3.get(); // g3 still owns |p|
  92. * assert(g3->x == 5); // operator-> works (if .get() != nullptr)
  93. * assert((*g3).x == 5); // also operator* (again, if not cleared)
  94. * Swap(g3, g4); // g4 now owns the S, g3 cleared
  95. * g3.swap(g4); // g3 now owns the S, g4 cleared
  96. * UniquePtr<S> g5(Move(g3)); // g5 owns the S, g3 cleared
  97. * g5.reset(); // deletes the S, g5 cleared
  98. *
  99. * struct FreePolicy { void operator()(void* p) { free(p); } };
  100. * UniquePtr<int, FreePolicy> g6(static_cast<int*>(malloc(sizeof(int))));
  101. * int* ptr = g6.get();
  102. * g6 = nullptr; // calls free(ptr)
  103. *
  104. * Now, carefully note a few things you *can't* do:
  105. *
  106. * UniquePtr<int> b1;
  107. * b1 = new int; // BAD: can only assign another UniquePtr
  108. * int* ptr = b1; // BAD: no auto-conversion to pointer, use get()
  109. *
  110. * UniquePtr<int> b2(b1); // BAD: can't copy a UniquePtr
  111. * UniquePtr<int> b3 = b1; // BAD: can't copy-assign a UniquePtr
  112. *
  113. * (Note that changing a UniquePtr to store a direct |new| expression is
  114. * permitted, but usually you should use MakeUnique, defined at the end of this
  115. * header.)
  116. *
  117. * A few miscellaneous notes:
  118. *
  119. * UniquePtr, when not instantiated for an array type, can be move-constructed
  120. * and move-assigned, not only from itself but from "derived" UniquePtr<U, E>
  121. * instantiations where U converts to T and E converts to D. If you want to use
  122. * this, you're going to have to specify a deletion policy for both UniquePtr
  123. * instantations, and T pretty much has to have a virtual destructor. In other
  124. * words, this doesn't work:
  125. *
  126. * struct Base { virtual ~Base() {} };
  127. * struct Derived : Base {};
  128. *
  129. * UniquePtr<Base> b1;
  130. * // BAD: DefaultDelete<Base> and DefaultDelete<Derived> don't interconvert
  131. * UniquePtr<Derived> d1(Move(b));
  132. *
  133. * UniquePtr<Base> b2;
  134. * UniquePtr<Derived, DefaultDelete<Base>> d2(Move(b2)); // okay
  135. *
  136. * UniquePtr is specialized for array types. Specializing with an array type
  137. * creates a smart-pointer version of that array -- not a pointer to such an
  138. * array.
  139. *
  140. * UniquePtr<int[]> arr(new int[5]);
  141. * arr[0] = 4;
  142. *
  143. * What else is different? Deletion of course uses |delete[]|. An operator[]
  144. * is provided. Functionality that doesn't make sense for arrays is removed.
  145. * The constructors and mutating methods only accept array pointers (not T*, U*
  146. * that converts to T*, or UniquePtr<U[]> or UniquePtr<U>) or |nullptr|.
  147. *
  148. * It's perfectly okay for a function to return a UniquePtr. This transfers
  149. * the UniquePtr's sole ownership of the data, to the fresh UniquePtr created
  150. * in the calling function, that will then solely own that data. Such functions
  151. * can return a local variable UniquePtr, |nullptr|, |UniquePtr(ptr)| where
  152. * |ptr| is a |T*|, or a UniquePtr |Move()|'d from elsewhere.
  153. *
  154. * UniquePtr will commonly be a member of a class, with lifetime equivalent to
  155. * that of that class. If you want to expose the related resource, you could
  156. * expose a raw pointer via |get()|, but ownership of a raw pointer is
  157. * inherently unclear. So it's better to expose a |const UniquePtr&| instead.
  158. * This prohibits mutation but still allows use of |get()| when needed (but
  159. * operator-> is preferred). Of course, you can only use this smart pointer as
  160. * long as the enclosing class instance remains live -- no different than if you
  161. * exposed the |get()| raw pointer.
  162. *
  163. * To pass a UniquePtr-managed resource as a pointer, use a |const UniquePtr&|
  164. * argument. To specify an inout parameter (where the method may or may not
  165. * take ownership of the resource, or reset it), or to specify an out parameter
  166. * (where simply returning a |UniquePtr| isn't possible), use a |UniquePtr&|
  167. * argument. To unconditionally transfer ownership of a UniquePtr
  168. * into a method, use a |UniquePtr| argument. To conditionally transfer
  169. * ownership of a resource into a method, should the method want it, use a
  170. * |UniquePtr&&| argument.
  171. */
  172. template<typename T, class D>
  173. class UniquePtr
  174. {
  175. public:
  176. typedef T ElementType;
  177. typedef D DeleterType;
  178. typedef typename detail::PointerType<T, DeleterType>::Type Pointer;
  179. private:
  180. Pair<Pointer, DeleterType> mTuple;
  181. Pointer& ptr() { return mTuple.first(); }
  182. const Pointer& ptr() const { return mTuple.first(); }
  183. DeleterType& del() { return mTuple.second(); }
  184. const DeleterType& del() const { return mTuple.second(); }
  185. public:
  186. /**
  187. * Construct a UniquePtr containing |nullptr|.
  188. */
  189. constexpr UniquePtr()
  190. : mTuple(static_cast<Pointer>(nullptr), DeleterType())
  191. {
  192. static_assert(!IsPointer<D>::value, "must provide a deleter instance");
  193. static_assert(!IsReference<D>::value, "must provide a deleter instance");
  194. }
  195. /**
  196. * Construct a UniquePtr containing |aPtr|.
  197. */
  198. explicit UniquePtr(Pointer aPtr)
  199. : mTuple(aPtr, DeleterType())
  200. {
  201. static_assert(!IsPointer<D>::value, "must provide a deleter instance");
  202. static_assert(!IsReference<D>::value, "must provide a deleter instance");
  203. }
  204. UniquePtr(Pointer aPtr,
  205. typename Conditional<IsReference<D>::value,
  206. D,
  207. const D&>::Type aD1)
  208. : mTuple(aPtr, aD1)
  209. {}
  210. // If you encounter an error with MSVC10 about RemoveReference below, along
  211. // the lines that "more than one partial specialization matches the template
  212. // argument list": don't use UniquePtr<T, reference to function>! Ideally
  213. // you should make deletion use the same function every time, using a
  214. // deleter policy:
  215. //
  216. // // BAD, won't compile with MSVC10, deleter doesn't need to be a
  217. // // variable at all
  218. // typedef void (&FreeSignature)(void*);
  219. // UniquePtr<int, FreeSignature> ptr((int*) malloc(sizeof(int)), free);
  220. //
  221. // // GOOD, compiles with MSVC10, deletion behavior statically known and
  222. // // optimizable
  223. // struct DeleteByFreeing
  224. // {
  225. // void operator()(void* aPtr) { free(aPtr); }
  226. // };
  227. //
  228. // If deletion really, truly, must be a variable: you might be able to work
  229. // around this with a deleter class that contains the function reference.
  230. // But this workaround is untried and untested, because variable deletion
  231. // behavior really isn't something you should use.
  232. UniquePtr(Pointer aPtr,
  233. typename RemoveReference<D>::Type&& aD2)
  234. : mTuple(aPtr, Move(aD2))
  235. {
  236. static_assert(!IsReference<D>::value,
  237. "rvalue deleter can't be stored by reference");
  238. }
  239. UniquePtr(UniquePtr&& aOther)
  240. : mTuple(aOther.release(), Forward<DeleterType>(aOther.get_deleter()))
  241. {}
  242. MOZ_IMPLICIT
  243. UniquePtr(decltype(nullptr))
  244. : mTuple(nullptr, DeleterType())
  245. {
  246. static_assert(!IsPointer<D>::value, "must provide a deleter instance");
  247. static_assert(!IsReference<D>::value, "must provide a deleter instance");
  248. }
  249. template<typename U, class E>
  250. MOZ_IMPLICIT
  251. UniquePtr(UniquePtr<U, E>&& aOther,
  252. typename EnableIf<IsConvertible<typename UniquePtr<U, E>::Pointer,
  253. Pointer>::value &&
  254. !IsArray<U>::value &&
  255. (IsReference<D>::value
  256. ? IsSame<D, E>::value
  257. : IsConvertible<E, D>::value),
  258. int>::Type aDummy = 0)
  259. : mTuple(aOther.release(), Forward<E>(aOther.get_deleter()))
  260. {
  261. }
  262. ~UniquePtr() { reset(nullptr); }
  263. UniquePtr& operator=(UniquePtr&& aOther)
  264. {
  265. reset(aOther.release());
  266. get_deleter() = Forward<DeleterType>(aOther.get_deleter());
  267. return *this;
  268. }
  269. template<typename U, typename E>
  270. UniquePtr& operator=(UniquePtr<U, E>&& aOther)
  271. {
  272. static_assert(IsConvertible<typename UniquePtr<U, E>::Pointer,
  273. Pointer>::value,
  274. "incompatible UniquePtr pointees");
  275. static_assert(!IsArray<U>::value,
  276. "can't assign from UniquePtr holding an array");
  277. reset(aOther.release());
  278. get_deleter() = Forward<E>(aOther.get_deleter());
  279. return *this;
  280. }
  281. UniquePtr& operator=(decltype(nullptr))
  282. {
  283. reset(nullptr);
  284. return *this;
  285. }
  286. T& operator*() const { return *get(); }
  287. Pointer operator->() const
  288. {
  289. MOZ_ASSERT(get(), "dereferencing a UniquePtr containing nullptr");
  290. return get();
  291. }
  292. explicit operator bool() const { return get() != nullptr; }
  293. Pointer get() const { return ptr(); }
  294. DeleterType& get_deleter() { return del(); }
  295. const DeleterType& get_deleter() const { return del(); }
  296. MOZ_MUST_USE Pointer release()
  297. {
  298. Pointer p = ptr();
  299. ptr() = nullptr;
  300. return p;
  301. }
  302. void reset(Pointer aPtr = Pointer())
  303. {
  304. Pointer old = ptr();
  305. ptr() = aPtr;
  306. if (old != nullptr) {
  307. get_deleter()(old);
  308. }
  309. }
  310. void swap(UniquePtr& aOther)
  311. {
  312. mTuple.swap(aOther.mTuple);
  313. }
  314. UniquePtr(const UniquePtr& aOther) = delete; // construct using Move()!
  315. void operator=(const UniquePtr& aOther) = delete; // assign using Move()!
  316. };
  317. // In case you didn't read the comment by the main definition (you should!): the
  318. // UniquePtr<T[]> specialization exists to manage array pointers. It deletes
  319. // such pointers using delete[], it will reject construction and modification
  320. // attempts using U* or U[]. Otherwise it works like the normal UniquePtr.
  321. template<typename T, class D>
  322. class UniquePtr<T[], D>
  323. {
  324. public:
  325. typedef T* Pointer;
  326. typedef T ElementType;
  327. typedef D DeleterType;
  328. private:
  329. Pair<Pointer, DeleterType> mTuple;
  330. public:
  331. /**
  332. * Construct a UniquePtr containing nullptr.
  333. */
  334. constexpr UniquePtr()
  335. : mTuple(static_cast<Pointer>(nullptr), DeleterType())
  336. {
  337. static_assert(!IsPointer<D>::value, "must provide a deleter instance");
  338. static_assert(!IsReference<D>::value, "must provide a deleter instance");
  339. }
  340. /**
  341. * Construct a UniquePtr containing |aPtr|.
  342. */
  343. explicit UniquePtr(Pointer aPtr)
  344. : mTuple(aPtr, DeleterType())
  345. {
  346. static_assert(!IsPointer<D>::value, "must provide a deleter instance");
  347. static_assert(!IsReference<D>::value, "must provide a deleter instance");
  348. }
  349. // delete[] knows how to handle *only* an array of a single class type. For
  350. // delete[] to work correctly, it must know the size of each element, the
  351. // fields and base classes of each element requiring destruction, and so on.
  352. // So forbid all overloads which would end up invoking delete[] on a pointer
  353. // of the wrong type.
  354. template<typename U>
  355. UniquePtr(U&& aU,
  356. typename EnableIf<IsPointer<U>::value &&
  357. IsConvertible<U, Pointer>::value,
  358. int>::Type aDummy = 0)
  359. = delete;
  360. UniquePtr(Pointer aPtr,
  361. typename Conditional<IsReference<D>::value,
  362. D,
  363. const D&>::Type aD1)
  364. : mTuple(aPtr, aD1)
  365. {}
  366. // If you encounter an error with MSVC10 about RemoveReference below, along
  367. // the lines that "more than one partial specialization matches the template
  368. // argument list": don't use UniquePtr<T[], reference to function>! See the
  369. // comment by this constructor in the non-T[] specialization above.
  370. UniquePtr(Pointer aPtr,
  371. typename RemoveReference<D>::Type&& aD2)
  372. : mTuple(aPtr, Move(aD2))
  373. {
  374. static_assert(!IsReference<D>::value,
  375. "rvalue deleter can't be stored by reference");
  376. }
  377. // Forbidden for the same reasons as stated above.
  378. template<typename U, typename V>
  379. UniquePtr(U&& aU, V&& aV,
  380. typename EnableIf<IsPointer<U>::value &&
  381. IsConvertible<U, Pointer>::value,
  382. int>::Type aDummy = 0)
  383. = delete;
  384. UniquePtr(UniquePtr&& aOther)
  385. : mTuple(aOther.release(), Forward<DeleterType>(aOther.get_deleter()))
  386. {}
  387. MOZ_IMPLICIT
  388. UniquePtr(decltype(nullptr))
  389. : mTuple(nullptr, DeleterType())
  390. {
  391. static_assert(!IsPointer<D>::value, "must provide a deleter instance");
  392. static_assert(!IsReference<D>::value, "must provide a deleter instance");
  393. }
  394. ~UniquePtr() { reset(nullptr); }
  395. UniquePtr& operator=(UniquePtr&& aOther)
  396. {
  397. reset(aOther.release());
  398. get_deleter() = Forward<DeleterType>(aOther.get_deleter());
  399. return *this;
  400. }
  401. UniquePtr& operator=(decltype(nullptr))
  402. {
  403. reset();
  404. return *this;
  405. }
  406. explicit operator bool() const { return get() != nullptr; }
  407. T& operator[](decltype(sizeof(int)) aIndex) const { return get()[aIndex]; }
  408. Pointer get() const { return mTuple.first(); }
  409. DeleterType& get_deleter() { return mTuple.second(); }
  410. const DeleterType& get_deleter() const { return mTuple.second(); }
  411. MOZ_MUST_USE Pointer release()
  412. {
  413. Pointer p = mTuple.first();
  414. mTuple.first() = nullptr;
  415. return p;
  416. }
  417. void reset(Pointer aPtr = Pointer())
  418. {
  419. Pointer old = mTuple.first();
  420. mTuple.first() = aPtr;
  421. if (old != nullptr) {
  422. mTuple.second()(old);
  423. }
  424. }
  425. void reset(decltype(nullptr))
  426. {
  427. Pointer old = mTuple.first();
  428. mTuple.first() = nullptr;
  429. if (old != nullptr) {
  430. mTuple.second()(old);
  431. }
  432. }
  433. template<typename U>
  434. void reset(U) = delete;
  435. void swap(UniquePtr& aOther) { mTuple.swap(aOther.mTuple); }
  436. UniquePtr(const UniquePtr& aOther) = delete; // construct using Move()!
  437. void operator=(const UniquePtr& aOther) = delete; // assign using Move()!
  438. };
  439. /**
  440. * A default deletion policy using plain old operator delete.
  441. *
  442. * Note that this type can be specialized, but authors should beware of the risk
  443. * that the specialization may at some point cease to match (either because it
  444. * gets moved to a different compilation unit or the signature changes). If the
  445. * non-specialized (|delete|-based) version compiles for that type but does the
  446. * wrong thing, bad things could happen.
  447. *
  448. * This is a non-issue for types which are always incomplete (i.e. opaque handle
  449. * types), since |delete|-ing such a type will always trigger a compilation
  450. * error.
  451. */
  452. template<typename T>
  453. class DefaultDelete
  454. {
  455. public:
  456. constexpr DefaultDelete() {}
  457. template<typename U>
  458. MOZ_IMPLICIT DefaultDelete(const DefaultDelete<U>& aOther,
  459. typename EnableIf<mozilla::IsConvertible<U*, T*>::value,
  460. int>::Type aDummy = 0)
  461. {}
  462. void operator()(T* aPtr) const
  463. {
  464. static_assert(sizeof(T) > 0, "T must be complete");
  465. delete aPtr;
  466. }
  467. };
  468. /** A default deletion policy using operator delete[]. */
  469. template<typename T>
  470. class DefaultDelete<T[]>
  471. {
  472. public:
  473. constexpr DefaultDelete() {}
  474. void operator()(T* aPtr) const
  475. {
  476. static_assert(sizeof(T) > 0, "T must be complete");
  477. delete[] aPtr;
  478. }
  479. template<typename U>
  480. void operator()(U* aPtr) const = delete;
  481. };
  482. template<typename T, class D>
  483. void
  484. Swap(UniquePtr<T, D>& aX, UniquePtr<T, D>& aY)
  485. {
  486. aX.swap(aY);
  487. }
  488. template<typename T, class D, typename U, class E>
  489. bool
  490. operator==(const UniquePtr<T, D>& aX, const UniquePtr<U, E>& aY)
  491. {
  492. return aX.get() == aY.get();
  493. }
  494. template<typename T, class D, typename U, class E>
  495. bool
  496. operator!=(const UniquePtr<T, D>& aX, const UniquePtr<U, E>& aY)
  497. {
  498. return aX.get() != aY.get();
  499. }
  500. template<typename T, class D>
  501. bool
  502. operator==(const UniquePtr<T, D>& aX, decltype(nullptr))
  503. {
  504. return !aX;
  505. }
  506. template<typename T, class D>
  507. bool
  508. operator==(decltype(nullptr), const UniquePtr<T, D>& aX)
  509. {
  510. return !aX;
  511. }
  512. template<typename T, class D>
  513. bool
  514. operator!=(const UniquePtr<T, D>& aX, decltype(nullptr))
  515. {
  516. return bool(aX);
  517. }
  518. template<typename T, class D>
  519. bool
  520. operator!=(decltype(nullptr), const UniquePtr<T, D>& aX)
  521. {
  522. return bool(aX);
  523. }
  524. // No operator<, operator>, operator<=, operator>= for now because simplicity.
  525. namespace detail {
  526. template<typename T>
  527. struct UniqueSelector
  528. {
  529. typedef UniquePtr<T> SingleObject;
  530. };
  531. template<typename T>
  532. struct UniqueSelector<T[]>
  533. {
  534. typedef UniquePtr<T[]> UnknownBound;
  535. };
  536. template<typename T, decltype(sizeof(int)) N>
  537. struct UniqueSelector<T[N]>
  538. {
  539. typedef UniquePtr<T[N]> KnownBound;
  540. };
  541. } // namespace detail
  542. /**
  543. * MakeUnique is a helper function for allocating new'd objects and arrays,
  544. * returning a UniquePtr containing the resulting pointer. The semantics of
  545. * MakeUnique<Type>(...) are as follows.
  546. *
  547. * If Type is an array T[n]:
  548. * Disallowed, deleted, no overload for you!
  549. * If Type is an array T[]:
  550. * MakeUnique<T[]>(size_t) is the only valid overload. The pointer returned
  551. * is as if by |new T[n]()|, which value-initializes each element. (If T
  552. * isn't a class type, this will zero each element. If T is a class type,
  553. * then roughly speaking, each element will be constructed using its default
  554. * constructor. See C++11 [dcl.init]p7 for the full gory details.)
  555. * If Type is non-array T:
  556. * The arguments passed to MakeUnique<T>(...) are forwarded into a
  557. * |new T(...)| call, initializing the T as would happen if executing
  558. * |T(...)|.
  559. *
  560. * There are various benefits to using MakeUnique instead of |new| expressions.
  561. *
  562. * First, MakeUnique eliminates use of |new| from code entirely. If objects are
  563. * only created through UniquePtr, then (assuming all explicit release() calls
  564. * are safe, including transitively, and no type-safety casting funniness)
  565. * correctly maintained ownership of the UniquePtr guarantees no leaks are
  566. * possible. (This pays off best if a class is only ever created through a
  567. * factory method on the class, using a private constructor.)
  568. *
  569. * Second, initializing a UniquePtr using a |new| expression requires repeating
  570. * the name of the new'd type, whereas MakeUnique in concert with the |auto|
  571. * keyword names it only once:
  572. *
  573. * UniquePtr<char> ptr1(new char()); // repetitive
  574. * auto ptr2 = MakeUnique<char>(); // shorter
  575. *
  576. * Of course this assumes the reader understands the operation MakeUnique
  577. * performs. In the long run this is probably a reasonable assumption. In the
  578. * short run you'll have to use your judgment about what readers can be expected
  579. * to know, or to quickly look up.
  580. *
  581. * Third, a call to MakeUnique can be assigned directly to a UniquePtr. In
  582. * contrast you can't assign a pointer into a UniquePtr without using the
  583. * cumbersome reset().
  584. *
  585. * UniquePtr<char> p;
  586. * p = new char; // ERROR
  587. * p.reset(new char); // works, but fugly
  588. * p = MakeUnique<char>(); // preferred
  589. *
  590. * (And third, although not relevant to Mozilla: MakeUnique is exception-safe.
  591. * An exception thrown after |new T| succeeds will leak that memory, unless the
  592. * pointer is assigned to an object that will manage its ownership. UniquePtr
  593. * ably serves this function.)
  594. */
  595. template<typename T, typename... Args>
  596. typename detail::UniqueSelector<T>::SingleObject
  597. MakeUnique(Args&&... aArgs)
  598. {
  599. return UniquePtr<T>(new T(Forward<Args>(aArgs)...));
  600. }
  601. template<typename T>
  602. typename detail::UniqueSelector<T>::UnknownBound
  603. MakeUnique(decltype(sizeof(int)) aN)
  604. {
  605. typedef typename RemoveExtent<T>::Type ArrayType;
  606. return UniquePtr<T>(new ArrayType[aN]());
  607. }
  608. template<typename T, typename... Args>
  609. typename detail::UniqueSelector<T>::KnownBound
  610. MakeUnique(Args&&... aArgs) = delete;
  611. } // namespace mozilla
  612. #endif /* mozilla_UniquePtr_h */