blob: 955bcfeab4907d3f45a64e2e0b50b98dd18addc6 [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>
kwibergb7f89d62016-02-17 10:04:18 -080091#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000093#include "webrtc/base/constructormagic.h"
kwiberg36220ae2016-01-12 07:24:19 -080094#include "webrtc/base/deprecation.h"
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +000095#include "webrtc/base/template_util.h"
96#include "webrtc/typedefs.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097
98namespace rtc {
99
100// Function object which deletes its parameter, which must be a pointer.
101// If C is an array type, invokes 'delete[]' on the parameter; otherwise,
102// invokes 'delete'. The default deleter for scoped_ptr<T>.
103template <class T>
104struct DefaultDeleter {
105 DefaultDeleter() {}
106 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) {
107 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor
108 // if U* is implicitly convertible to T* and U is not an array type.
109 //
110 // Correct implementation should use SFINAE to disable this
111 // constructor. However, since there are no other 1-argument constructors,
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000112 // using a static_assert based on is_convertible<> and requiring
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 // complete types is simpler and will cause compile failures for equivalent
114 // misuses.
115 //
116 // Note, the is_convertible<U*, T*> check also ensures that U is not an
117 // array. T is guaranteed to be a non-array, so any U* where U is an array
118 // cannot convert to T*.
119 enum { T_must_be_complete = sizeof(T) };
120 enum { U_must_be_complete = sizeof(U) };
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000121 static_assert(rtc::is_convertible<U*, T*>::value,
122 "U* must implicitly convert to T*");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 }
124 inline void operator()(T* ptr) const {
125 enum { type_must_be_complete = sizeof(T) };
126 delete ptr;
127 }
128};
129
130// Specialization of DefaultDeleter for array types.
131template <class T>
132struct DefaultDeleter<T[]> {
133 inline void operator()(T* ptr) const {
134 enum { type_must_be_complete = sizeof(T) };
135 delete[] ptr;
136 }
137
138 private:
139 // Disable this operator for any U != T because it is undefined to execute
140 // an array delete when the static type of the array mismatches the dynamic
141 // type.
142 //
143 // References:
144 // C++98 [expr.delete]p3
145 // http://cplusplus.github.com/LWG/lwg-defects.html#938
146 template <typename U> void operator()(U* array) const;
147};
148
149template <class T, int n>
150struct DefaultDeleter<T[n]> {
151 // Never allow someone to declare something like scoped_ptr<int[10]>.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000152 static_assert(sizeof(T) == -1, "do not use array with size as type");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153};
154
155// Function object which invokes 'free' on its parameter, which must be
156// a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
157//
158// scoped_ptr<int, rtc::FreeDeleter> foo_ptr(
159// static_cast<int*>(malloc(sizeof(int))));
160struct FreeDeleter {
161 inline void operator()(void* ptr) const {
162 free(ptr);
163 }
164};
165
166namespace internal {
167
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000168template <typename T>
169struct ShouldAbortOnSelfReset {
170 template <typename U>
171 static rtc::internal::NoType Test(const typename U::AllowSelfReset*);
172
173 template <typename U>
174 static rtc::internal::YesType Test(...);
175
176 static const bool value =
177 sizeof(Test<T>(0)) == sizeof(rtc::internal::YesType);
178};
179
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180// Minimal implementation of the core logic of scoped_ptr, suitable for
181// reuse in both scoped_ptr and its specializations.
182template <class T, class D>
183class scoped_ptr_impl {
184 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000185 explicit scoped_ptr_impl(T* p) : data_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186
187 // Initializer for deleters that have data parameters.
188 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {}
189
190 // Templated constructor that destructively takes the value from another
191 // scoped_ptr_impl.
192 template <typename U, typename V>
193 scoped_ptr_impl(scoped_ptr_impl<U, V>* other)
194 : data_(other->release(), other->get_deleter()) {
195 // We do not support move-only deleters. We could modify our move
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000196 // emulation to have rtc::subtle::move() and rtc::subtle::forward()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197 // functions that are imperfect emulations of their C++11 equivalents,
198 // but until there's a requirement, just assume deleters are copyable.
199 }
200
201 template <typename U, typename V>
202 void TakeState(scoped_ptr_impl<U, V>* other) {
203 // See comment in templated constructor above regarding lack of support
204 // for move-only deleters.
205 reset(other->release());
206 get_deleter() = other->get_deleter();
207 }
208
209 ~scoped_ptr_impl() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000210 if (data_.ptr != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 // Not using get_deleter() saves one function call in non-optimized
212 // builds.
213 static_cast<D&>(data_)(data_.ptr);
214 }
215 }
216
217 void reset(T* p) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000218 // This is a self-reset, which is no longer allowed for default deleters:
219 // https://crbug.com/162971
220 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221
222 // Note that running data_.ptr = p can lead to undefined behavior if
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000223 // get_deleter()(get()) deletes this. In order to prevent this, reset()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224 // should update the stored pointer before deleting its old value.
225 //
226 // However, changing reset() to use that behavior may cause current code to
227 // break in unexpected ways. If the destruction of the owned object
228 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
229 // then it will incorrectly dispatch calls to |p| rather than the original
230 // value of |data_.ptr|.
231 //
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000232 // During the transition period, set the stored pointer to nullptr while
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233 // deleting the object. Eventually, this safety check will be removed to
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000234 // prevent the scenario initially described from occurring and
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 // http://crbug.com/176091 can be closed.
236 T* old = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000237 data_.ptr = nullptr;
238 if (old != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 static_cast<D&>(data_)(old);
240 data_.ptr = p;
241 }
242
243 T* get() const { return data_.ptr; }
244
245 D& get_deleter() { return data_; }
246 const D& get_deleter() const { return data_; }
247
248 void swap(scoped_ptr_impl& p2) {
249 // Standard swap idiom: 'using std::swap' ensures that std::swap is
250 // present in the overload set, but we call swap unqualified so that
251 // any more-specific overloads can be used, if available.
252 using std::swap;
253 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_));
254 swap(data_.ptr, p2.data_.ptr);
255 }
256
257 T* release() {
258 T* old_ptr = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000259 data_.ptr = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260 return old_ptr;
261 }
262
263 T** accept() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000264 reset(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265 return &(data_.ptr);
266 }
267
268 T** use() {
269 return &(data_.ptr);
270 }
271
272 private:
273 // Needed to allow type-converting constructor.
274 template <typename U, typename V> friend class scoped_ptr_impl;
275
276 // Use the empty base class optimization to allow us to have a D
277 // member, while avoiding any space overhead for it when D is an
278 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
279 // discussion of this technique.
280 struct Data : public D {
281 explicit Data(T* ptr_in) : ptr(ptr_in) {}
282 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {}
283 T* ptr;
284 };
285
286 Data data_;
287
henrikg3c089d72015-09-16 05:37:44 -0700288 RTC_DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289};
290
291} // namespace internal
292
293// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
294// automatically deletes the pointer it holds (if any).
295// That is, scoped_ptr<T> owns the T object that it points to.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000296// Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T
297// object. Also like T*, scoped_ptr<T> is thread-compatible, and once you
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298// dereference it, you get the thread safety guarantees of T.
299//
300// The size of scoped_ptr is small. On most compilers, when using the
301// DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
302// increase the size proportional to whatever state they need to have. See
303// comments inside scoped_ptr_impl<> for details.
304//
305// Current implementation targets having a strict subset of C++11's
306// unique_ptr<> features. Known deficiencies include not supporting move-only
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000307// deleters, function pointers as deleters, and deleters with reference
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308// types.
309template <class T, class D = rtc::DefaultDeleter<T> >
310class scoped_ptr {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000311
312 // TODO(ajm): If we ever import RefCountedBase, this check needs to be
313 // enabled.
314 //static_assert(rtc::internal::IsNotRefCounted<T>::value,
315 // "T is refcounted type and needs scoped refptr");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316
317 public:
318 // The element and deleter types.
319 typedef T element_type;
320 typedef D deleter_type;
321
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000322 // Constructor. Defaults to initializing with nullptr.
323 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324
325 // Constructor. Takes ownership of p.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000326 explicit scoped_ptr(element_type* p) : impl_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327
328 // Constructor. Allows initialization of a stateful deleter.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000329 scoped_ptr(element_type* p, const D& d) : impl_(p, d) {}
330
331 // Constructor. Allows construction from a nullptr.
kwiberg9390f842015-12-17 06:20:27 -0800332 scoped_ptr(std::nullptr_t) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333
334 // Constructor. Allows construction from a scoped_ptr rvalue for a
335 // convertible type and deleter.
336 //
337 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
338 // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
339 // has different post-conditions if D is a reference type. Since this
340 // implementation does not support deleters with reference type,
341 // we do not need a separate move constructor allowing us to avoid one
342 // use of SFINAE. You only need to care about this if you modify the
343 // implementation of scoped_ptr.
344 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000345 scoped_ptr(scoped_ptr<U, V>&& other)
346 : impl_(&other.impl_) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000347 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000348 }
349
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000350 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
351 // type and deleter.
352 //
353 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
354 // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
355 // form has different requirements on for move-only Deleters. Since this
356 // implementation does not support move-only Deleters, we do not need a
357 // separate move assignment operator allowing us to avoid one use of SFINAE.
358 // You only need to care about this if you modify the implementation of
359 // scoped_ptr.
360 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000361 scoped_ptr& operator=(scoped_ptr<U, V>&& rhs) {
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 impl_.TakeState(&rhs.impl_);
364 return *this;
365 }
366
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000367 // operator=. Allows assignment from a nullptr. Deletes the currently owned
368 // object, if any.
kwiberg9390f842015-12-17 06:20:27 -0800369 scoped_ptr& operator=(std::nullptr_t) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000370 reset();
371 return *this;
372 }
373
Karl Wiberga8e285d2015-04-22 19:44:19 +0200374 // Deleted copy constructor and copy assignment, to make the type move-only.
375 scoped_ptr(const scoped_ptr& other) = delete;
376 scoped_ptr& operator=(const scoped_ptr& other) = delete;
377
378 // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).)
kwiberg36220ae2016-01-12 07:24:19 -0800379 // Deprecated; remove in March 2016 (bug 5373).
380 RTC_DEPRECATED scoped_ptr&& Pass() {
381 return std::move(*this);
382 }
Karl Wiberga8e285d2015-04-22 19:44:19 +0200383
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384 // Reset. Deletes the currently owned object, if any.
385 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000386 void reset(element_type* p = nullptr) { impl_.reset(p); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000387
388 // Accessors to get the owned object.
389 // operator* and operator-> will assert() if there is no current object.
390 element_type& operator*() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000391 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000392 return *impl_.get();
393 }
394 element_type* operator->() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000395 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000396 return impl_.get();
397 }
398 element_type* get() const { return impl_.get(); }
399
400 // Access to the deleter.
401 deleter_type& get_deleter() { return impl_.get_deleter(); }
402 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
403
404 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
405 // implicitly convertible to a real bool (which is dangerous).
406 //
407 // Note that this trick is only safe when the == and != operators
408 // are declared explicitly, as otherwise "scoped_ptr1 ==
409 // scoped_ptr2" will compile but do the wrong thing (i.e., convert
410 // to Testable and then do the comparison).
411 private:
412 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
413 scoped_ptr::*Testable;
414
415 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000416 operator Testable() const {
417 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
418 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419
420 // Comparison operators.
421 // These return whether two scoped_ptr refer to the same object, not just to
422 // two different but equal objects.
423 bool operator==(const element_type* p) const { return impl_.get() == p; }
424 bool operator!=(const element_type* p) const { return impl_.get() != p; }
425
426 // Swap two scoped pointers.
427 void swap(scoped_ptr& p2) {
428 impl_.swap(p2.impl_);
429 }
430
431 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000432 // The return value is the current pointer held by this object. If this object
433 // holds a nullptr, the return value is nullptr. After this operation, this
434 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000435 element_type* release() WARN_UNUSED_RESULT {
436 return impl_.release();
437 }
438
439 // Delete the currently held pointer and return a pointer
440 // to allow overwriting of the current pointer address.
441 element_type** accept() WARN_UNUSED_RESULT {
442 return impl_.accept();
443 }
444
445 // Return a pointer to the current pointer address.
446 element_type** use() WARN_UNUSED_RESULT {
447 return impl_.use();
448 }
449
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000450 private:
451 // Needed to reach into |impl_| in the constructor.
452 template <typename U, typename V> friend class scoped_ptr;
453 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
454
455 // Forbidden for API compatibility with std::unique_ptr.
456 explicit scoped_ptr(int disallow_construction_from_null);
457
458 // Forbid comparison of scoped_ptr types. If U != T, it totally
459 // doesn't make sense, and if U == T, it still doesn't make sense
460 // because you should never have the same object owned by two different
461 // scoped_ptrs.
462 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
463 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
464};
465
466template <class T, class D>
467class scoped_ptr<T[], D> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000468 public:
469 // The element and deleter types.
470 typedef T element_type;
471 typedef D deleter_type;
472
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000473 // Constructor. Defaults to initializing with nullptr.
474 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000475
476 // Constructor. Stores the given array. Note that the argument's type
477 // must exactly match T*. In particular:
478 // - it cannot be a pointer to a type derived from T, because it is
479 // inherently unsafe in the general case to access an array through a
480 // pointer whose dynamic type does not match its static type (eg., if
481 // T and the derived types had different sizes access would be
482 // incorrectly calculated). Deletion is also always undefined
483 // (C++98 [expr.delete]p3). If you're doing this, fix your code.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000484 // - it cannot be const-qualified differently from T per unique_ptr spec
485 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
486 // to work around this may use implicit_cast<const T*>().
487 // However, because of the first bullet in this comment, users MUST
488 // NOT use implicit_cast<Base*>() to upcast the static type of the array.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000489 explicit scoped_ptr(element_type* array) : impl_(array) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000491 // Constructor. Allows construction from a nullptr.
kwiberg9390f842015-12-17 06:20:27 -0800492 scoped_ptr(std::nullptr_t) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000493
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000494 // Constructor. Allows construction from a scoped_ptr rvalue.
495 scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {}
496
497 // operator=. Allows assignment from a scoped_ptr rvalue.
498 scoped_ptr& operator=(scoped_ptr&& rhs) {
499 impl_.TakeState(&rhs.impl_);
500 return *this;
501 }
502
503 // operator=. Allows assignment from a nullptr. Deletes the currently owned
504 // array, if any.
kwiberg9390f842015-12-17 06:20:27 -0800505 scoped_ptr& operator=(std::nullptr_t) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000506 reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000507 return *this;
508 }
509
Karl Wiberga8e285d2015-04-22 19:44:19 +0200510 // Deleted copy constructor and copy assignment, to make the type move-only.
511 scoped_ptr(const scoped_ptr& other) = delete;
512 scoped_ptr& operator=(const scoped_ptr& other) = delete;
513
514 // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).)
kwiberg36220ae2016-01-12 07:24:19 -0800515 // Deprecated; remove in March 2016 (bug 5373).
516 RTC_DEPRECATED scoped_ptr&& Pass() {
517 return std::move(*this);
518 }
Karl Wiberga8e285d2015-04-22 19:44:19 +0200519
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000520 // Reset. Deletes the currently owned array, if any.
521 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000522 void reset(element_type* array = nullptr) { impl_.reset(array); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000523
524 // Accessors to get the owned array.
525 element_type& operator[](size_t i) const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000526 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000527 return impl_.get()[i];
528 }
529 element_type* get() const { return impl_.get(); }
530
531 // Access to the deleter.
532 deleter_type& get_deleter() { return impl_.get_deleter(); }
533 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
534
535 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
536 // implicitly convertible to a real bool (which is dangerous).
537 private:
538 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
539 scoped_ptr::*Testable;
540
541 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000542 operator Testable() const {
543 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
544 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000545
546 // Comparison operators.
547 // These return whether two scoped_ptr refer to the same object, not just to
548 // two different but equal objects.
549 bool operator==(element_type* array) const { return impl_.get() == array; }
550 bool operator!=(element_type* array) const { return impl_.get() != array; }
551
552 // Swap two scoped pointers.
553 void swap(scoped_ptr& p2) {
554 impl_.swap(p2.impl_);
555 }
556
557 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000558 // The return value is the current pointer held by this object. If this object
559 // holds a nullptr, the return value is nullptr. After this operation, this
560 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000561 element_type* release() WARN_UNUSED_RESULT {
562 return impl_.release();
563 }
564
565 // Delete the currently held pointer and return a pointer
566 // to allow overwriting of the current pointer address.
567 element_type** accept() WARN_UNUSED_RESULT {
568 return impl_.accept();
569 }
570
571 // Return a pointer to the current pointer address.
572 element_type** use() WARN_UNUSED_RESULT {
573 return impl_.use();
574 }
575
576 private:
577 // Force element_type to be a complete type.
578 enum { type_must_be_complete = sizeof(element_type) };
579
580 // Actually hold the data.
581 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
582
583 // Disable initialization from any type other than element_type*, by
584 // providing a constructor that matches such an initialization, but is
585 // private and has no definition. This is disabled because it is not safe to
586 // call delete[] on an array whose static type does not match its dynamic
587 // type.
588 template <typename U> explicit scoped_ptr(U* array);
589 explicit scoped_ptr(int disallow_construction_from_null);
590
591 // Disable reset() from any type other than element_type*, for the same
592 // reasons as the constructor above.
593 template <typename U> void reset(U* array);
594 void reset(int disallow_reset_from_null);
595
596 // Forbid comparison of scoped_ptr types. If U != T, it totally
597 // doesn't make sense, and if U == T, it still doesn't make sense
598 // because you should never have the same object owned by two different
599 // scoped_ptrs.
600 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
601 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
602};
603
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000604template <class T, class D>
605void swap(rtc::scoped_ptr<T, D>& p1, rtc::scoped_ptr<T, D>& p2) {
606 p1.swap(p2);
607}
608
kwibergb7f89d62016-02-17 10:04:18 -0800609// Convert between the most common kinds of scoped_ptr and unique_ptr.
610template <typename T>
611std::unique_ptr<T> ScopedToUnique(scoped_ptr<T> sp) {
612 return std::unique_ptr<T>(sp.release());
613}
614template <typename T>
615scoped_ptr<T> UniqueToScoped(std::unique_ptr<T> up) {
616 return scoped_ptr<T>(up.release());
617}
618
Karl Wiberg94784372015-04-20 14:03:07 +0200619} // namespace rtc
620
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000621template <class T, class D>
622bool operator==(T* p1, const rtc::scoped_ptr<T, D>& p2) {
623 return p1 == p2.get();
624}
625
626template <class T, class D>
627bool operator!=(T* p1, const rtc::scoped_ptr<T, D>& p2) {
628 return p1 != p2.get();
629}
630
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000631// A function to convert T* into scoped_ptr<T>
632// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
633// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
634template <typename T>
635rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) {
636 return rtc::scoped_ptr<T>(ptr);
637}
638
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000639#endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__