blob: 02fa15d74587b69cddd4c42fce3a88187ffbe611 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000011// Borrowed from Chromium's src/base/memory/scoped_ptr.h.
12
13// Scopers help you manage ownership of a pointer, helping you easily manage a
14// pointer within a scope, and automatically destroying the pointer at the end
15// of a scope. There are two main classes you will use, which correspond to the
16// operators new/delete and new[]/delete[].
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017//
18// Example usage (scoped_ptr<T>):
19// {
20// scoped_ptr<Foo> foo(new Foo("wee"));
21// } // foo goes out of scope, releasing the pointer with it.
22//
23// {
24// scoped_ptr<Foo> foo; // No pointer managed.
25// foo.reset(new Foo("wee")); // Now a pointer is managed.
26// foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
27// foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
28// foo->Method(); // Foo::Method() called.
29// foo.get()->Method(); // Foo::Method() called.
30// SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer
31// // manages a pointer.
32// foo.reset(new Foo("wee4")); // foo manages a pointer again.
33// foo.reset(); // Foo("wee4") destroyed, foo no longer
34// // manages a pointer.
35// } // foo wasn't managing a pointer, so nothing was destroyed.
36//
37// Example usage (scoped_ptr<T[]>):
38// {
39// scoped_ptr<Foo[]> foo(new Foo[100]);
40// foo.get()->Method(); // Foo::Method on the 0th element.
41// foo[10].Method(); // Foo::Method on the 10th element.
42// }
43//
44// These scopers also implement part of the functionality of C++11 unique_ptr
45// in that they are "movable but not copyable." You can use the scopers in
46// the parameter and return types of functions to signify ownership transfer
47// in to and out of a function. When calling a function that has a scoper
48// as the argument type, it must be called with the result of an analogous
49// scoper's Pass() function or another function that generates a temporary;
50// passing by copy will NOT work. Here is an example using scoped_ptr:
51//
52// void TakesOwnership(scoped_ptr<Foo> arg) {
53// // Do something with arg
54// }
55// scoped_ptr<Foo> CreateFoo() {
56// // No need for calling Pass() because we are constructing a temporary
57// // for the return value.
58// return scoped_ptr<Foo>(new Foo("new"));
59// }
60// scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
61// return arg.Pass();
62// }
63//
64// {
65// scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay").
66// TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay").
67// scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo.
68// scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000069// PassThru(ptr2.Pass()); // ptr2 is correspondingly nullptr.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070// }
71//
72// Notice that if you do not call Pass() when returning from PassThru(), or
73// when invoking TakesOwnership(), the code will not compile because scopers
74// are not copyable; they only implement move semantics which require calling
75// the Pass() function to signify a destructive transfer of state. CreateFoo()
76// is different though because we are constructing a temporary on the return
77// line and thus can avoid needing to call Pass().
78//
79// Pass() properly handles upcast in initialization, i.e. you can use a
80// scoped_ptr<Child> to initialize a scoped_ptr<Parent>:
81//
82// scoped_ptr<Foo> foo(new Foo());
83// scoped_ptr<FooParent> parent(foo.Pass());
84//
85// PassAs<>() should be used to upcast return value in return statement:
86//
87// scoped_ptr<Foo> CreateFoo() {
88// scoped_ptr<FooChild> result(new FooChild());
89// return result.PassAs<Foo>();
90// }
91//
92// Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for
93// scoped_ptr<T[]>. This is because casting array pointers may not be safe.
94
95#ifndef WEBRTC_BASE_SCOPED_PTR_H__
96#define WEBRTC_BASE_SCOPED_PTR_H__
97
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000098// This is an implementation designed to match the anticipated future TR2
99// implementation of the scoped_ptr class.
100
101#include <assert.h>
102#include <stddef.h>
103#include <stdlib.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104
105#include <algorithm> // For std::swap().
106
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000107#include "webrtc/base/constructormagic.h"
108#include "webrtc/base/move.h"
109#include "webrtc/base/template_util.h"
110#include "webrtc/typedefs.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
112namespace rtc {
113
114// Function object which deletes its parameter, which must be a pointer.
115// If C is an array type, invokes 'delete[]' on the parameter; otherwise,
116// invokes 'delete'. The default deleter for scoped_ptr<T>.
117template <class T>
118struct DefaultDeleter {
119 DefaultDeleter() {}
120 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) {
121 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor
122 // if U* is implicitly convertible to T* and U is not an array type.
123 //
124 // Correct implementation should use SFINAE to disable this
125 // constructor. However, since there are no other 1-argument constructors,
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000126 // using a static_assert based on is_convertible<> and requiring
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 // complete types is simpler and will cause compile failures for equivalent
128 // misuses.
129 //
130 // Note, the is_convertible<U*, T*> check also ensures that U is not an
131 // array. T is guaranteed to be a non-array, so any U* where U is an array
132 // cannot convert to T*.
133 enum { T_must_be_complete = sizeof(T) };
134 enum { U_must_be_complete = sizeof(U) };
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000135 static_assert(rtc::is_convertible<U*, T*>::value,
136 "U* must implicitly convert to T*");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 }
138 inline void operator()(T* ptr) const {
139 enum { type_must_be_complete = sizeof(T) };
140 delete ptr;
141 }
142};
143
144// Specialization of DefaultDeleter for array types.
145template <class T>
146struct DefaultDeleter<T[]> {
147 inline void operator()(T* ptr) const {
148 enum { type_must_be_complete = sizeof(T) };
149 delete[] ptr;
150 }
151
152 private:
153 // Disable this operator for any U != T because it is undefined to execute
154 // an array delete when the static type of the array mismatches the dynamic
155 // type.
156 //
157 // References:
158 // C++98 [expr.delete]p3
159 // http://cplusplus.github.com/LWG/lwg-defects.html#938
160 template <typename U> void operator()(U* array) const;
161};
162
163template <class T, int n>
164struct DefaultDeleter<T[n]> {
165 // Never allow someone to declare something like scoped_ptr<int[10]>.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000166 static_assert(sizeof(T) == -1, "do not use array with size as type");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167};
168
169// Function object which invokes 'free' on its parameter, which must be
170// a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
171//
172// scoped_ptr<int, rtc::FreeDeleter> foo_ptr(
173// static_cast<int*>(malloc(sizeof(int))));
174struct FreeDeleter {
175 inline void operator()(void* ptr) const {
176 free(ptr);
177 }
178};
179
180namespace internal {
181
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000182template <typename T>
183struct ShouldAbortOnSelfReset {
184 template <typename U>
185 static rtc::internal::NoType Test(const typename U::AllowSelfReset*);
186
187 template <typename U>
188 static rtc::internal::YesType Test(...);
189
190 static const bool value =
191 sizeof(Test<T>(0)) == sizeof(rtc::internal::YesType);
192};
193
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194// Minimal implementation of the core logic of scoped_ptr, suitable for
195// reuse in both scoped_ptr and its specializations.
196template <class T, class D>
197class scoped_ptr_impl {
198 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000199 explicit scoped_ptr_impl(T* p) : data_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200
201 // Initializer for deleters that have data parameters.
202 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {}
203
204 // Templated constructor that destructively takes the value from another
205 // scoped_ptr_impl.
206 template <typename U, typename V>
207 scoped_ptr_impl(scoped_ptr_impl<U, V>* other)
208 : data_(other->release(), other->get_deleter()) {
209 // We do not support move-only deleters. We could modify our move
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000210 // emulation to have rtc::subtle::move() and rtc::subtle::forward()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 // functions that are imperfect emulations of their C++11 equivalents,
212 // but until there's a requirement, just assume deleters are copyable.
213 }
214
215 template <typename U, typename V>
216 void TakeState(scoped_ptr_impl<U, V>* other) {
217 // See comment in templated constructor above regarding lack of support
218 // for move-only deleters.
219 reset(other->release());
220 get_deleter() = other->get_deleter();
221 }
222
223 ~scoped_ptr_impl() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000224 if (data_.ptr != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225 // Not using get_deleter() saves one function call in non-optimized
226 // builds.
227 static_cast<D&>(data_)(data_.ptr);
228 }
229 }
230
231 void reset(T* p) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000232 // This is a self-reset, which is no longer allowed for default deleters:
233 // https://crbug.com/162971
234 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235
236 // Note that running data_.ptr = p can lead to undefined behavior if
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000237 // get_deleter()(get()) deletes this. In order to prevent this, reset()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000238 // should update the stored pointer before deleting its old value.
239 //
240 // However, changing reset() to use that behavior may cause current code to
241 // break in unexpected ways. If the destruction of the owned object
242 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
243 // then it will incorrectly dispatch calls to |p| rather than the original
244 // value of |data_.ptr|.
245 //
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000246 // During the transition period, set the stored pointer to nullptr while
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 // deleting the object. Eventually, this safety check will be removed to
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000248 // prevent the scenario initially described from occurring and
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249 // http://crbug.com/176091 can be closed.
250 T* old = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000251 data_.ptr = nullptr;
252 if (old != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253 static_cast<D&>(data_)(old);
254 data_.ptr = p;
255 }
256
257 T* get() const { return data_.ptr; }
258
259 D& get_deleter() { return data_; }
260 const D& get_deleter() const { return data_; }
261
262 void swap(scoped_ptr_impl& p2) {
263 // Standard swap idiom: 'using std::swap' ensures that std::swap is
264 // present in the overload set, but we call swap unqualified so that
265 // any more-specific overloads can be used, if available.
266 using std::swap;
267 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_));
268 swap(data_.ptr, p2.data_.ptr);
269 }
270
271 T* release() {
272 T* old_ptr = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000273 data_.ptr = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274 return old_ptr;
275 }
276
277 T** accept() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000278 reset(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 return &(data_.ptr);
280 }
281
282 T** use() {
283 return &(data_.ptr);
284 }
285
286 private:
287 // Needed to allow type-converting constructor.
288 template <typename U, typename V> friend class scoped_ptr_impl;
289
290 // Use the empty base class optimization to allow us to have a D
291 // member, while avoiding any space overhead for it when D is an
292 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
293 // discussion of this technique.
294 struct Data : public D {
295 explicit Data(T* ptr_in) : ptr(ptr_in) {}
296 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {}
297 T* ptr;
298 };
299
300 Data data_;
301
302 DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
303};
304
305} // namespace internal
306
307// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
308// automatically deletes the pointer it holds (if any).
309// That is, scoped_ptr<T> owns the T object that it points to.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000310// Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T
311// object. Also like T*, scoped_ptr<T> is thread-compatible, and once you
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000312// dereference it, you get the thread safety guarantees of T.
313//
314// The size of scoped_ptr is small. On most compilers, when using the
315// DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
316// increase the size proportional to whatever state they need to have. See
317// comments inside scoped_ptr_impl<> for details.
318//
319// Current implementation targets having a strict subset of C++11's
320// unique_ptr<> features. Known deficiencies include not supporting move-only
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000321// deleters, function pointers as deleters, and deleters with reference
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322// types.
323template <class T, class D = rtc::DefaultDeleter<T> >
324class scoped_ptr {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000325 RTC_MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_ptr)
326
327 // TODO(ajm): If we ever import RefCountedBase, this check needs to be
328 // enabled.
329 //static_assert(rtc::internal::IsNotRefCounted<T>::value,
330 // "T is refcounted type and needs scoped refptr");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000331
332 public:
333 // The element and deleter types.
334 typedef T element_type;
335 typedef D deleter_type;
336
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000337 // Constructor. Defaults to initializing with nullptr.
338 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339
340 // Constructor. Takes ownership of p.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000341 explicit scoped_ptr(element_type* p) : impl_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342
343 // Constructor. Allows initialization of a stateful deleter.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000344 scoped_ptr(element_type* p, const D& d) : impl_(p, d) {}
345
346 // Constructor. Allows construction from a nullptr.
347 scoped_ptr(decltype(nullptr)) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000348
349 // Constructor. Allows construction from a scoped_ptr rvalue for a
350 // convertible type and deleter.
351 //
352 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
353 // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
354 // has different post-conditions if D is a reference type. Since this
355 // implementation does not support deleters with reference type,
356 // we do not need a separate move constructor allowing us to avoid one
357 // use of SFINAE. You only need to care about this if you modify the
358 // implementation of scoped_ptr.
359 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000360 scoped_ptr(scoped_ptr<U, V>&& other)
361 : impl_(&other.impl_) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000362 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363 }
364
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000365 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
366 // type and deleter.
367 //
368 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
369 // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
370 // form has different requirements on for move-only Deleters. Since this
371 // implementation does not support move-only Deleters, we do not need a
372 // separate move assignment operator allowing us to avoid one use of SFINAE.
373 // You only need to care about this if you modify the implementation of
374 // scoped_ptr.
375 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000376 scoped_ptr& operator=(scoped_ptr<U, V>&& rhs) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000377 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000378 impl_.TakeState(&rhs.impl_);
379 return *this;
380 }
381
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000382 // operator=. Allows assignment from a nullptr. Deletes the currently owned
383 // object, if any.
384 scoped_ptr& operator=(decltype(nullptr)) {
385 reset();
386 return *this;
387 }
388
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000389 // Reset. Deletes the currently owned object, if any.
390 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000391 void reset(element_type* p = nullptr) { impl_.reset(p); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000392
393 // Accessors to get the owned object.
394 // operator* and operator-> will assert() if there is no current object.
395 element_type& operator*() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000396 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000397 return *impl_.get();
398 }
399 element_type* operator->() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000400 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000401 return impl_.get();
402 }
403 element_type* get() const { return impl_.get(); }
404
405 // Access to the deleter.
406 deleter_type& get_deleter() { return impl_.get_deleter(); }
407 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
408
409 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
410 // implicitly convertible to a real bool (which is dangerous).
411 //
412 // Note that this trick is only safe when the == and != operators
413 // are declared explicitly, as otherwise "scoped_ptr1 ==
414 // scoped_ptr2" will compile but do the wrong thing (i.e., convert
415 // to Testable and then do the comparison).
416 private:
417 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
418 scoped_ptr::*Testable;
419
420 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000421 operator Testable() const {
422 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
423 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000424
425 // Comparison operators.
426 // These return whether two scoped_ptr refer to the same object, not just to
427 // two different but equal objects.
428 bool operator==(const element_type* p) const { return impl_.get() == p; }
429 bool operator!=(const element_type* p) const { return impl_.get() != p; }
430
431 // Swap two scoped pointers.
432 void swap(scoped_ptr& p2) {
433 impl_.swap(p2.impl_);
434 }
435
436 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000437 // The return value is the current pointer held by this object. If this object
438 // holds a nullptr, the return value is nullptr. After this operation, this
439 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000440 element_type* release() WARN_UNUSED_RESULT {
441 return impl_.release();
442 }
443
444 // Delete the currently held pointer and return a pointer
445 // to allow overwriting of the current pointer address.
446 element_type** accept() WARN_UNUSED_RESULT {
447 return impl_.accept();
448 }
449
450 // Return a pointer to the current pointer address.
451 element_type** use() WARN_UNUSED_RESULT {
452 return impl_.use();
453 }
454
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000455 private:
456 // Needed to reach into |impl_| in the constructor.
457 template <typename U, typename V> friend class scoped_ptr;
458 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
459
460 // Forbidden for API compatibility with std::unique_ptr.
461 explicit scoped_ptr(int disallow_construction_from_null);
462
463 // Forbid comparison of scoped_ptr types. If U != T, it totally
464 // doesn't make sense, and if U == T, it still doesn't make sense
465 // because you should never have the same object owned by two different
466 // scoped_ptrs.
467 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
468 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
469};
470
471template <class T, class D>
472class scoped_ptr<T[], D> {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000473 RTC_MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_ptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000474
475 public:
476 // The element and deleter types.
477 typedef T element_type;
478 typedef D deleter_type;
479
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000480 // Constructor. Defaults to initializing with nullptr.
481 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000482
483 // Constructor. Stores the given array. Note that the argument's type
484 // must exactly match T*. In particular:
485 // - it cannot be a pointer to a type derived from T, because it is
486 // inherently unsafe in the general case to access an array through a
487 // pointer whose dynamic type does not match its static type (eg., if
488 // T and the derived types had different sizes access would be
489 // incorrectly calculated). Deletion is also always undefined
490 // (C++98 [expr.delete]p3). If you're doing this, fix your code.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000491 // - it cannot be const-qualified differently from T per unique_ptr spec
492 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
493 // to work around this may use implicit_cast<const T*>().
494 // However, because of the first bullet in this comment, users MUST
495 // NOT use implicit_cast<Base*>() to upcast the static type of the array.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000496 explicit scoped_ptr(element_type* array) : impl_(array) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000497
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000498 // Constructor. Allows construction from a nullptr.
499 scoped_ptr(decltype(nullptr)) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000500
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000501 // Constructor. Allows construction from a scoped_ptr rvalue.
502 scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {}
503
504 // operator=. Allows assignment from a scoped_ptr rvalue.
505 scoped_ptr& operator=(scoped_ptr&& rhs) {
506 impl_.TakeState(&rhs.impl_);
507 return *this;
508 }
509
510 // operator=. Allows assignment from a nullptr. Deletes the currently owned
511 // array, if any.
512 scoped_ptr& operator=(decltype(nullptr)) {
513 reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000514 return *this;
515 }
516
517 // Reset. Deletes the currently owned array, if any.
518 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000519 void reset(element_type* array = nullptr) { impl_.reset(array); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000520
521 // Accessors to get the owned array.
522 element_type& operator[](size_t i) const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000523 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000524 return impl_.get()[i];
525 }
526 element_type* get() const { return impl_.get(); }
527
528 // Access to the deleter.
529 deleter_type& get_deleter() { return impl_.get_deleter(); }
530 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
531
532 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
533 // implicitly convertible to a real bool (which is dangerous).
534 private:
535 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
536 scoped_ptr::*Testable;
537
538 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000539 operator Testable() const {
540 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
541 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000542
543 // Comparison operators.
544 // These return whether two scoped_ptr refer to the same object, not just to
545 // two different but equal objects.
546 bool operator==(element_type* array) const { return impl_.get() == array; }
547 bool operator!=(element_type* array) const { return impl_.get() != array; }
548
549 // Swap two scoped pointers.
550 void swap(scoped_ptr& p2) {
551 impl_.swap(p2.impl_);
552 }
553
554 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000555 // The return value is the current pointer held by this object. If this object
556 // holds a nullptr, the return value is nullptr. After this operation, this
557 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558 element_type* release() WARN_UNUSED_RESULT {
559 return impl_.release();
560 }
561
562 // Delete the currently held pointer and return a pointer
563 // to allow overwriting of the current pointer address.
564 element_type** accept() WARN_UNUSED_RESULT {
565 return impl_.accept();
566 }
567
568 // Return a pointer to the current pointer address.
569 element_type** use() WARN_UNUSED_RESULT {
570 return impl_.use();
571 }
572
573 private:
574 // Force element_type to be a complete type.
575 enum { type_must_be_complete = sizeof(element_type) };
576
577 // Actually hold the data.
578 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
579
580 // Disable initialization from any type other than element_type*, by
581 // providing a constructor that matches such an initialization, but is
582 // private and has no definition. This is disabled because it is not safe to
583 // call delete[] on an array whose static type does not match its dynamic
584 // type.
585 template <typename U> explicit scoped_ptr(U* array);
586 explicit scoped_ptr(int disallow_construction_from_null);
587
588 // Disable reset() from any type other than element_type*, for the same
589 // reasons as the constructor above.
590 template <typename U> void reset(U* array);
591 void reset(int disallow_reset_from_null);
592
593 // Forbid comparison of scoped_ptr types. If U != T, it totally
594 // doesn't make sense, and if U == T, it still doesn't make sense
595 // because you should never have the same object owned by two different
596 // scoped_ptrs.
597 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
598 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
599};
600
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000601template <class T, class D>
602void swap(rtc::scoped_ptr<T, D>& p1, rtc::scoped_ptr<T, D>& p2) {
603 p1.swap(p2);
604}
605
Karl Wiberg94784372015-04-20 14:03:07 +0200606} // namespace rtc
607
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000608template <class T, class D>
609bool operator==(T* p1, const rtc::scoped_ptr<T, D>& p2) {
610 return p1 == p2.get();
611}
612
613template <class T, class D>
614bool operator!=(T* p1, const rtc::scoped_ptr<T, D>& p2) {
615 return p1 != p2.get();
616}
617
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000618// A function to convert T* into scoped_ptr<T>
619// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
620// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
621template <typename T>
622rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) {
623 return rtc::scoped_ptr<T>(ptr);
624}
625
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000626#endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__