OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_MOVE_H_ | |
6 #define BASE_MOVE_H_ | |
7 | |
8 // Macro with the boilerplate that makes a type move only in C++03. | |
9 // | |
10 // USAGE | |
11 // | |
12 // This macro should be used instead of DISALLOW_COPY_AND_ASSIGN to create | |
13 // a "move only type." Unlike, this macro should be the first line in a | |
14 // class declaration. | |
15 // | |
16 // A class using this macro must call .Pass() (or somehow be an r-value already) | |
17 // before it can be: | |
18 // | |
19 // * Passed as a function argument | |
20 // * Used as the righthand side of an assignment | |
21 // * Return from a function | |
22 // | |
23 // Each class will still need to define their own "move constructor" and "move | |
24 // operator=" to make this useful. Here's an example of both from the | |
25 // scoped_ptr class. | |
26 // | |
27 // scoped_ptr(RValue& other) : ptr_(other.release()) { } | |
28 // scoped_ptr& operator=(RValue& other) { | |
29 // swap(other); | |
30 // return *this; | |
31 // } | |
32 // | |
33 // Note that the constructor must NOT be marked explicit. | |
34 // | |
35 // | |
36 // HOW THIS WORKS | |
37 // | |
38 // For a thorough explanation of this technique, see: | |
39 // | |
40 // http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor | |
41 // | |
42 // The summary is that we take advantage of 2 properties: | |
43 // | |
44 // 1) non-const references will not bind to r-values. | |
45 // 2) C++ can apply one user-defined conversion when initializing a | |
46 // variable. | |
47 // | |
48 // The first lets us disable the copy constructor and assignment operator | |
49 // by declaring private version of them with a non-const reference parameter. | |
50 // | |
51 // For l-values, direct initialization still fails like in | |
52 // DISALLOW_COPY_AND_ASSIGN because the copy constructor and assignment | |
53 // operators are private. | |
54 // | |
55 // For r-values, thie situation is different. The copy constructor and | |
56 // assignment operator are not viable due to (1), so we are trying to call | |
57 // a non-existant constructor and non-existing operator= rather than a private | |
58 // one. Since we have not committed an error quite yet, we can provide an | |
59 // alternate conversion sequence and a constructor. We add | |
60 // * a private struct named "RValue" | |
61 // * a user-defined converstion "operator RValue&()" | |
62 // * a "move constructor" and "move operator=" that take the RValue& as | |
63 // their sole parameter. | |
64 // | |
65 // Only r-values will triger this sequence and execute our "move constructor" | |
66 // or "move operator=." Lvalues will match the private copy constructor and | |
67 // operator= first giving a "private in this context" error. This combination | |
68 // gives us a "move only type,." | |
69 // | |
70 // For signaling a destructive transfer of data from an Lvalue, we provide a | |
71 // method named Pass() which creates an r-values for the current instance | |
72 // triggering the move constructor or move operator=. | |
73 // | |
74 // Other ways to get r-values is to use the result of an expression like a | |
75 // function call. | |
76 // | |
77 // Here's an example with comments explaining what gets triggered where: | |
78 // | |
79 // class Foo { | |
80 // MOVE_ONLY_TYPE_FOR_CPP_03(Foo); | |
81 // | |
82 // public: | |
83 // ... API here including move constructor/operator=... | |
84 // }; | |
85 // Foo MakeFoo(); // Function that returns a Foo. | |
86 // | |
87 // Foo f; | |
88 // Foo f_copy(f); // ERROR: Foo(Foo&) is private in this context. | |
89 // Foo f_assign; | |
90 // f_assign = f; // ERROR: operator=(Foo&) is private in this context. | |
91 // | |
92 // | |
93 // Foo f(MakeFoo()); // R-value so alternate conversion executed. | |
94 // Foo f_copy(f.Pass()); // R-value so alternate conversion executed. | |
95 // f = f_copy.Pass(); // R-value so alternate conversion executed. | |
96 // | |
97 // | |
98 // IMPLEMENTATION SUBTLTIES WITH RValue | |
kim.grasman
2012/01/18 08:27:20
Nice spelling, but I think "SUBTLETIES" is more es
awong
2012/01/18 23:21:04
Thanks. Fixed!
| |
99 // | |
100 // The RValue struct has subtle properties: | |
101 // | |
102 // 1) All its methods are declared, but intentionally not defined. | |
103 // 2) It is *never* instantiated. | |
104 // 3) It is a child of the "move only" type. | |
105 // | |
106 // (1) is a guard against accidental violation of (2). If an instance of | |
107 // RValue were ever created, either as a temporary, or as a copy to some | |
108 // function parameter or field of a class, the binary will not link. | |
109 // | |
110 // This ensures that RValue can only exist as a temporary which is important | |
111 // to avoid accidental danging references. | |
112 // | |
113 // (3) allows us to get around instantiations because our user-defined | |
114 // conversion can return a downcast of this pointer. | |
115 // | |
116 // operator RValue&() { return *reinterpret_cast<RValue*>(this); } | |
117 // | |
118 // Because RValue does not extend the object size or add any virtual methods, | |
119 // this type-pun is safe. | |
120 // | |
121 // An alternative implementation would be to make RValue into a concrete | |
122 // struct that holds a reference to the type. But in the non-optimized build, | |
123 // this causes unnecessary temporaries to be made bloating the object files. | |
124 // Also, it would then be possible to accidentally persist an RValue instance. | |
125 // | |
126 // | |
127 // COMPARED TO C++11 | |
128 // | |
129 // In C++11, you would implement this functionality using an r-value reference | |
130 // and our .Pass() method would be replaced with a call to std::move(). | |
131 // | |
132 // This emulation also has a deficiency where it uses up the single | |
133 // user-defined conversion allowed by C++ during initialization. This can | |
134 // cause problems in some API edge cases. For instance, in scoped_ptr, it is | |
135 // impossible to make an funcion "void Foo(scoped_ptr<Parent> p)" accept a | |
136 // value of type scoped_ptr<Child> even if you add a constructor to | |
137 // scoped_ptr<> that would make it look like it should work. C++11 does not | |
138 // have this deficiency. | |
139 // | |
140 // | |
141 // COMPARED TO Boost.Move | |
142 // | |
143 // Our implementation is based on Boost.Move, but we keep the RValue struct | |
144 // private to the move-only type. | |
145 // | |
146 // In Boost.Move, RValue is the boost::rv<> template. This type can be used | |
147 // when writing APIs like: | |
148 // | |
149 // void MyFunc(boost::rv<Foo>& f) | |
150 // | |
151 // that can take advantage of rv<> to avoid extra copies of a type. However you | |
152 // would still be able to call this version of MyFunc with an l-value: | |
153 // | |
154 // Foo f; | |
155 // MyFunc(f); // Uh oh, we probably just destoyred |f| w/o calling Pass(). | |
156 // | |
157 // unless someone is very careful to also declare a parallel override like: | |
158 // | |
159 // void MyFunc(const Foo& f) | |
160 // | |
161 // that would catch the l-values first. This was declared unsafe in C++11 and | |
162 // a C++11 compiler will explicitly fail MyFunc(f). Unfortunately, we cannot | |
163 // ensure this in C++03. | |
164 // | |
165 // Since we have no need for writing such APIs yet, our implementaion keeps | |
166 // RValue private and uses a .Pass() method to do the conversion instead of | |
167 // trying to write a version of "std::move()." Writing an API like std::move() | |
168 // would require the RValue structs to be public. | |
169 // | |
170 // | |
171 // CAVEATS | |
172 // | |
173 // If you include a move-only type as a field inside a class that does not | |
174 // explicitly declare a copy constructor, the containing class's implicit | |
175 // copy constructor will change from Containing(const Constaining&) to | |
176 // Containing(Containing&). This can cause some unexpected errors. | |
177 // | |
178 // http://llvm.org/bugs/show_bug.cgi?id=11528 | |
179 // | |
180 // The workaround is to explicitly declare your copy constructor. | |
181 // | |
182 #define MOVE_ONLY_TYPE_FOR_CPP_03(type) \ | |
183 private: \ | |
184 struct RValue : public type { \ | |
185 RValue(); \ | |
186 ~RValue(); \ | |
187 RValue(const RValue&); \ | |
188 void operator=(const RValue&); \ | |
189 }; \ | |
190 type(type&); \ | |
191 void operator=(type&); \ | |
192 public: \ | |
193 operator RValue&() { return *reinterpret_cast<RValue*>(this); } \ | |
194 type Pass() { return type(*reinterpret_cast<RValue*>(this)); } \ | |
195 private: | |
196 | |
197 #endif // BASE_MOVE_H_ | |
OLD | NEW |