Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_MOVE_H_ |
michael@0 | 6 | #define BASE_MOVE_H_ |
michael@0 | 7 | |
michael@0 | 8 | // Macro with the boilerplate that makes a type move-only in C++03. |
michael@0 | 9 | // |
michael@0 | 10 | // USAGE |
michael@0 | 11 | // |
michael@0 | 12 | // This macro should be used instead of DISALLOW_COPY_AND_ASSIGN to create |
michael@0 | 13 | // a "move-only" type. Unlike DISALLOW_COPY_AND_ASSIGN, this macro should be |
michael@0 | 14 | // the first line in a class declaration. |
michael@0 | 15 | // |
michael@0 | 16 | // A class using this macro must call .Pass() (or somehow be an r-value already) |
michael@0 | 17 | // before it can be: |
michael@0 | 18 | // |
michael@0 | 19 | // * Passed as a function argument |
michael@0 | 20 | // * Used as the right-hand side of an assignment |
michael@0 | 21 | // * Returned from a function |
michael@0 | 22 | // |
michael@0 | 23 | // Each class will still need to define their own "move constructor" and "move |
michael@0 | 24 | // operator=" to make this useful. Here's an example of the macro, the move |
michael@0 | 25 | // constructor, and the move operator= from the scoped_ptr class: |
michael@0 | 26 | // |
michael@0 | 27 | // template <typename T> |
michael@0 | 28 | // class scoped_ptr { |
michael@0 | 29 | // MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
michael@0 | 30 | // public: |
michael@0 | 31 | // scoped_ptr(RValue& other) : ptr_(other.release()) { } |
michael@0 | 32 | // scoped_ptr& operator=(RValue& other) { |
michael@0 | 33 | // swap(other); |
michael@0 | 34 | // return *this; |
michael@0 | 35 | // } |
michael@0 | 36 | // }; |
michael@0 | 37 | // |
michael@0 | 38 | // Note that the constructor must NOT be marked explicit. |
michael@0 | 39 | // |
michael@0 | 40 | // For consistency, the second parameter to the macro should always be RValue |
michael@0 | 41 | // unless you have a strong reason to do otherwise. It is only exposed as a |
michael@0 | 42 | // macro parameter so that the move constructor and move operator= don't look |
michael@0 | 43 | // like they're using a phantom type. |
michael@0 | 44 | // |
michael@0 | 45 | // |
michael@0 | 46 | // HOW THIS WORKS |
michael@0 | 47 | // |
michael@0 | 48 | // For a thorough explanation of this technique, see: |
michael@0 | 49 | // |
michael@0 | 50 | // http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor |
michael@0 | 51 | // |
michael@0 | 52 | // The summary is that we take advantage of 2 properties: |
michael@0 | 53 | // |
michael@0 | 54 | // 1) non-const references will not bind to r-values. |
michael@0 | 55 | // 2) C++ can apply one user-defined conversion when initializing a |
michael@0 | 56 | // variable. |
michael@0 | 57 | // |
michael@0 | 58 | // The first lets us disable the copy constructor and assignment operator |
michael@0 | 59 | // by declaring private version of them with a non-const reference parameter. |
michael@0 | 60 | // |
michael@0 | 61 | // For l-values, direct initialization still fails like in |
michael@0 | 62 | // DISALLOW_COPY_AND_ASSIGN because the copy constructor and assignment |
michael@0 | 63 | // operators are private. |
michael@0 | 64 | // |
michael@0 | 65 | // For r-values, the situation is different. The copy constructor and |
michael@0 | 66 | // assignment operator are not viable due to (1), so we are trying to call |
michael@0 | 67 | // a non-existent constructor and non-existing operator= rather than a private |
michael@0 | 68 | // one. Since we have not committed an error quite yet, we can provide an |
michael@0 | 69 | // alternate conversion sequence and a constructor. We add |
michael@0 | 70 | // |
michael@0 | 71 | // * a private struct named "RValue" |
michael@0 | 72 | // * a user-defined conversion "operator RValue()" |
michael@0 | 73 | // * a "move constructor" and "move operator=" that take the RValue& as |
michael@0 | 74 | // their sole parameter. |
michael@0 | 75 | // |
michael@0 | 76 | // Only r-values will trigger this sequence and execute our "move constructor" |
michael@0 | 77 | // or "move operator=." L-values will match the private copy constructor and |
michael@0 | 78 | // operator= first giving a "private in this context" error. This combination |
michael@0 | 79 | // gives us a move-only type. |
michael@0 | 80 | // |
michael@0 | 81 | // For signaling a destructive transfer of data from an l-value, we provide a |
michael@0 | 82 | // method named Pass() which creates an r-value for the current instance |
michael@0 | 83 | // triggering the move constructor or move operator=. |
michael@0 | 84 | // |
michael@0 | 85 | // Other ways to get r-values is to use the result of an expression like a |
michael@0 | 86 | // function call. |
michael@0 | 87 | // |
michael@0 | 88 | // Here's an example with comments explaining what gets triggered where: |
michael@0 | 89 | // |
michael@0 | 90 | // class Foo { |
michael@0 | 91 | // MOVE_ONLY_TYPE_FOR_CPP_03(Foo, RValue); |
michael@0 | 92 | // |
michael@0 | 93 | // public: |
michael@0 | 94 | // ... API ... |
michael@0 | 95 | // Foo(RValue other); // Move constructor. |
michael@0 | 96 | // Foo& operator=(RValue rhs); // Move operator= |
michael@0 | 97 | // }; |
michael@0 | 98 | // |
michael@0 | 99 | // Foo MakeFoo(); // Function that returns a Foo. |
michael@0 | 100 | // |
michael@0 | 101 | // Foo f; |
michael@0 | 102 | // Foo f_copy(f); // ERROR: Foo(Foo&) is private in this context. |
michael@0 | 103 | // Foo f_assign; |
michael@0 | 104 | // f_assign = f; // ERROR: operator=(Foo&) is private in this context. |
michael@0 | 105 | // |
michael@0 | 106 | // |
michael@0 | 107 | // Foo f(MakeFoo()); // R-value so alternate conversion executed. |
michael@0 | 108 | // Foo f_copy(f.Pass()); // R-value so alternate conversion executed. |
michael@0 | 109 | // f = f_copy.Pass(); // R-value so alternate conversion executed. |
michael@0 | 110 | // |
michael@0 | 111 | // |
michael@0 | 112 | // IMPLEMENTATION SUBTLETIES WITH RValue |
michael@0 | 113 | // |
michael@0 | 114 | // The RValue struct is just a container for a pointer back to the original |
michael@0 | 115 | // object. It should only ever be created as a temporary, and no external |
michael@0 | 116 | // class should ever declare it or use it in a parameter. |
michael@0 | 117 | // |
michael@0 | 118 | // It is tempting to want to use the RValue type in function parameters, but |
michael@0 | 119 | // excluding the limited usage here for the move constructor and move |
michael@0 | 120 | // operator=, doing so would mean that the function could take both r-values |
michael@0 | 121 | // and l-values equially which is unexpected. See COMPARED To Boost.Move for |
michael@0 | 122 | // more details. |
michael@0 | 123 | // |
michael@0 | 124 | // An alternate, and incorrect, implementation of the RValue class used by |
michael@0 | 125 | // Boost.Move makes RValue a fieldless child of the move-only type. RValue& |
michael@0 | 126 | // is then used in place of RValue in the various operators. The RValue& is |
michael@0 | 127 | // "created" by doing *reinterpret_cast<RValue*>(this). This has the appeal |
michael@0 | 128 | // of never creating a temporary RValue struct even with optimizations |
michael@0 | 129 | // disabled. Also, by virtue of inheritance you can treat the RValue |
michael@0 | 130 | // reference as if it were the move-only type itself. Unfortunately, |
michael@0 | 131 | // using the result of this reinterpret_cast<> is actually undefined behavior |
michael@0 | 132 | // due to C++98 5.2.10.7. In certain compilers (e.g., NaCl) the optimizer |
michael@0 | 133 | // will generate non-working code. |
michael@0 | 134 | // |
michael@0 | 135 | // In optimized builds, both implementations generate the same assembly so we |
michael@0 | 136 | // choose the one that adheres to the standard. |
michael@0 | 137 | // |
michael@0 | 138 | // |
michael@0 | 139 | // COMPARED TO C++11 |
michael@0 | 140 | // |
michael@0 | 141 | // In C++11, you would implement this functionality using an r-value reference |
michael@0 | 142 | // and our .Pass() method would be replaced with a call to std::move(). |
michael@0 | 143 | // |
michael@0 | 144 | // This emulation also has a deficiency where it uses up the single |
michael@0 | 145 | // user-defined conversion allowed by C++ during initialization. This can |
michael@0 | 146 | // cause problems in some API edge cases. For instance, in scoped_ptr, it is |
michael@0 | 147 | // impossible to make a function "void Foo(scoped_ptr<Parent> p)" accept a |
michael@0 | 148 | // value of type scoped_ptr<Child> even if you add a constructor to |
michael@0 | 149 | // scoped_ptr<> that would make it look like it should work. C++11 does not |
michael@0 | 150 | // have this deficiency. |
michael@0 | 151 | // |
michael@0 | 152 | // |
michael@0 | 153 | // COMPARED TO Boost.Move |
michael@0 | 154 | // |
michael@0 | 155 | // Our implementation similar to Boost.Move, but we keep the RValue struct |
michael@0 | 156 | // private to the move-only type, and we don't use the reinterpret_cast<> hack. |
michael@0 | 157 | // |
michael@0 | 158 | // In Boost.Move, RValue is the boost::rv<> template. This type can be used |
michael@0 | 159 | // when writing APIs like: |
michael@0 | 160 | // |
michael@0 | 161 | // void MyFunc(boost::rv<Foo>& f) |
michael@0 | 162 | // |
michael@0 | 163 | // that can take advantage of rv<> to avoid extra copies of a type. However you |
michael@0 | 164 | // would still be able to call this version of MyFunc with an l-value: |
michael@0 | 165 | // |
michael@0 | 166 | // Foo f; |
michael@0 | 167 | // MyFunc(f); // Uh oh, we probably just destroyed |f| w/o calling Pass(). |
michael@0 | 168 | // |
michael@0 | 169 | // unless someone is very careful to also declare a parallel override like: |
michael@0 | 170 | // |
michael@0 | 171 | // void MyFunc(const Foo& f) |
michael@0 | 172 | // |
michael@0 | 173 | // that would catch the l-values first. This was declared unsafe in C++11 and |
michael@0 | 174 | // a C++11 compiler will explicitly fail MyFunc(f). Unfortunately, we cannot |
michael@0 | 175 | // ensure this in C++03. |
michael@0 | 176 | // |
michael@0 | 177 | // Since we have no need for writing such APIs yet, our implementation keeps |
michael@0 | 178 | // RValue private and uses a .Pass() method to do the conversion instead of |
michael@0 | 179 | // trying to write a version of "std::move()." Writing an API like std::move() |
michael@0 | 180 | // would require the RValue struct to be public. |
michael@0 | 181 | // |
michael@0 | 182 | // |
michael@0 | 183 | // CAVEATS |
michael@0 | 184 | // |
michael@0 | 185 | // If you include a move-only type as a field inside a class that does not |
michael@0 | 186 | // explicitly declare a copy constructor, the containing class's implicit |
michael@0 | 187 | // copy constructor will change from Containing(const Containing&) to |
michael@0 | 188 | // Containing(Containing&). This can cause some unexpected errors. |
michael@0 | 189 | // |
michael@0 | 190 | // http://llvm.org/bugs/show_bug.cgi?id=11528 |
michael@0 | 191 | // |
michael@0 | 192 | // The workaround is to explicitly declare your copy constructor. |
michael@0 | 193 | // |
michael@0 | 194 | #define MOVE_ONLY_TYPE_FOR_CPP_03(type, rvalue_type) \ |
michael@0 | 195 | private: \ |
michael@0 | 196 | struct rvalue_type { \ |
michael@0 | 197 | explicit rvalue_type(type* object) : object(object) {} \ |
michael@0 | 198 | type* object; \ |
michael@0 | 199 | }; \ |
michael@0 | 200 | type(type&); \ |
michael@0 | 201 | void operator=(type&); \ |
michael@0 | 202 | public: \ |
michael@0 | 203 | operator rvalue_type() { return rvalue_type(this); } \ |
michael@0 | 204 | type Pass() { return type(rvalue_type(this)); } \ |
michael@0 | 205 | private: |
michael@0 | 206 | |
michael@0 | 207 | #endif // BASE_MOVE_H_ |