blob: db615f30895fef2c22c03ba9439453102feeb2ad [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
kwiberg0eb15ed2015-12-17 03:04:15 -080045// in that they are "movable but not copyable." You can use the scopers in the
46// parameter and return types of functions to signify ownership transfer in to
47// and out of a function. When calling a function that has a scoper as the
48// argument type, it must be called with the result of calling std::move on an
49// analogous scoper, or another function that generates a temporary; passing by
50// copy will NOT work. Here is an example using scoped_ptr:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051//
52// void TakesOwnership(scoped_ptr<Foo> arg) {
53// // Do something with arg
54// }
55// scoped_ptr<Foo> CreateFoo() {
kwiberg0eb15ed2015-12-17 03:04:15 -080056// // No need for calling std::move because we are constructing a temporary
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057// // for the return value.
58// return scoped_ptr<Foo>(new Foo("new"));
59// }
60// scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
kwiberg0eb15ed2015-12-17 03:04:15 -080061// return std::move(arg);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062// }
63//
64// {
65// scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay").
kwiberg0eb15ed2015-12-17 03:04:15 -080066// TakesOwnership(std::move(ptr)); // ptr no longer owns Foo("yay").
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067// scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo.
68// scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
kwiberg0eb15ed2015-12-17 03:04:15 -080069// PassThru(std::move(ptr2)); // ptr2 is correspondingly nullptr.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070// }
71//
kwiberg0eb15ed2015-12-17 03:04:15 -080072// Notice that if you do not call std::move when returning from PassThru(), or
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073// when invoking TakesOwnership(), the code will not compile because scopers
74// are not copyable; they only implement move semantics which require calling
kwiberg0eb15ed2015-12-17 03:04:15 -080075// std::move to signify a destructive transfer of state. CreateFoo() is
76// different though because we are constructing a temporary on the return line
77// and thus can avoid needing to call std::move.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
79#ifndef WEBRTC_BASE_SCOPED_PTR_H__
80#define WEBRTC_BASE_SCOPED_PTR_H__
81
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000082// This is an implementation designed to match the anticipated future TR2
83// implementation of the scoped_ptr class.
84
85#include <assert.h>
86#include <stddef.h>
87#include <stdlib.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088
89#include <algorithm> // For std::swap().
kwiberg9390f842015-12-17 06:20:27 -080090#include <cstddef>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000092#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000093#include "webrtc/base/template_util.h"
94#include "webrtc/typedefs.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095
96namespace rtc {
97
98// Function object which deletes its parameter, which must be a pointer.
99// If C is an array type, invokes 'delete[]' on the parameter; otherwise,
100// invokes 'delete'. The default deleter for scoped_ptr<T>.
101template <class T>
102struct DefaultDeleter {
103 DefaultDeleter() {}
104 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) {
105 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor
106 // if U* is implicitly convertible to T* and U is not an array type.
107 //
108 // Correct implementation should use SFINAE to disable this
109 // constructor. However, since there are no other 1-argument constructors,
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000110 // using a static_assert based on is_convertible<> and requiring
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 // complete types is simpler and will cause compile failures for equivalent
112 // misuses.
113 //
114 // Note, the is_convertible<U*, T*> check also ensures that U is not an
115 // array. T is guaranteed to be a non-array, so any U* where U is an array
116 // cannot convert to T*.
117 enum { T_must_be_complete = sizeof(T) };
118 enum { U_must_be_complete = sizeof(U) };
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000119 static_assert(rtc::is_convertible<U*, T*>::value,
120 "U* must implicitly convert to T*");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 }
122 inline void operator()(T* ptr) const {
123 enum { type_must_be_complete = sizeof(T) };
124 delete ptr;
125 }
126};
127
128// Specialization of DefaultDeleter for array types.
129template <class T>
130struct DefaultDeleter<T[]> {
131 inline void operator()(T* ptr) const {
132 enum { type_must_be_complete = sizeof(T) };
133 delete[] ptr;
134 }
135
136 private:
137 // Disable this operator for any U != T because it is undefined to execute
138 // an array delete when the static type of the array mismatches the dynamic
139 // type.
140 //
141 // References:
142 // C++98 [expr.delete]p3
143 // http://cplusplus.github.com/LWG/lwg-defects.html#938
144 template <typename U> void operator()(U* array) const;
145};
146
147template <class T, int n>
148struct DefaultDeleter<T[n]> {
149 // Never allow someone to declare something like scoped_ptr<int[10]>.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000150 static_assert(sizeof(T) == -1, "do not use array with size as type");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151};
152
153// Function object which invokes 'free' on its parameter, which must be
154// a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
155//
156// scoped_ptr<int, rtc::FreeDeleter> foo_ptr(
157// static_cast<int*>(malloc(sizeof(int))));
158struct FreeDeleter {
159 inline void operator()(void* ptr) const {
160 free(ptr);
161 }
162};
163
164namespace internal {
165
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000166template <typename T>
167struct ShouldAbortOnSelfReset {
168 template <typename U>
169 static rtc::internal::NoType Test(const typename U::AllowSelfReset*);
170
171 template <typename U>
172 static rtc::internal::YesType Test(...);
173
174 static const bool value =
175 sizeof(Test<T>(0)) == sizeof(rtc::internal::YesType);
176};
177
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178// Minimal implementation of the core logic of scoped_ptr, suitable for
179// reuse in both scoped_ptr and its specializations.
180template <class T, class D>
181class scoped_ptr_impl {
182 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000183 explicit scoped_ptr_impl(T* p) : data_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184
185 // Initializer for deleters that have data parameters.
186 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {}
187
188 // Templated constructor that destructively takes the value from another
189 // scoped_ptr_impl.
190 template <typename U, typename V>
191 scoped_ptr_impl(scoped_ptr_impl<U, V>* other)
192 : data_(other->release(), other->get_deleter()) {
193 // We do not support move-only deleters. We could modify our move
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000194 // emulation to have rtc::subtle::move() and rtc::subtle::forward()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 // functions that are imperfect emulations of their C++11 equivalents,
196 // but until there's a requirement, just assume deleters are copyable.
197 }
198
199 template <typename U, typename V>
200 void TakeState(scoped_ptr_impl<U, V>* other) {
201 // See comment in templated constructor above regarding lack of support
202 // for move-only deleters.
203 reset(other->release());
204 get_deleter() = other->get_deleter();
205 }
206
207 ~scoped_ptr_impl() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000208 if (data_.ptr != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209 // Not using get_deleter() saves one function call in non-optimized
210 // builds.
211 static_cast<D&>(data_)(data_.ptr);
212 }
213 }
214
215 void reset(T* p) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000216 // This is a self-reset, which is no longer allowed for default deleters:
217 // https://crbug.com/162971
218 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219
220 // Note that running data_.ptr = p can lead to undefined behavior if
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000221 // get_deleter()(get()) deletes this. In order to prevent this, reset()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 // should update the stored pointer before deleting its old value.
223 //
224 // However, changing reset() to use that behavior may cause current code to
225 // break in unexpected ways. If the destruction of the owned object
226 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
227 // then it will incorrectly dispatch calls to |p| rather than the original
228 // value of |data_.ptr|.
229 //
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000230 // During the transition period, set the stored pointer to nullptr while
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231 // deleting the object. Eventually, this safety check will be removed to
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000232 // prevent the scenario initially described from occurring and
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233 // http://crbug.com/176091 can be closed.
234 T* old = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000235 data_.ptr = nullptr;
236 if (old != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237 static_cast<D&>(data_)(old);
238 data_.ptr = p;
239 }
240
241 T* get() const { return data_.ptr; }
242
243 D& get_deleter() { return data_; }
244 const D& get_deleter() const { return data_; }
245
246 void swap(scoped_ptr_impl& p2) {
247 // Standard swap idiom: 'using std::swap' ensures that std::swap is
248 // present in the overload set, but we call swap unqualified so that
249 // any more-specific overloads can be used, if available.
250 using std::swap;
251 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_));
252 swap(data_.ptr, p2.data_.ptr);
253 }
254
255 T* release() {
256 T* old_ptr = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000257 data_.ptr = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000258 return old_ptr;
259 }
260
261 T** accept() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000262 reset(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 return &(data_.ptr);
264 }
265
266 T** use() {
267 return &(data_.ptr);
268 }
269
270 private:
271 // Needed to allow type-converting constructor.
272 template <typename U, typename V> friend class scoped_ptr_impl;
273
274 // Use the empty base class optimization to allow us to have a D
275 // member, while avoiding any space overhead for it when D is an
276 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
277 // discussion of this technique.
278 struct Data : public D {
279 explicit Data(T* ptr_in) : ptr(ptr_in) {}
280 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {}
281 T* ptr;
282 };
283
284 Data data_;
285
henrikg3c089d72015-09-16 05:37:44 -0700286 RTC_DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287};
288
289} // namespace internal
290
291// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
292// automatically deletes the pointer it holds (if any).
293// That is, scoped_ptr<T> owns the T object that it points to.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000294// Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T
295// object. Also like T*, scoped_ptr<T> is thread-compatible, and once you
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296// dereference it, you get the thread safety guarantees of T.
297//
298// The size of scoped_ptr is small. On most compilers, when using the
299// DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
300// increase the size proportional to whatever state they need to have. See
301// comments inside scoped_ptr_impl<> for details.
302//
303// Current implementation targets having a strict subset of C++11's
304// unique_ptr<> features. Known deficiencies include not supporting move-only
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000305// deleters, function pointers as deleters, and deleters with reference
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000306// types.
307template <class T, class D = rtc::DefaultDeleter<T> >
308class scoped_ptr {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000309
310 // TODO(ajm): If we ever import RefCountedBase, this check needs to be
311 // enabled.
312 //static_assert(rtc::internal::IsNotRefCounted<T>::value,
313 // "T is refcounted type and needs scoped refptr");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314
315 public:
316 // The element and deleter types.
317 typedef T element_type;
318 typedef D deleter_type;
319
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000320 // Constructor. Defaults to initializing with nullptr.
321 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322
323 // Constructor. Takes ownership of p.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000324 explicit scoped_ptr(element_type* p) : impl_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325
326 // Constructor. Allows initialization of a stateful deleter.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000327 scoped_ptr(element_type* p, const D& d) : impl_(p, d) {}
328
329 // Constructor. Allows construction from a nullptr.
kwiberg9390f842015-12-17 06:20:27 -0800330 scoped_ptr(std::nullptr_t) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000331
332 // Constructor. Allows construction from a scoped_ptr rvalue for a
333 // convertible type and deleter.
334 //
335 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
336 // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
337 // has different post-conditions if D is a reference type. Since this
338 // implementation does not support deleters with reference type,
339 // we do not need a separate move constructor allowing us to avoid one
340 // use of SFINAE. You only need to care about this if you modify the
341 // implementation of scoped_ptr.
342 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000343 scoped_ptr(scoped_ptr<U, V>&& other)
344 : impl_(&other.impl_) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000345 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000346 }
347
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000348 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
349 // type and deleter.
350 //
351 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
352 // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
353 // form has different requirements on for move-only Deleters. Since this
354 // implementation does not support move-only Deleters, we do not need a
355 // separate move assignment operator allowing us to avoid one use of SFINAE.
356 // You only need to care about this if you modify the implementation of
357 // scoped_ptr.
358 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000359 scoped_ptr& operator=(scoped_ptr<U, V>&& rhs) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000360 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000361 impl_.TakeState(&rhs.impl_);
362 return *this;
363 }
364
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000365 // operator=. Allows assignment from a nullptr. Deletes the currently owned
366 // object, if any.
kwiberg9390f842015-12-17 06:20:27 -0800367 scoped_ptr& operator=(std::nullptr_t) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000368 reset();
369 return *this;
370 }
371
Karl Wiberga8e285d2015-04-22 19:44:19 +0200372 // Deleted copy constructor and copy assignment, to make the type move-only.
373 scoped_ptr(const scoped_ptr& other) = delete;
374 scoped_ptr& operator=(const scoped_ptr& other) = delete;
375
376 // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).)
377 scoped_ptr&& Pass() { return static_cast<scoped_ptr&&>(*this); }
378
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379 // Reset. Deletes the currently owned object, if any.
380 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000381 void reset(element_type* p = nullptr) { impl_.reset(p); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000382
383 // Accessors to get the owned object.
384 // operator* and operator-> will assert() if there is no current object.
385 element_type& operator*() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000386 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000387 return *impl_.get();
388 }
389 element_type* operator->() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000390 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000391 return impl_.get();
392 }
393 element_type* get() const { return impl_.get(); }
394
395 // Access to the deleter.
396 deleter_type& get_deleter() { return impl_.get_deleter(); }
397 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
398
399 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
400 // implicitly convertible to a real bool (which is dangerous).
401 //
402 // Note that this trick is only safe when the == and != operators
403 // are declared explicitly, as otherwise "scoped_ptr1 ==
404 // scoped_ptr2" will compile but do the wrong thing (i.e., convert
405 // to Testable and then do the comparison).
406 private:
407 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
408 scoped_ptr::*Testable;
409
410 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000411 operator Testable() const {
412 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
413 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000414
415 // Comparison operators.
416 // These return whether two scoped_ptr refer to the same object, not just to
417 // two different but equal objects.
418 bool operator==(const element_type* p) const { return impl_.get() == p; }
419 bool operator!=(const element_type* p) const { return impl_.get() != p; }
420
421 // Swap two scoped pointers.
422 void swap(scoped_ptr& p2) {
423 impl_.swap(p2.impl_);
424 }
425
426 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000427 // The return value is the current pointer held by this object. If this object
428 // holds a nullptr, the return value is nullptr. After this operation, this
429 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000430 element_type* release() WARN_UNUSED_RESULT {
431 return impl_.release();
432 }
433
434 // Delete the currently held pointer and return a pointer
435 // to allow overwriting of the current pointer address.
436 element_type** accept() WARN_UNUSED_RESULT {
437 return impl_.accept();
438 }
439
440 // Return a pointer to the current pointer address.
441 element_type** use() WARN_UNUSED_RESULT {
442 return impl_.use();
443 }
444
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000445 private:
446 // Needed to reach into |impl_| in the constructor.
447 template <typename U, typename V> friend class scoped_ptr;
448 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
449
450 // Forbidden for API compatibility with std::unique_ptr.
451 explicit scoped_ptr(int disallow_construction_from_null);
452
453 // Forbid comparison of scoped_ptr types. If U != T, it totally
454 // doesn't make sense, and if U == T, it still doesn't make sense
455 // because you should never have the same object owned by two different
456 // scoped_ptrs.
457 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
458 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
459};
460
461template <class T, class D>
462class scoped_ptr<T[], D> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000463 public:
464 // The element and deleter types.
465 typedef T element_type;
466 typedef D deleter_type;
467
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000468 // Constructor. Defaults to initializing with nullptr.
469 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000470
471 // Constructor. Stores the given array. Note that the argument's type
472 // must exactly match T*. In particular:
473 // - it cannot be a pointer to a type derived from T, because it is
474 // inherently unsafe in the general case to access an array through a
475 // pointer whose dynamic type does not match its static type (eg., if
476 // T and the derived types had different sizes access would be
477 // incorrectly calculated). Deletion is also always undefined
478 // (C++98 [expr.delete]p3). If you're doing this, fix your code.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000479 // - it cannot be const-qualified differently from T per unique_ptr spec
480 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
481 // to work around this may use implicit_cast<const T*>().
482 // However, because of the first bullet in this comment, users MUST
483 // NOT use implicit_cast<Base*>() to upcast the static type of the array.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000484 explicit scoped_ptr(element_type* array) : impl_(array) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000485
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000486 // Constructor. Allows construction from a nullptr.
kwiberg9390f842015-12-17 06:20:27 -0800487 scoped_ptr(std::nullptr_t) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000488
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000489 // Constructor. Allows construction from a scoped_ptr rvalue.
490 scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {}
491
492 // operator=. Allows assignment from a scoped_ptr rvalue.
493 scoped_ptr& operator=(scoped_ptr&& rhs) {
494 impl_.TakeState(&rhs.impl_);
495 return *this;
496 }
497
498 // operator=. Allows assignment from a nullptr. Deletes the currently owned
499 // array, if any.
kwiberg9390f842015-12-17 06:20:27 -0800500 scoped_ptr& operator=(std::nullptr_t) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000501 reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000502 return *this;
503 }
504
Karl Wiberga8e285d2015-04-22 19:44:19 +0200505 // Deleted copy constructor and copy assignment, to make the type move-only.
506 scoped_ptr(const scoped_ptr& other) = delete;
507 scoped_ptr& operator=(const scoped_ptr& other) = delete;
508
509 // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).)
510 scoped_ptr&& Pass() { return static_cast<scoped_ptr&&>(*this); }
511
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000512 // Reset. Deletes the currently owned array, if any.
513 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000514 void reset(element_type* array = nullptr) { impl_.reset(array); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000515
516 // Accessors to get the owned array.
517 element_type& operator[](size_t i) const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000518 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000519 return impl_.get()[i];
520 }
521 element_type* get() const { return impl_.get(); }
522
523 // Access to the deleter.
524 deleter_type& get_deleter() { return impl_.get_deleter(); }
525 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
526
527 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
528 // implicitly convertible to a real bool (which is dangerous).
529 private:
530 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
531 scoped_ptr::*Testable;
532
533 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000534 operator Testable() const {
535 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
536 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000537
538 // Comparison operators.
539 // These return whether two scoped_ptr refer to the same object, not just to
540 // two different but equal objects.
541 bool operator==(element_type* array) const { return impl_.get() == array; }
542 bool operator!=(element_type* array) const { return impl_.get() != array; }
543
544 // Swap two scoped pointers.
545 void swap(scoped_ptr& p2) {
546 impl_.swap(p2.impl_);
547 }
548
549 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000550 // The return value is the current pointer held by this object. If this object
551 // holds a nullptr, the return value is nullptr. After this operation, this
552 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000553 element_type* release() WARN_UNUSED_RESULT {
554 return impl_.release();
555 }
556
557 // Delete the currently held pointer and return a pointer
558 // to allow overwriting of the current pointer address.
559 element_type** accept() WARN_UNUSED_RESULT {
560 return impl_.accept();
561 }
562
563 // Return a pointer to the current pointer address.
564 element_type** use() WARN_UNUSED_RESULT {
565 return impl_.use();
566 }
567
568 private:
569 // Force element_type to be a complete type.
570 enum { type_must_be_complete = sizeof(element_type) };
571
572 // Actually hold the data.
573 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
574
575 // Disable initialization from any type other than element_type*, by
576 // providing a constructor that matches such an initialization, but is
577 // private and has no definition. This is disabled because it is not safe to
578 // call delete[] on an array whose static type does not match its dynamic
579 // type.
580 template <typename U> explicit scoped_ptr(U* array);
581 explicit scoped_ptr(int disallow_construction_from_null);
582
583 // Disable reset() from any type other than element_type*, for the same
584 // reasons as the constructor above.
585 template <typename U> void reset(U* array);
586 void reset(int disallow_reset_from_null);
587
588 // Forbid comparison of scoped_ptr types. If U != T, it totally
589 // doesn't make sense, and if U == T, it still doesn't make sense
590 // because you should never have the same object owned by two different
591 // scoped_ptrs.
592 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
593 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
594};
595
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000596template <class T, class D>
597void swap(rtc::scoped_ptr<T, D>& p1, rtc::scoped_ptr<T, D>& p2) {
598 p1.swap(p2);
599}
600
Karl Wiberg94784372015-04-20 14:03:07 +0200601} // namespace rtc
602
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000603template <class T, class D>
604bool operator==(T* p1, const rtc::scoped_ptr<T, D>& p2) {
605 return p1 == p2.get();
606}
607
608template <class T, class D>
609bool operator!=(T* p1, const rtc::scoped_ptr<T, D>& p2) {
610 return p1 != p2.get();
611}
612
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000613// A function to convert T* into scoped_ptr<T>
614// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
615// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
616template <typename T>
617rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) {
618 return rtc::scoped_ptr<T>(ptr);
619}
620
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000621#endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__