blob: 95e7f49d96ced772324ef5cc0a4113e19b7e44f2 [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/common.h"
108#include "webrtc/base/constructormagic.h"
109#include "webrtc/base/move.h"
110#include "webrtc/base/template_util.h"
111#include "webrtc/typedefs.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112
113namespace rtc {
114
115// Function object which deletes its parameter, which must be a pointer.
116// If C is an array type, invokes 'delete[]' on the parameter; otherwise,
117// invokes 'delete'. The default deleter for scoped_ptr<T>.
118template <class T>
119struct DefaultDeleter {
120 DefaultDeleter() {}
121 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) {
122 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor
123 // if U* is implicitly convertible to T* and U is not an array type.
124 //
125 // Correct implementation should use SFINAE to disable this
126 // constructor. However, since there are no other 1-argument constructors,
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000127 // using a static_assert based on is_convertible<> and requiring
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 // complete types is simpler and will cause compile failures for equivalent
129 // misuses.
130 //
131 // Note, the is_convertible<U*, T*> check also ensures that U is not an
132 // array. T is guaranteed to be a non-array, so any U* where U is an array
133 // cannot convert to T*.
134 enum { T_must_be_complete = sizeof(T) };
135 enum { U_must_be_complete = sizeof(U) };
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000136 static_assert(rtc::is_convertible<U*, T*>::value,
137 "U* must implicitly convert to T*");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 }
139 inline void operator()(T* ptr) const {
140 enum { type_must_be_complete = sizeof(T) };
141 delete ptr;
142 }
143};
144
145// Specialization of DefaultDeleter for array types.
146template <class T>
147struct DefaultDeleter<T[]> {
148 inline void operator()(T* ptr) const {
149 enum { type_must_be_complete = sizeof(T) };
150 delete[] ptr;
151 }
152
153 private:
154 // Disable this operator for any U != T because it is undefined to execute
155 // an array delete when the static type of the array mismatches the dynamic
156 // type.
157 //
158 // References:
159 // C++98 [expr.delete]p3
160 // http://cplusplus.github.com/LWG/lwg-defects.html#938
161 template <typename U> void operator()(U* array) const;
162};
163
164template <class T, int n>
165struct DefaultDeleter<T[n]> {
166 // Never allow someone to declare something like scoped_ptr<int[10]>.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000167 static_assert(sizeof(T) == -1, "do not use array with size as type");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168};
169
170// Function object which invokes 'free' on its parameter, which must be
171// a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
172//
173// scoped_ptr<int, rtc::FreeDeleter> foo_ptr(
174// static_cast<int*>(malloc(sizeof(int))));
175struct FreeDeleter {
176 inline void operator()(void* ptr) const {
177 free(ptr);
178 }
179};
180
181namespace internal {
182
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000183template <typename T>
184struct ShouldAbortOnSelfReset {
185 template <typename U>
186 static rtc::internal::NoType Test(const typename U::AllowSelfReset*);
187
188 template <typename U>
189 static rtc::internal::YesType Test(...);
190
191 static const bool value =
192 sizeof(Test<T>(0)) == sizeof(rtc::internal::YesType);
193};
194
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195// Minimal implementation of the core logic of scoped_ptr, suitable for
196// reuse in both scoped_ptr and its specializations.
197template <class T, class D>
198class scoped_ptr_impl {
199 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000200 explicit scoped_ptr_impl(T* p) : data_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201
202 // Initializer for deleters that have data parameters.
203 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {}
204
205 // Templated constructor that destructively takes the value from another
206 // scoped_ptr_impl.
207 template <typename U, typename V>
208 scoped_ptr_impl(scoped_ptr_impl<U, V>* other)
209 : data_(other->release(), other->get_deleter()) {
210 // We do not support move-only deleters. We could modify our move
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000211 // emulation to have rtc::subtle::move() and rtc::subtle::forward()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212 // functions that are imperfect emulations of their C++11 equivalents,
213 // but until there's a requirement, just assume deleters are copyable.
214 }
215
216 template <typename U, typename V>
217 void TakeState(scoped_ptr_impl<U, V>* other) {
218 // See comment in templated constructor above regarding lack of support
219 // for move-only deleters.
220 reset(other->release());
221 get_deleter() = other->get_deleter();
222 }
223
224 ~scoped_ptr_impl() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000225 if (data_.ptr != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 // Not using get_deleter() saves one function call in non-optimized
227 // builds.
228 static_cast<D&>(data_)(data_.ptr);
229 }
230 }
231
232 void reset(T* p) {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000233 // This is a self-reset, which is no longer allowed for default deleters:
234 // https://crbug.com/162971
235 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236
237 // Note that running data_.ptr = p can lead to undefined behavior if
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000238 // get_deleter()(get()) deletes this. In order to prevent this, reset()
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 // should update the stored pointer before deleting its old value.
240 //
241 // However, changing reset() to use that behavior may cause current code to
242 // break in unexpected ways. If the destruction of the owned object
243 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
244 // then it will incorrectly dispatch calls to |p| rather than the original
245 // value of |data_.ptr|.
246 //
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000247 // During the transition period, set the stored pointer to nullptr while
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248 // deleting the object. Eventually, this safety check will be removed to
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000249 // prevent the scenario initially described from occurring and
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000250 // http://crbug.com/176091 can be closed.
251 T* old = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000252 data_.ptr = nullptr;
253 if (old != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 static_cast<D&>(data_)(old);
255 data_.ptr = p;
256 }
257
258 T* get() const { return data_.ptr; }
259
260 D& get_deleter() { return data_; }
261 const D& get_deleter() const { return data_; }
262
263 void swap(scoped_ptr_impl& p2) {
264 // Standard swap idiom: 'using std::swap' ensures that std::swap is
265 // present in the overload set, but we call swap unqualified so that
266 // any more-specific overloads can be used, if available.
267 using std::swap;
268 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_));
269 swap(data_.ptr, p2.data_.ptr);
270 }
271
272 T* release() {
273 T* old_ptr = data_.ptr;
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000274 data_.ptr = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275 return old_ptr;
276 }
277
278 T** accept() {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000279 reset(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280 return &(data_.ptr);
281 }
282
283 T** use() {
284 return &(data_.ptr);
285 }
286
287 private:
288 // Needed to allow type-converting constructor.
289 template <typename U, typename V> friend class scoped_ptr_impl;
290
291 // Use the empty base class optimization to allow us to have a D
292 // member, while avoiding any space overhead for it when D is an
293 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
294 // discussion of this technique.
295 struct Data : public D {
296 explicit Data(T* ptr_in) : ptr(ptr_in) {}
297 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {}
298 T* ptr;
299 };
300
301 Data data_;
302
303 DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
304};
305
306} // namespace internal
307
308// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
309// automatically deletes the pointer it holds (if any).
310// That is, scoped_ptr<T> owns the T object that it points to.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000311// Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T
312// object. Also like T*, scoped_ptr<T> is thread-compatible, and once you
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000313// dereference it, you get the thread safety guarantees of T.
314//
315// The size of scoped_ptr is small. On most compilers, when using the
316// DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
317// increase the size proportional to whatever state they need to have. See
318// comments inside scoped_ptr_impl<> for details.
319//
320// Current implementation targets having a strict subset of C++11's
321// unique_ptr<> features. Known deficiencies include not supporting move-only
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000322// deleters, function pointers as deleters, and deleters with reference
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323// types.
324template <class T, class D = rtc::DefaultDeleter<T> >
325class scoped_ptr {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000326 RTC_MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_ptr)
327
328 // TODO(ajm): If we ever import RefCountedBase, this check needs to be
329 // enabled.
330 //static_assert(rtc::internal::IsNotRefCounted<T>::value,
331 // "T is refcounted type and needs scoped refptr");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000332
333 public:
334 // The element and deleter types.
335 typedef T element_type;
336 typedef D deleter_type;
337
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000338 // Constructor. Defaults to initializing with nullptr.
339 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340
341 // Constructor. Takes ownership of p.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000342 explicit scoped_ptr(element_type* p) : impl_(p) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343
344 // Constructor. Allows initialization of a stateful deleter.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000345 scoped_ptr(element_type* p, const D& d) : impl_(p, d) {}
346
347 // Constructor. Allows construction from a nullptr.
348 scoped_ptr(decltype(nullptr)) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349
350 // Constructor. Allows construction from a scoped_ptr rvalue for a
351 // convertible type and deleter.
352 //
353 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
354 // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
355 // has different post-conditions if D is a reference type. Since this
356 // implementation does not support deleters with reference type,
357 // we do not need a separate move constructor allowing us to avoid one
358 // use of SFINAE. You only need to care about this if you modify the
359 // implementation of scoped_ptr.
360 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000361 scoped_ptr(scoped_ptr<U, V>&& other)
362 : impl_(&other.impl_) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000363 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000364 }
365
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
367 // type and deleter.
368 //
369 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
370 // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
371 // form has different requirements on for move-only Deleters. Since this
372 // implementation does not support move-only Deleters, we do not need a
373 // separate move assignment operator allowing us to avoid one use of SFINAE.
374 // You only need to care about this if you modify the implementation of
375 // scoped_ptr.
376 template <typename U, typename V>
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000377 scoped_ptr& operator=(scoped_ptr<U, V>&& rhs) {
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000378 static_assert(!rtc::is_array<U>::value, "U cannot be an array");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379 impl_.TakeState(&rhs.impl_);
380 return *this;
381 }
382
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000383 // operator=. Allows assignment from a nullptr. Deletes the currently owned
384 // object, if any.
385 scoped_ptr& operator=(decltype(nullptr)) {
386 reset();
387 return *this;
388 }
389
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000390 // Reset. Deletes the currently owned object, if any.
391 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000392 void reset(element_type* p = nullptr) { impl_.reset(p); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000393
394 // Accessors to get the owned object.
395 // operator* and operator-> will assert() if there is no current object.
396 element_type& operator*() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000397 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000398 return *impl_.get();
399 }
400 element_type* operator->() const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000401 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 return impl_.get();
403 }
404 element_type* get() const { return impl_.get(); }
405
406 // Access to the deleter.
407 deleter_type& get_deleter() { return impl_.get_deleter(); }
408 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
409
410 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
411 // implicitly convertible to a real bool (which is dangerous).
412 //
413 // Note that this trick is only safe when the == and != operators
414 // are declared explicitly, as otherwise "scoped_ptr1 ==
415 // scoped_ptr2" will compile but do the wrong thing (i.e., convert
416 // to Testable and then do the comparison).
417 private:
418 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
419 scoped_ptr::*Testable;
420
421 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000422 operator Testable() const {
423 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
424 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425
426 // Comparison operators.
427 // These return whether two scoped_ptr refer to the same object, not just to
428 // two different but equal objects.
429 bool operator==(const element_type* p) const { return impl_.get() == p; }
430 bool operator!=(const element_type* p) const { return impl_.get() != p; }
431
432 // Swap two scoped pointers.
433 void swap(scoped_ptr& p2) {
434 impl_.swap(p2.impl_);
435 }
436
437 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000438 // The return value is the current pointer held by this object. If this object
439 // holds a nullptr, the return value is nullptr. After this operation, this
440 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000441 element_type* release() WARN_UNUSED_RESULT {
442 return impl_.release();
443 }
444
445 // Delete the currently held pointer and return a pointer
446 // to allow overwriting of the current pointer address.
447 element_type** accept() WARN_UNUSED_RESULT {
448 return impl_.accept();
449 }
450
451 // Return a pointer to the current pointer address.
452 element_type** use() WARN_UNUSED_RESULT {
453 return impl_.use();
454 }
455
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000456 private:
457 // Needed to reach into |impl_| in the constructor.
458 template <typename U, typename V> friend class scoped_ptr;
459 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
460
461 // Forbidden for API compatibility with std::unique_ptr.
462 explicit scoped_ptr(int disallow_construction_from_null);
463
464 // Forbid comparison of scoped_ptr types. If U != T, it totally
465 // doesn't make sense, and if U == T, it still doesn't make sense
466 // because you should never have the same object owned by two different
467 // scoped_ptrs.
468 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
469 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
470};
471
472template <class T, class D>
473class scoped_ptr<T[], D> {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000474 RTC_MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_ptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000475
476 public:
477 // The element and deleter types.
478 typedef T element_type;
479 typedef D deleter_type;
480
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000481 // Constructor. Defaults to initializing with nullptr.
482 scoped_ptr() : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000483
484 // Constructor. Stores the given array. Note that the argument's type
485 // must exactly match T*. In particular:
486 // - it cannot be a pointer to a type derived from T, because it is
487 // inherently unsafe in the general case to access an array through a
488 // pointer whose dynamic type does not match its static type (eg., if
489 // T and the derived types had different sizes access would be
490 // incorrectly calculated). Deletion is also always undefined
491 // (C++98 [expr.delete]p3). If you're doing this, fix your code.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000492 // - it cannot be const-qualified differently from T per unique_ptr spec
493 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
494 // to work around this may use implicit_cast<const T*>().
495 // However, because of the first bullet in this comment, users MUST
496 // NOT use implicit_cast<Base*>() to upcast the static type of the array.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000497 explicit scoped_ptr(element_type* array) : impl_(array) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000498
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000499 // Constructor. Allows construction from a nullptr.
500 scoped_ptr(decltype(nullptr)) : impl_(nullptr) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000501
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000502 // Constructor. Allows construction from a scoped_ptr rvalue.
503 scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {}
504
505 // operator=. Allows assignment from a scoped_ptr rvalue.
506 scoped_ptr& operator=(scoped_ptr&& rhs) {
507 impl_.TakeState(&rhs.impl_);
508 return *this;
509 }
510
511 // operator=. Allows assignment from a nullptr. Deletes the currently owned
512 // array, if any.
513 scoped_ptr& operator=(decltype(nullptr)) {
514 reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000515 return *this;
516 }
517
518 // Reset. Deletes the currently owned array, if any.
519 // Then takes ownership of a new object, if given.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000520 void reset(element_type* array = nullptr) { impl_.reset(array); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000521
522 // Accessors to get the owned array.
523 element_type& operator[](size_t i) const {
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000524 assert(impl_.get() != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000525 return impl_.get()[i];
526 }
527 element_type* get() const { return impl_.get(); }
528
529 // Access to the deleter.
530 deleter_type& get_deleter() { return impl_.get_deleter(); }
531 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
532
533 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
534 // implicitly convertible to a real bool (which is dangerous).
535 private:
536 typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type>
537 scoped_ptr::*Testable;
538
539 public:
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000540 operator Testable() const {
541 return impl_.get() ? &scoped_ptr::impl_ : nullptr;
542 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543
544 // Comparison operators.
545 // These return whether two scoped_ptr refer to the same object, not just to
546 // two different but equal objects.
547 bool operator==(element_type* array) const { return impl_.get() == array; }
548 bool operator!=(element_type* array) const { return impl_.get() != array; }
549
550 // Swap two scoped pointers.
551 void swap(scoped_ptr& p2) {
552 impl_.swap(p2.impl_);
553 }
554
555 // Release a pointer.
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000556 // The return value is the current pointer held by this object. If this object
557 // holds a nullptr, the return value is nullptr. After this operation, this
558 // object will hold a nullptr, and will not own the object any more.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000559 element_type* release() WARN_UNUSED_RESULT {
560 return impl_.release();
561 }
562
563 // Delete the currently held pointer and return a pointer
564 // to allow overwriting of the current pointer address.
565 element_type** accept() WARN_UNUSED_RESULT {
566 return impl_.accept();
567 }
568
569 // Return a pointer to the current pointer address.
570 element_type** use() WARN_UNUSED_RESULT {
571 return impl_.use();
572 }
573
574 private:
575 // Force element_type to be a complete type.
576 enum { type_must_be_complete = sizeof(element_type) };
577
578 // Actually hold the data.
579 rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
580
581 // Disable initialization from any type other than element_type*, by
582 // providing a constructor that matches such an initialization, but is
583 // private and has no definition. This is disabled because it is not safe to
584 // call delete[] on an array whose static type does not match its dynamic
585 // type.
586 template <typename U> explicit scoped_ptr(U* array);
587 explicit scoped_ptr(int disallow_construction_from_null);
588
589 // Disable reset() from any type other than element_type*, for the same
590 // reasons as the constructor above.
591 template <typename U> void reset(U* array);
592 void reset(int disallow_reset_from_null);
593
594 // Forbid comparison of scoped_ptr types. If U != T, it totally
595 // doesn't make sense, and if U == T, it still doesn't make sense
596 // because you should never have the same object owned by two different
597 // scoped_ptrs.
598 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
599 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
600};
601
602} // namespace rtc
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
609template <class T, class D>
610bool operator==(T* p1, const rtc::scoped_ptr<T, D>& p2) {
611 return p1 == p2.get();
612}
613
614template <class T, class D>
615bool operator!=(T* p1, const rtc::scoped_ptr<T, D>& p2) {
616 return p1 != p2.get();
617}
618
kwiberg@webrtc.org73ca1942015-01-29 09:12:47 +0000619// A function to convert T* into scoped_ptr<T>
620// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
621// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
622template <typename T>
623rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) {
624 return rtc::scoped_ptr<T>(ptr);
625}
626
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000627#endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__