ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 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 | |
vmpstr | 6317056 | 2015-11-17 08:10:31 +0900 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
bratell | 5088ec9 | 2015-04-15 06:44:34 +0900 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
| 11 | |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 12 | // Macro with the boilerplate that makes a type move-only in C++11. |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 13 | // |
| 14 | // USAGE |
| 15 | // |
| 16 | // This macro should be used instead of DISALLOW_COPY_AND_ASSIGN to create |
| 17 | // a "move-only" type. Unlike DISALLOW_COPY_AND_ASSIGN, this macro should be |
| 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 |
jhawkins@chromium.org | f8659b7 | 2012-03-06 11:13:10 +0900 | [diff] [blame] | 25 | // * Returned from a function |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 26 | // |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 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: |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 30 | // |
| 31 | // template <typename T> |
| 32 | // class scoped_ptr { |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 33 | // MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type); |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 34 | // public: |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 35 | // scoped_ptr(scoped_ptr&& other) : ptr_(other.release()) { } |
| 36 | // scoped_ptr& operator=(scoped_ptr&& other) { |
| 37 | // reset(other.release()); |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 38 | // return *this; |
| 39 | // } |
| 40 | // }; |
| 41 | // |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 42 | // |
kinuko@chromium.org | 288f27a | 2013-10-22 18:18:20 +0900 | [diff] [blame] | 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 |
| 50 | // for more details. |
danakj | 16ca5a8 | 2014-10-07 04:34:20 +0900 | [diff] [blame] | 51 | |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 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: \ |
| 57 | type(const type&) = delete; \ |
| 58 | void operator=(const type&) = delete; \ |
| 59 | \ |
| 60 | public: \ |
vmpstr | 6317056 | 2015-11-17 08:10:31 +0900 | [diff] [blame] | 61 | type&& Pass() WARN_UNUSED_RESULT { return std::move(*this); } \ |
dcheng | 8e5e0e6 | 2015-12-01 21:09:52 +0900 | [diff] [blame^] | 62 | typedef void MoveOnlyTypeForCPP03; \ |
| 63 | \ |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 64 | private: |
| 65 | |
kkimlabs | 56bcfba | 2015-04-14 05:49:43 +0900 | [diff] [blame] | 66 | #define TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type) \ |
| 67 | public: \ |
vmpstr | 6317056 | 2015-11-17 08:10:31 +0900 | [diff] [blame] | 68 | type&& Pass() WARN_UNUSED_RESULT { return std::move(*this); } \ |
kkimlabs | 56bcfba | 2015-04-14 05:49:43 +0900 | [diff] [blame] | 69 | private: |
| 70 | |
ajwong@chromium.org | 59f1fbb | 2012-01-20 09:03:45 +0900 | [diff] [blame] | 71 | #endif // BASE_MOVE_H_ |