diff options
Diffstat (limited to 'webrtc/base/scoped_ptr.h')
-rw-r--r-- | webrtc/base/scoped_ptr.h | 64 |
1 files changed, 28 insertions, 36 deletions
diff --git a/webrtc/base/scoped_ptr.h b/webrtc/base/scoped_ptr.h index 4266d052b1..c4603c3208 100644 --- a/webrtc/base/scoped_ptr.h +++ b/webrtc/base/scoped_ptr.h @@ -42,55 +42,39 @@ // } // // These scopers also implement part of the functionality of C++11 unique_ptr -// in that they are "movable but not copyable." You can use the scopers in -// the parameter and return types of functions to signify ownership transfer -// in to and out of a function. When calling a function that has a scoper -// as the argument type, it must be called with the result of an analogous -// scoper's Pass() function or another function that generates a temporary; -// passing by copy will NOT work. Here is an example using scoped_ptr: +// in that they are "movable but not copyable." You can use the scopers in the +// parameter and return types of functions to signify ownership transfer in to +// and out of a function. When calling a function that has a scoper as the +// argument type, it must be called with the result of calling std::move on an +// analogous scoper, or another function that generates a temporary; passing by +// copy will NOT work. Here is an example using scoped_ptr: // // void TakesOwnership(scoped_ptr<Foo> arg) { // // Do something with arg // } // scoped_ptr<Foo> CreateFoo() { -// // No need for calling Pass() because we are constructing a temporary +// // No need for calling std::move because we are constructing a temporary // // for the return value. // return scoped_ptr<Foo>(new Foo("new")); // } // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { -// return arg.Pass(); +// return std::move(arg); // } // // { // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). -// TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay"). +// TakesOwnership(std::move(ptr)); // 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 nullptr. +// PassThru(std::move(ptr2)); // ptr2 is correspondingly nullptr. // } // -// Notice that if you do not call Pass() when returning from PassThru(), or +// Notice that if you do not call std::move when returning from PassThru(), or // when invoking TakesOwnership(), the code will not compile because scopers // are not copyable; they only implement move semantics which require calling -// the Pass() function to signify a destructive transfer of state. CreateFoo() -// is different though because we are constructing a temporary on the return -// line and thus can avoid needing to call Pass(). -// -// Pass() properly handles upcast in initialization, i.e. you can use a -// scoped_ptr<Child> to initialize a scoped_ptr<Parent>: -// -// scoped_ptr<Foo> foo(new Foo()); -// scoped_ptr<FooParent> parent(foo.Pass()); -// -// PassAs<>() should be used to upcast return value in return statement: -// -// scoped_ptr<Foo> CreateFoo() { -// scoped_ptr<FooChild> result(new FooChild()); -// return result.PassAs<Foo>(); -// } -// -// Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for -// scoped_ptr<T[]>. This is because casting array pointers may not be safe. +// std::move to signify a destructive transfer of state. CreateFoo() is +// different though because we are constructing a temporary on the return line +// and thus can avoid needing to call std::move. #ifndef WEBRTC_BASE_SCOPED_PTR_H__ #define WEBRTC_BASE_SCOPED_PTR_H__ @@ -103,8 +87,10 @@ #include <stdlib.h> #include <algorithm> // For std::swap(). +#include <cstddef> #include "webrtc/base/constructormagic.h" +#include "webrtc/base/deprecation.h" #include "webrtc/base/template_util.h" #include "webrtc/typedefs.h" @@ -342,7 +328,7 @@ class scoped_ptr { scoped_ptr(element_type* p, const D& d) : impl_(p, d) {} // Constructor. Allows construction from a nullptr. - scoped_ptr(decltype(nullptr)) : impl_(nullptr) {} + scoped_ptr(std::nullptr_t) : impl_(nullptr) {} // Constructor. Allows construction from a scoped_ptr rvalue for a // convertible type and deleter. @@ -379,7 +365,7 @@ class scoped_ptr { // operator=. Allows assignment from a nullptr. Deletes the currently owned // object, if any. - scoped_ptr& operator=(decltype(nullptr)) { + scoped_ptr& operator=(std::nullptr_t) { reset(); return *this; } @@ -389,7 +375,10 @@ class scoped_ptr { scoped_ptr& operator=(const scoped_ptr& other) = delete; // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) - scoped_ptr&& Pass() { return static_cast<scoped_ptr&&>(*this); } + // Deprecated; remove in March 2016 (bug 5373). + RTC_DEPRECATED scoped_ptr&& Pass() { + return std::move(*this); + } // Reset. Deletes the currently owned object, if any. // Then takes ownership of a new object, if given. @@ -499,7 +488,7 @@ class scoped_ptr<T[], D> { explicit scoped_ptr(element_type* array) : impl_(array) {} // Constructor. Allows construction from a nullptr. - scoped_ptr(decltype(nullptr)) : impl_(nullptr) {} + scoped_ptr(std::nullptr_t) : impl_(nullptr) {} // Constructor. Allows construction from a scoped_ptr rvalue. scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {} @@ -512,7 +501,7 @@ class scoped_ptr<T[], D> { // operator=. Allows assignment from a nullptr. Deletes the currently owned // array, if any. - scoped_ptr& operator=(decltype(nullptr)) { + scoped_ptr& operator=(std::nullptr_t) { reset(); return *this; } @@ -522,7 +511,10 @@ class scoped_ptr<T[], D> { scoped_ptr& operator=(const scoped_ptr& other) = delete; // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) - scoped_ptr&& Pass() { return static_cast<scoped_ptr&&>(*this); } + // Deprecated; remove in March 2016 (bug 5373). + RTC_DEPRECATED scoped_ptr&& Pass() { + return std::move(*this); + } // Reset. Deletes the currently owned array, if any. // Then takes ownership of a new object, if given. |