PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/external/chromium_org/base/move.h

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