Chromium Code Reviews| Index: base/memory/scoped_ptr.h |
| diff --git a/base/memory/scoped_ptr.h b/base/memory/scoped_ptr.h |
| index 5ac6846f3964e2ea929cef92d065454f23a64a4a..cc64a699c67c680fc6734df6311e793530f2dfa0 100644 |
| --- a/base/memory/scoped_ptr.h |
| +++ b/base/memory/scoped_ptr.h |
| @@ -32,6 +32,37 @@ |
| // foo.get()->Method(); // Foo::Method on the 0th element. |
| // foo[10].Method(); // Foo::Method on the 10th element. |
| // } |
| +// |
| +// These scopers also implement part of the functionality of C++11 unique_ptr |
| +// in that they are "moveable 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 funciton 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() { |
| +// return scoped_ptr<Foo>(new Foo("new")); |
| +// } |
| +// scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { |
| +// return arg.Pass(); |
| +// } |
| +// |
| +// { |
| +// scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay)" |
| +// 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 correspondly NULL. |
| +// } |
| +// |
| +// Notice that if you do not call Pass() when retruning from CreateFoo(), or |
|
darin (slow to review)
2011/12/03 00:25:10
retruning -> returning
awong
2011/12/06 00:02:08
Done.
|
| +// when invoking TakesOwnership(), the code will not compile because scopers |
| +// are not copyable; they only implement move semantics based on the Pass() |
| +// function. |
| #ifndef BASE_MEMORY_SCOPED_PTR_H_ |
| #define BASE_MEMORY_SCOPED_PTR_H_ |
| @@ -47,6 +78,28 @@ |
| #include "base/compiler_specific.h" |
| +// Macro with the boilerplate C++03 move emulation for a class. |
| +// |
| +// In C++11, this is done via r-value references. Here, we use |
| +// C++03 move emulation. For a more detailed explanation, see: |
| +// |
| +// http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor |
| +// |
| +#define CPP_03_MOVE_EMULATION(scoper, field) \ |
| + private: \ |
| + struct MoveProxy { \ |
| + explicit MoveProxy(scoper& obj) : obj_(obj) {} \ |
| + scoper& obj_; \ |
| + }; \ |
| + public: \ |
| + operator MoveProxy() { return MoveProxy(*this); } \ |
| + scoper(MoveProxy proxy) : field(proxy.obj_.release()) { } \ |
| + scoper& operator=(MoveProxy proxy) { \ |
| + swap(proxy.obj_); \ |
| + return *this; \ |
| + } \ |
| + scoper Pass() { return scoper(MoveProxy(*this)); } |
| + |
| // 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. |
| @@ -122,6 +175,8 @@ class scoped_ptr { |
| return retVal; |
| } |
| + CPP_03_MOVE_EMULATION(scoped_ptr, ptr_); |
| + |
| private: |
| C* ptr_; |
| @@ -131,9 +186,10 @@ class scoped_ptr { |
| template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; |
| template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; |
| - // Disallow evil constructors |
| - scoped_ptr(const scoped_ptr&); |
| - void operator=(const scoped_ptr&); |
| + // Disallow evil constructors. Note that MUST NOT take a const& because we |
| + // are implmenting move semantics. See the CPP_03_MOVE_EMULATION macro. |
| + scoped_ptr(scoped_ptr&); |
| + void operator=(scoped_ptr&); |
| }; |
| // Free functions |
| @@ -229,6 +285,8 @@ class scoped_array { |
| return retVal; |
| } |
| + CPP_03_MOVE_EMULATION(scoped_array, array_); |
| + |
| private: |
| C* array_; |
| @@ -236,9 +294,10 @@ class scoped_array { |
| template <class C2> bool operator==(scoped_array<C2> const& p2) const; |
| template <class C2> bool operator!=(scoped_array<C2> const& p2) const; |
| - // Disallow evil constructors |
| - scoped_array(const scoped_array&); |
| - void operator=(const scoped_array&); |
| + // Disallow evil constructors. Note that MUST NOT take a const& because we |
| + // are implmenting move semantics. See the CPP_03_MOVE_EMULATION macro. |
| + scoped_array(scoped_array&); |
| + void operator=(scoped_array&); |
| }; |
| // Free functions |
| @@ -347,6 +406,8 @@ class scoped_ptr_malloc { |
| return tmp; |
| } |
| + CPP_03_MOVE_EMULATION(scoped_ptr_malloc, ptr_); |
| + |
| private: |
| C* ptr_; |
| @@ -356,11 +417,14 @@ class scoped_ptr_malloc { |
| template <class C2, class GP> |
| bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const; |
| - // Disallow evil constructors |
| - scoped_ptr_malloc(const scoped_ptr_malloc&); |
| - void operator=(const scoped_ptr_malloc&); |
| + // Disallow evil constructors. Note that MUST NOT take a const& because we |
| + // are implmenting move semantics. See the CPP_03_MOVE_EMULATION macro. |
| + scoped_ptr_malloc(scoped_ptr_malloc&); |
| + void operator=(scoped_ptr_malloc&); |
| }; |
| +#undef CPP_03_MOVE_EMULATION |
| + |
| template<class C, class FP> inline |
| void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) { |
| a.swap(b); |