Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Side by Side Diff: base/move.h

Issue 11078014: Fix move.h's to use a concrete RValue carrier object rather than hacking a RValue&. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix error. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/memory/scoped_vector.h ('k') | base/win/scoped_handle.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_MOVE_H_ 5 #ifndef BASE_MOVE_H_
6 #define BASE_MOVE_H_ 6 #define BASE_MOVE_H_
7 7
8 // Macro with the boilerplate that makes a type move-only in C++03. 8 // Macro with the boilerplate that makes a type move-only in C++03.
9 // 9 //
10 // USAGE 10 // USAGE
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 // DISALLOW_COPY_AND_ASSIGN because the copy constructor and assignment 62 // DISALLOW_COPY_AND_ASSIGN because the copy constructor and assignment
63 // operators are private. 63 // operators are private.
64 // 64 //
65 // For r-values, the situation is different. The copy constructor and 65 // For r-values, the situation is different. The copy constructor and
66 // assignment operator are not viable due to (1), so we are trying to call 66 // assignment operator are not viable due to (1), so we are trying to call
67 // a non-existent constructor and non-existing operator= rather than a private 67 // a non-existent constructor and non-existing operator= rather than a private
68 // one. Since we have not committed an error quite yet, we can provide an 68 // one. Since we have not committed an error quite yet, we can provide an
69 // alternate conversion sequence and a constructor. We add 69 // alternate conversion sequence and a constructor. We add
70 // 70 //
71 // * a private struct named "RValue" 71 // * a private struct named "RValue"
72 // * a user-defined conversion "operator RValue&()" 72 // * a user-defined conversion "operator RValue()"
73 // * a "move constructor" and "move operator=" that take the RValue& as 73 // * a "move constructor" and "move operator=" that take the RValue& as
74 // their sole parameter. 74 // their sole parameter.
75 // 75 //
76 // Only r-values will trigger this sequence and execute our "move constructor" 76 // Only r-values will trigger this sequence and execute our "move constructor"
77 // or "move operator=." L-values will match the private copy constructor and 77 // or "move operator=." L-values will match the private copy constructor and
78 // operator= first giving a "private in this context" error. This combination 78 // operator= first giving a "private in this context" error. This combination
79 // gives us a move-only type. 79 // gives us a move-only type.
80 // 80 //
81 // For signaling a destructive transfer of data from an l-value, we provide a 81 // For signaling a destructive transfer of data from an l-value, we provide a
82 // method named Pass() which creates an r-value for the current instance 82 // method named Pass() which creates an r-value for the current instance
83 // triggering the move constructor or move operator=. 83 // triggering the move constructor or move operator=.
84 // 84 //
85 // Other ways to get r-values is to use the result of an expression like a 85 // Other ways to get r-values is to use the result of an expression like a
86 // function call. 86 // function call.
87 // 87 //
88 // Here's an example with comments explaining what gets triggered where: 88 // Here's an example with comments explaining what gets triggered where:
89 // 89 //
90 // class Foo { 90 // class Foo {
91 // MOVE_ONLY_TYPE_FOR_CPP_03(Foo, RValue); 91 // MOVE_ONLY_TYPE_FOR_CPP_03(Foo, RValue);
92 // 92 //
93 // public: 93 // public:
94 // ... API ... 94 // ... API ...
95 // Foo(RValue& other); // Move constructor. 95 // Foo(RValue other); // Move constructor.
96 // Foo& operator=(RValue& rhs); // Move operator= 96 // Foo& operator=(RValue rhs); // Move operator=
97 // }; 97 // };
98 // 98 //
99 // Foo MakeFoo(); // Function that returns a Foo. 99 // Foo MakeFoo(); // Function that returns a Foo.
100 // 100 //
101 // Foo f; 101 // Foo f;
102 // Foo f_copy(f); // ERROR: Foo(Foo&) is private in this context. 102 // Foo f_copy(f); // ERROR: Foo(Foo&) is private in this context.
103 // Foo f_assign; 103 // Foo f_assign;
104 // f_assign = f; // ERROR: operator=(Foo&) is private in this context. 104 // f_assign = f; // ERROR: operator=(Foo&) is private in this context.
105 // 105 //
106 // 106 //
107 // Foo f(MakeFoo()); // R-value so alternate conversion executed. 107 // Foo f(MakeFoo()); // R-value so alternate conversion executed.
108 // Foo f_copy(f.Pass()); // R-value so alternate conversion executed. 108 // Foo f_copy(f.Pass()); // R-value so alternate conversion executed.
109 // f = f_copy.Pass(); // R-value so alternate conversion executed. 109 // f = f_copy.Pass(); // R-value so alternate conversion executed.
110 // 110 //
111 // 111 //
112 // IMPLEMENTATION SUBTLETIES WITH RValue 112 // IMPLEMENTATION SUBTLETIES WITH RValue
113 // 113 //
114 // The RValue struct has subtle properties: 114 // The RValue struct is just a container for a pointer back to the original
115 // object. It should only ever be created as a temporary, and no external
116 // class should ever declare it or use it in a parameter.
115 // 117 //
116 // 1) All its methods are declared, but intentionally not defined. 118 // It is tempting to want to use the RValue type in function parameters, but
117 // 2) It is *never* instantiated. 119 // excluding the limited usage here for the move constructor and move
118 // 3) It is a child of the move-only type. 120 // operator=, doing so would mean that the function could take both r-values
121 // and l-values equially which is unexpected. See COMPARED To Boost.Move for
122 // more details.
119 // 123 //
120 // (1) is a guard against accidental violation of (2). If an instance of 124 // An alternate, and incorrect, implementation of the RValue class used by
121 // RValue were ever created, either as a temporary, or as a copy to some 125 // Boost.Move makes RValue a fieldless child of the move-only type. RValue&
122 // function parameter or field of a class, the binary will not link. 126 // is then used in place of RValue in the various operators. The RValue& is
127 // "created" by doing *reinterpret_cast<RValue*>(this). This has the appeal
128 // of never creating a temproary RValue struct even with optimizations
129 // disabled. Also, by virtue of inheritance you can treat the RValue
130 // reference as if it were the move-only type itself. Unfortuantely,
131 // using the result of this reinterpret_cast<> is actually undefined behavior
132 // due to C++98 5.2.10.7. In certain compilers (eg., NaCl) the optimizer
133 // will generate non-working code.
123 // 134 //
124 // This ensures that RValue can only exist as a temporary which is important 135 // In optimized builds, both implementations generate the same assembly so we
125 // to avoid accidental dangling references. 136 // choose the one that adheres to the standard. ☃
126 //
127 // (3) allows us to get around instantiations because our user-defined
128 // conversion can return a downcast of this pointer.
129 //
130 // operator RValue&() { return *reinterpret_cast<RValue*>(this); }
131 //
132 // Because RValue does not extend the object size or add any virtual methods,
133 // this type-pun is safe.
134 //
135 // An alternative implementation would be to make RValue into a concrete
136 // struct that holds a reference to the type. But in the non-optimized build,
137 // this causes unnecessary temporaries to be made bloating the object files.
138 // Also, it would then be possible to accidentally persist an RValue instance.
139 // 137 //
140 // 138 //
141 // COMPARED TO C++11 139 // COMPARED TO C++11
142 // 140 //
143 // In C++11, you would implement this functionality using an r-value reference 141 // In C++11, you would implement this functionality using an r-value reference
144 // and our .Pass() method would be replaced with a call to std::move(). 142 // and our .Pass() method would be replaced with a call to std::move().
145 // 143 //
146 // This emulation also has a deficiency where it uses up the single 144 // This emulation also has a deficiency where it uses up the single
147 // user-defined conversion allowed by C++ during initialization. This can 145 // user-defined conversion allowed by C++ during initialization. This can
148 // cause problems in some API edge cases. For instance, in scoped_ptr, it is 146 // cause problems in some API edge cases. For instance, in scoped_ptr, it is
149 // impossible to make an function "void Foo(scoped_ptr<Parent> p)" accept a 147 // impossible to make an function "void Foo(scoped_ptr<Parent> p)" accept a
150 // value of type scoped_ptr<Child> even if you add a constructor to 148 // value of type scoped_ptr<Child> even if you add a constructor to
151 // scoped_ptr<> that would make it look like it should work. C++11 does not 149 // scoped_ptr<> that would make it look like it should work. C++11 does not
152 // have this deficiency. 150 // have this deficiency.
153 // 151 //
154 // 152 //
155 // COMPARED TO Boost.Move 153 // COMPARED TO Boost.Move
156 // 154 //
157 // Our implementation is based on Boost.Move, but we keep the RValue struct 155 // Our implementation similar to Boost.Move, but we keep the RValue struct
158 // private to the move-only type. 156 // private to the move-only type, and we don't use the reinterpret_cast<> hack.
159 // 157 //
160 // In Boost.Move, RValue is the boost::rv<> template. This type can be used 158 // In Boost.Move, RValue is the boost::rv<> template. This type can be used
161 // when writing APIs like: 159 // when writing APIs like:
162 // 160 //
163 // void MyFunc(boost::rv<Foo>& f) 161 // void MyFunc(boost::rv<Foo>& f)
164 // 162 //
165 // that can take advantage of rv<> to avoid extra copies of a type. However you 163 // that can take advantage of rv<> to avoid extra copies of a type. However you
166 // would still be able to call this version of MyFunc with an l-value: 164 // would still be able to call this version of MyFunc with an l-value:
167 // 165 //
168 // Foo f; 166 // Foo f;
(...skipping 19 matching lines...) Expand all
188 // explicitly declare a copy constructor, the containing class's implicit 186 // explicitly declare a copy constructor, the containing class's implicit
189 // copy constructor will change from Containing(const Containing&) to 187 // copy constructor will change from Containing(const Containing&) to
190 // Containing(Containing&). This can cause some unexpected errors. 188 // Containing(Containing&). This can cause some unexpected errors.
191 // 189 //
192 // http://llvm.org/bugs/show_bug.cgi?id=11528 190 // http://llvm.org/bugs/show_bug.cgi?id=11528
193 // 191 //
194 // The workaround is to explicitly declare your copy constructor. 192 // The workaround is to explicitly declare your copy constructor.
195 // 193 //
196 #define MOVE_ONLY_TYPE_FOR_CPP_03(type, rvalue_type) \ 194 #define MOVE_ONLY_TYPE_FOR_CPP_03(type, rvalue_type) \
197 private: \ 195 private: \
198 struct rvalue_type : public type { \ 196 struct rvalue_type { \
199 rvalue_type(); \ 197 rvalue_type(type* object) : object(object) {} \
200 ~rvalue_type(); \ 198 type* object; \
201 rvalue_type(const rvalue_type&); \
202 void operator=(const rvalue_type&); \
203 }; \ 199 }; \
204 type(type&); \ 200 type(type&); \
205 void operator=(type&); \ 201 void operator=(type&); \
206 public: \ 202 public: \
207 operator rvalue_type&() { return *reinterpret_cast<rvalue_type*>(this); } \ 203 operator rvalue_type() { return rvalue_type(this); } \
208 type Pass() { return type(*reinterpret_cast<rvalue_type*>(this)); } \ 204 type Pass() { return type(rvalue_type(this)); } \
209 private: 205 private:
210 206
211 #endif // BASE_MOVE_H_ 207 #endif // BASE_MOVE_H_
OLDNEW
« no previous file with comments | « base/memory/scoped_vector.h ('k') | base/win/scoped_handle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698