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 // TODO(crbug.com/566182): DEPRECATED! |
| 13 // Use DISALLOW_COPY_AND_ASSIGN instead, or if your type will be used in |
| 14 // Callbacks, use DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND instead. |
| 15 #define MOVE_ONLY_TYPE_FOR_CPP_03(type) \ |
| 16 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(type) |
| 17 |
| 18 // A macro to disallow the copy constructor and copy assignment functions. |
| 19 // This should be used in the private: declarations for a class. |
13 // | 20 // |
14 // USAGE | 21 // Use this macro instead of DISALLOW_COPY_AND_ASSIGN if you want to pass |
| 22 // ownership of the type through a base::Callback without heap-allocating it |
| 23 // into a scoped_ptr. The class must define a move constructor and move |
| 24 // assignment operator to make this work. |
15 // | 25 // |
16 // This macro should be used instead of DISALLOW_COPY_AND_ASSIGN to create | 26 // 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 | 27 // 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 | 28 // See IsMoveOnlyType template and its usage in base/callback_internal.h |
50 // for more details. | 29 // for more details. |
51 | 30 // TODO(crbug.com/566182): Remove this macro and use DISALLOW_COPY_AND_ASSIGN |
52 #define MOVE_ONLY_TYPE_FOR_CPP_03(type) \ | 31 // everywhere instead. |
53 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type) | 32 #define DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(type) \ |
54 | |
55 #define MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(type) \ | |
56 private: \ | 33 private: \ |
57 type(const type&) = delete; \ | 34 type(const type&) = delete; \ |
58 void operator=(const type&) = delete; \ | 35 void operator=(const type&) = delete; \ |
59 \ | 36 \ |
60 public: \ | 37 public: \ |
61 type&& Pass() WARN_UNUSED_RESULT { return std::move(*this); } \ | 38 type&& Pass() WARN_UNUSED_RESULT { return std::move(*this); } \ |
62 typedef void MoveOnlyTypeForCPP03; \ | 39 typedef void MoveOnlyTypeForCPP03; \ |
63 \ | 40 \ |
64 private: | 41 private: |
65 | 42 |
66 #endif // BASE_MOVE_H_ | 43 #endif // BASE_MOVE_H_ |
OLD | NEW |