Source code

Revision control

Other Tools

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