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 // This file contains utility functions and classes that help the | 5 // This file contains utility functions and classes that help the |
6 // implementation, and management of the Callback objects. | 6 // implementation, and management of the Callback objects. |
7 | 7 |
8 #ifndef BASE_CALLBACK_INTERNAL_H_ | 8 #ifndef BASE_CALLBACK_INTERNAL_H_ |
9 #define BASE_CALLBACK_INTERNAL_H_ | 9 #define BASE_CALLBACK_INTERNAL_H_ |
10 | 10 |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 #include <memory> | 12 #include <memory> |
13 #include <type_traits> | 13 #include <type_traits> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/atomic_ref_count.h" | 16 #include "base/atomic_ref_count.h" |
17 #include "base/base_export.h" | 17 #include "base/base_export.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
21 #include "base/template_util.h" | |
22 | 21 |
23 namespace base { | 22 namespace base { |
24 namespace internal { | 23 namespace internal { |
25 class CallbackBase; | 24 class CallbackBase; |
26 | 25 |
27 // BindStateBase is used to provide an opaque handle that the Callback | 26 // BindStateBase is used to provide an opaque handle that the Callback |
28 // class can use to represent a function object with bound arguments. It | 27 // class can use to represent a function object with bound arguments. It |
29 // behaves as an existential type that is used by a corresponding | 28 // behaves as an existential type that is used by a corresponding |
30 // DoInvoke function to perform the function execution. This allows | 29 // DoInvoke function to perform the function execution. This allows |
31 // us to shield the Callback class from the types of the bound argument via | 30 // us to shield the Callback class from the types of the bound argument via |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 95 |
97 // A helper template to determine if given type is non-const move-only-type, | 96 // A helper template to determine if given type is non-const move-only-type, |
98 // i.e. if a value of the given type should be passed via std::move() in a | 97 // i.e. if a value of the given type should be passed via std::move() in a |
99 // destructive way. Types are considered to be move-only if they have a | 98 // destructive way. Types are considered to be move-only if they have a |
100 // sentinel MoveOnlyTypeForCPP03 member: a class typically gets this from using | 99 // sentinel MoveOnlyTypeForCPP03 member: a class typically gets this from using |
101 // the DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND macro. | 100 // the DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND macro. |
102 // It would be easy to generalize this trait to all move-only types... but this | 101 // It would be easy to generalize this trait to all move-only types... but this |
103 // confuses template deduction in VS2013 with certain types such as | 102 // confuses template deduction in VS2013 with certain types such as |
104 // std::unique_ptr. | 103 // std::unique_ptr. |
105 // TODO(dcheng): Revisit this when Windows switches to VS2015 by default. | 104 // TODO(dcheng): Revisit this when Windows switches to VS2015 by default. |
| 105 |
106 template <typename T> struct IsMoveOnlyType { | 106 template <typename T> struct IsMoveOnlyType { |
| 107 // Types YesType and NoType are guaranteed such that sizeof(YesType) < |
| 108 // sizeof(NoType). |
| 109 using YesType = char; |
| 110 struct NoType { YesType dummy[2]; }; |
| 111 |
107 template <typename U> | 112 template <typename U> |
108 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); | 113 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); |
109 | 114 |
110 template <typename U> | 115 template <typename U> |
111 static NoType Test(...); | 116 static NoType Test(...); |
112 | 117 |
113 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) && | 118 static const bool value = sizeof((Test<T>(0))) == sizeof(YesType) && |
114 !is_const<T>::value; | 119 !std::is_const<T>::value; |
115 }; | 120 }; |
116 | 121 |
117 // Specialization of IsMoveOnlyType so that std::unique_ptr is still considered | 122 // Specialization of IsMoveOnlyType so that std::unique_ptr is still considered |
118 // move-only, even without the sentinel member. | 123 // move-only, even without the sentinel member. |
119 template <typename T, typename D> | 124 template <typename T, typename D> |
120 struct IsMoveOnlyType<std::unique_ptr<T, D>> : std::true_type {}; | 125 struct IsMoveOnlyType<std::unique_ptr<T, D>> : std::true_type {}; |
121 | 126 |
122 // Specialization of std::vector, so that it's considered move-only if the | 127 // Specialization of std::vector, so that it's considered move-only if the |
123 // element type is move-only. Allocator is explicitly ignored when determining | 128 // element type is move-only. Allocator is explicitly ignored when determining |
124 // move-only status of the std::vector. | 129 // move-only status of the std::vector. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 template <typename T> | 213 template <typename T> |
209 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( | 214 typename std::enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward( |
210 T& t) { | 215 T& t) { |
211 return std::move(t); | 216 return std::move(t); |
212 } | 217 } |
213 | 218 |
214 } // namespace internal | 219 } // namespace internal |
215 } // namespace base | 220 } // namespace base |
216 | 221 |
217 #endif // BASE_CALLBACK_INTERNAL_H_ | 222 #endif // BASE_CALLBACK_INTERNAL_H_ |
OLD | NEW |