Update base/scoped_ptr.h from system_wrappers/interface/scoped_ptr.h

The latter file was more up-to-date. The files are now identical
with the following exceptions:

  * The namespace used (rtc vs. webrtc).

  * The name of the include guard.

  * base/scoped_ptr.h still has two extra methods, accept() and use().

  * base/scoped_ptr.h still includes webrtc/base/common.h even though
    it doesn't need it itself, since several .cc files expect to get
    it for free by incuding base/scoped_ptr.h. This is of course bad
    manners, and the "unused" include will be removed in a future CL.

A later CL will remove system_wrappers/interface/scoped_ptr.h.

R=andrew@webrtc.org

Committed: https://code.google.com/p/webrtc/source/detail?r=8147
And reverted again, because out-of-tree code using this file was defining nullptr to 0: https://code.google.com/p/webrtc/source/detail?r=8149

Review URL: https://webrtc-codereview.appspot.com/36919004

Cr-Commit-Position: refs/heads/master@{#8196}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8196 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kwiberg@webrtc.org 2015-01-29 09:12:47 +00:00
parent 43c883954f
commit 73ca1945ec

View File

@ -8,10 +8,12 @@
* be found in the AUTHORS file in the root of the source tree.
*/
// Scopers help you manage ownership of a pointer, helping you easily manage the
// a pointer within a scope, and automatically destroying the pointer at the
// end of a scope. There are two main classes you will use, which correspond
// to the operators new/delete and new[]/delete[].
// Borrowed from Chromium's src/base/memory/scoped_ptr.h.
// Scopers help you manage ownership of a pointer, helping you easily manage a
// pointer within a scope, and automatically destroying the pointer at the end
// of a scope. There are two main classes you will use, which correspond to the
// operators new/delete and new[]/delete[].
//
// Example usage (scoped_ptr<T>):
// {
@ -64,7 +66,7 @@
// TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay").
// scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo.
// scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
// PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL.
// PassThru(ptr2.Pass()); // ptr2 is correspondingly nullptr.
// }
//
// Notice that if you do not call Pass() when returning from PassThru(), or
@ -93,18 +95,20 @@
#ifndef WEBRTC_BASE_SCOPED_PTR_H__
#define WEBRTC_BASE_SCOPED_PTR_H__
#include <stddef.h> // for ptrdiff_t
#include <stdlib.h> // for free() decl
// This is an implementation designed to match the anticipated future TR2
// implementation of the scoped_ptr class.
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <algorithm> // For std::swap().
#include "webrtc/base/common.h" // for ASSERT
#include "webrtc/base/move.h" // for RTC_MOVE_ONLY_TYPE_FOR_CPP_03
#include "webrtc/base/template_util.h" // for is_convertible, is_array
#ifdef WEBRTC_WIN
namespace std { using ::ptrdiff_t; };
#endif // WEBRTC_WIN
#include "webrtc/base/common.h"
#include "webrtc/base/constructormagic.h"
#include "webrtc/base/move.h"
#include "webrtc/base/template_util.h"
#include "webrtc/typedefs.h"
namespace rtc {
@ -176,6 +180,18 @@ struct FreeDeleter {
namespace internal {
template <typename T>
struct ShouldAbortOnSelfReset {
template <typename U>
static rtc::internal::NoType Test(const typename U::AllowSelfReset*);
template <typename U>
static rtc::internal::YesType Test(...);
static const bool value =
sizeof(Test<T>(0)) == sizeof(rtc::internal::YesType);
};
// Minimal implementation of the core logic of scoped_ptr, suitable for
// reuse in both scoped_ptr and its specializations.
template <class T, class D>
@ -192,8 +208,7 @@ class scoped_ptr_impl {
scoped_ptr_impl(scoped_ptr_impl<U, V>* other)
: data_(other->release(), other->get_deleter()) {
// We do not support move-only deleters. We could modify our move
// emulation to have rtc::subtle::move() and
// rtc::subtle::forward()
// emulation to have rtc::subtle::move() and rtc::subtle::forward()
// functions that are imperfect emulations of their C++11 equivalents,
// but until there's a requirement, just assume deleters are copyable.
}
@ -207,7 +222,7 @@ class scoped_ptr_impl {
}
~scoped_ptr_impl() {
if (data_.ptr != NULL) {
if (data_.ptr != nullptr) {
// Not using get_deleter() saves one function call in non-optimized
// builds.
static_cast<D&>(data_)(data_.ptr);
@ -215,12 +230,12 @@ class scoped_ptr_impl {
}
void reset(T* p) {
// This is a self-reset, which is no longer allowed: http://crbug.com/162971
if (p != NULL && p == data_.ptr)
abort();
// This is a self-reset, which is no longer allowed for default deleters:
// https://crbug.com/162971
assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr);
// Note that running data_.ptr = p can lead to undefined behavior if
// get_deleter()(get()) deletes this. In order to pevent this, reset()
// get_deleter()(get()) deletes this. In order to prevent this, reset()
// should update the stored pointer before deleting its old value.
//
// However, changing reset() to use that behavior may cause current code to
@ -229,13 +244,13 @@ class scoped_ptr_impl {
// then it will incorrectly dispatch calls to |p| rather than the original
// value of |data_.ptr|.
//
// During the transition period, set the stored pointer to NULL while
// During the transition period, set the stored pointer to nullptr while
// deleting the object. Eventually, this safety check will be removed to
// prevent the scenario initially described from occuring and
// prevent the scenario initially described from occurring and
// http://crbug.com/176091 can be closed.
T* old = data_.ptr;
data_.ptr = NULL;
if (old != NULL)
data_.ptr = nullptr;
if (old != nullptr)
static_cast<D&>(data_)(old);
data_.ptr = p;
}
@ -256,12 +271,12 @@ class scoped_ptr_impl {
T* release() {
T* old_ptr = data_.ptr;
data_.ptr = NULL;
data_.ptr = nullptr;
return old_ptr;
}
T** accept() {
reset(NULL);
reset(nullptr);
return &(data_.ptr);
}
@ -293,8 +308,8 @@ class scoped_ptr_impl {
// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
// automatically deletes the pointer it holds (if any).
// That is, scoped_ptr<T> owns the T object that it points to.
// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
// Also like T*, scoped_ptr<T> is thread-compatible, and once you
// Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T
// object. Also like T*, scoped_ptr<T> is thread-compatible, and once you
// dereference it, you get the thread safety guarantees of T.
//
// The size of scoped_ptr is small. On most compilers, when using the
@ -304,19 +319,24 @@ class scoped_ptr_impl {
//
// Current implementation targets having a strict subset of C++11's
// unique_ptr<> features. Known deficiencies include not supporting move-only
// deleteres, function pointers as deleters, and deleters with reference
// deleters, function pointers as deleters, and deleters with reference
// types.
template <class T, class D = rtc::DefaultDeleter<T> >
class scoped_ptr {
RTC_MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
RTC_MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_ptr)
// TODO(ajm): If we ever import RefCountedBase, this check needs to be
// enabled.
//static_assert(rtc::internal::IsNotRefCounted<T>::value,
// "T is refcounted type and needs scoped refptr");
public:
// The element and deleter types.
typedef T element_type;
typedef D deleter_type;
// Constructor. Defaults to initializing with NULL.
scoped_ptr() : impl_(NULL) { }
// Constructor. Defaults to initializing with nullptr.
scoped_ptr() : impl_(nullptr) {}
// Constructor. Takes ownership of p.
explicit scoped_ptr(element_type* p) : impl_(p) {}
@ -324,6 +344,9 @@ class scoped_ptr {
// Constructor. Allows initialization of a stateful deleter.
scoped_ptr(element_type* p, const D& d) : impl_(p, d) {}
// Constructor. Allows construction from a nullptr.
scoped_ptr(decltype(nullptr)) : impl_(nullptr) {}
// Constructor. Allows construction from a scoped_ptr rvalue for a
// convertible type and deleter.
//
@ -335,13 +358,11 @@ class scoped_ptr {
// use of SFINAE. You only need to care about this if you modify the
// implementation of scoped_ptr.
template <typename U, typename V>
scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) {
scoped_ptr(scoped_ptr<U, V>&& other)
: impl_(&other.impl_) {
static_assert(!rtc::is_array<U>::value, "U cannot be an array");
}
// Constructor. Move constructor for C++03 move emulation of this type.
scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { }
// operator=. Allows assignment from a scoped_ptr rvalue for a convertible
// type and deleter.
//
@ -353,24 +374,31 @@ class scoped_ptr {
// You only need to care about this if you modify the implementation of
// scoped_ptr.
template <typename U, typename V>
scoped_ptr& operator=(scoped_ptr<U, V> rhs) {
scoped_ptr& operator=(scoped_ptr<U, V>&& rhs) {
static_assert(!rtc::is_array<U>::value, "U cannot be an array");
impl_.TakeState(&rhs.impl_);
return *this;
}
// operator=. Allows assignment from a nullptr. Deletes the currently owned
// object, if any.
scoped_ptr& operator=(decltype(nullptr)) {
reset();
return *this;
}
// Reset. Deletes the currently owned object, if any.
// Then takes ownership of a new object, if given.
void reset(element_type* p = NULL) { impl_.reset(p); }
void reset(element_type* p = nullptr) { impl_.reset(p); }
// Accessors to get the owned object.
// operator* and operator-> will assert() if there is no current object.
element_type& operator*() const {
ASSERT(impl_.get() != NULL);
assert(impl_.get() != nullptr);
return *impl_.get();
}
element_type* operator->() const {
ASSERT(impl_.get() != NULL);
assert(impl_.get() != nullptr);
return impl_.get();
}
element_type* get() const { return impl_.get(); }
@ -391,7 +419,9 @@ class scoped_ptr {
scoped_ptr::*Testable;
public:
operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
operator Testable() const {
return impl_.get() ? &scoped_ptr::impl_ : nullptr;
}
// Comparison operators.
// These return whether two scoped_ptr refer to the same object, not just to
@ -405,10 +435,9 @@ class scoped_ptr {
}
// Release a pointer.
// The return value is the current pointer held by this object.
// If this object holds a NULL pointer, the return value is NULL.
// After this operation, this object will hold a NULL pointer,
// and will not own the object any more.
// The return value is the current pointer held by this object. If this object
// holds a nullptr, the return value is nullptr. After this operation, this
// object will hold a nullptr, and will not own the object any more.
element_type* release() WARN_UNUSED_RESULT {
return impl_.release();
}
@ -424,17 +453,6 @@ class scoped_ptr {
return impl_.use();
}
// C++98 doesn't support functions templates with default parameters which
// makes it hard to write a PassAs() that understands converting the deleter
// while preserving simple calling semantics.
//
// Until there is a use case for PassAs() with custom deleters, just ignore
// the custom deleter.
template <typename PassAsType>
scoped_ptr<PassAsType> PassAs() {
return scoped_ptr<PassAsType>(Pass());
}
private:
// Needed to reach into |impl_| in the constructor.
template <typename U, typename V> friend class scoped_ptr;
@ -453,15 +471,15 @@ class scoped_ptr {
template <class T, class D>
class scoped_ptr<T[], D> {
RTC_MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
RTC_MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(scoped_ptr)
public:
// The element and deleter types.
typedef T element_type;
typedef D deleter_type;
// Constructor. Defaults to initializing with NULL.
scoped_ptr() : impl_(NULL) { }
// Constructor. Defaults to initializing with nullptr.
scoped_ptr() : impl_(nullptr) {}
// Constructor. Stores the given array. Note that the argument's type
// must exactly match T*. In particular:
@ -471,9 +489,6 @@ class scoped_ptr<T[], D> {
// T and the derived types had different sizes access would be
// incorrectly calculated). Deletion is also always undefined
// (C++98 [expr.delete]p3). If you're doing this, fix your code.
// - it cannot be NULL, because NULL is an integral expression, not a
// pointer to T. Use the no-argument version instead of explicitly
// passing NULL.
// - it cannot be const-qualified differently from T per unique_ptr spec
// (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
// to work around this may use implicit_cast<const T*>().
@ -481,22 +496,32 @@ class scoped_ptr<T[], D> {
// NOT use implicit_cast<Base*>() to upcast the static type of the array.
explicit scoped_ptr(element_type* array) : impl_(array) {}
// Constructor. Move constructor for C++03 move emulation of this type.
scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { }
// Constructor. Allows construction from a nullptr.
scoped_ptr(decltype(nullptr)) : impl_(nullptr) {}
// operator=. Move operator= for C++03 move emulation of this type.
scoped_ptr& operator=(RValue rhs) {
impl_.TakeState(&rhs.object->impl_);
// Constructor. Allows construction from a scoped_ptr rvalue.
scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {}
// operator=. Allows assignment from a scoped_ptr rvalue.
scoped_ptr& operator=(scoped_ptr&& rhs) {
impl_.TakeState(&rhs.impl_);
return *this;
}
// operator=. Allows assignment from a nullptr. Deletes the currently owned
// array, if any.
scoped_ptr& operator=(decltype(nullptr)) {
reset();
return *this;
}
// Reset. Deletes the currently owned array, if any.
// Then takes ownership of a new object, if given.
void reset(element_type* array = NULL) { impl_.reset(array); }
void reset(element_type* array = nullptr) { impl_.reset(array); }
// Accessors to get the owned array.
element_type& operator[](size_t i) const {
ASSERT(impl_.get() != NULL);
assert(impl_.get() != nullptr);
return impl_.get()[i];
}
element_type* get() const { return impl_.get(); }
@ -512,7 +537,9 @@ class scoped_ptr<T[], D> {
scoped_ptr::*Testable;
public:
operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
operator Testable() const {
return impl_.get() ? &scoped_ptr::impl_ : nullptr;
}
// Comparison operators.
// These return whether two scoped_ptr refer to the same object, not just to
@ -526,10 +553,9 @@ class scoped_ptr<T[], D> {
}
// Release a pointer.
// The return value is the current pointer held by this object.
// If this object holds a NULL pointer, the return value is NULL.
// After this operation, this object will hold a NULL pointer,
// and will not own the object any more.
// The return value is the current pointer held by this object. If this object
// holds a nullptr, the return value is nullptr. After this operation, this
// object will hold a nullptr, and will not own the object any more.
element_type* release() WARN_UNUSED_RESULT {
return impl_.release();
}
@ -575,7 +601,6 @@ class scoped_ptr<T[], D> {
} // namespace rtc
// Free functions
template <class T, class D>
void swap(rtc::scoped_ptr<T, D>& p1, rtc::scoped_ptr<T, D>& p2) {
p1.swap(p2);
@ -591,4 +616,12 @@ bool operator!=(T* p1, const rtc::scoped_ptr<T, D>& p2) {
return p1 != p2.get();
}
// A function to convert T* into scoped_ptr<T>
// Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
// for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
template <typename T>
rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) {
return rtc::scoped_ptr<T>(ptr);
}
#endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__