Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 | 11 |
| 12 // Macro with the boilerplate that makes a type move-only in C++11. | 12 // DEPRECATED: Use DISALLOW_COPY_AND_ASSIGN instead, or if your type will |
| 13 // be used in Callbacks, use DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND. | |
|
Nico
2015/12/04 22:13:58
we should probably have a tracking bug for doing t
danakj
2015/12/04 22:28:34
yaa I was thinking the same.. let me create and TO
| |
| 14 #define MOVE_ONLY_TYPE_FOR_CPP_03(type) \ | |
| 15 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(type) | |
| 16 | |
| 17 // A macro to disallow the copy constructor and copy assignment functions. | |
| 18 // This should be used in the private: declarations for a class. | |
| 13 // | 19 // |
| 14 // USAGE | 20 // Use this macro instead of DISALLOW_COPY_AND_ASSIGN if you want to pass |
| 21 // ownership of the type through a base::Callback without heap-allocating it | |
| 22 // into a scoped_ptr. The class must define a move constructor and move | |
| 23 // assignment operator to make this work. | |
| 15 // | 24 // |
| 16 // This macro should be used instead of DISALLOW_COPY_AND_ASSIGN to create | 25 // This version of the macro adds a Pass() function and a cryptic |
| 17 // a "move-only" type. Unlike DISALLOW_COPY_AND_ASSIGN, this macro should be | 26 // MoveOnlyTypeForCPP03 typedef for the base::Callback implementation to use. |
| 18 // the first line in a class declaration. | |
| 19 // | |
| 20 // A class using this macro must call .Pass() (or somehow be an r-value already) | |
| 21 // before it can be: | |
| 22 // | |
| 23 // * Passed as a function argument | |
| 24 // * Used as the right-hand side of an assignment | |
| 25 // * Returned from a function | |
| 26 // | |
| 27 // Each class will still need to define their own move constructor and move | |
| 28 // operator= to make this useful. Here's an example of the macro, the move | |
| 29 // constructor, and the move operator= from a hypothetical scoped_ptr class: | |
| 30 // | |
| 31 // template <typename T> | |
| 32 // class scoped_ptr { | |
| 33 // MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type); | |
| 34 // public: | |
| 35 // scoped_ptr(scoped_ptr&& other) : ptr_(other.release()) { } | |
| 36 // scoped_ptr& operator=(scoped_ptr&& other) { | |
| 37 // reset(other.release()); | |
| 38 // return *this; | |
| 39 // } | |
| 40 // }; | |
| 41 // | |
| 42 // | |
| 43 // WHY HAVE typedef void MoveOnlyTypeForCPP03 | |
| 44 // | |
| 45 // Callback<>/Bind() needs to understand movable-but-not-copyable semantics | |
| 46 // to call .Pass() appropriately when it is expected to transfer the value. | |
| 47 // The cryptic typedef MoveOnlyTypeForCPP03 is added to make this check | |
| 48 // easy and automatic in helper templates for Callback<>/Bind(). | |
| 49 // See IsMoveOnlyType template and its usage in base/callback_internal.h | 27 // See IsMoveOnlyType template and its usage in base/callback_internal.h |
| 50 // for more details. | 28 // for more details. |
| 51 | 29 #define DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(type) \ |
| 52 #define MOVE_ONLY_TYPE_FOR_CPP_03(type) \ | |
| 53 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type) | |
| 54 | |
| 55 #define MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type) \ | |
| 56 private: \ | 30 private: \ |
| 57 type(const type&) = delete; \ | 31 type(const type&) = delete; \ |
| 58 void operator=(const type&) = delete; \ | 32 void operator=(const type&) = delete; \ |
| 59 \ | 33 \ |
| 60 public: \ | 34 public: \ |
| 61 type&& Pass() WARN_UNUSED_RESULT { return std::move(*this); } \ | 35 type&& Pass() WARN_UNUSED_RESULT { return std::move(*this); } \ |
| 62 typedef void MoveOnlyTypeForCPP03; \ | 36 typedef void MoveOnlyTypeForCPP03; \ |
|
Nico
2015/12/04 22:13:58
We probably want to call this MoveOnlTypeForCallba
danakj
2015/12/04 22:28:34
Ya. I'll put a TODO to remove this whole macro for
| |
| 63 \ | 37 \ |
| 64 private: | 38 private: |
| 65 | 39 |
| 66 #endif // BASE_MOVE_H_ | 40 #endif // BASE_MOVE_H_ |
| OLD | NEW |