OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <stddef.h> | 12 #include <stddef.h> |
13 | 13 |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 | 16 |
18 namespace base { | 17 namespace base { |
19 namespace internal { | 18 namespace internal { |
20 | 19 |
21 // BindStateBase is used to provide an opaque handle that the Callback | 20 // BindStateBase is used to provide an opaque handle that the Callback |
22 // class can use to represent a function object with bound arguments. It | 21 // class can use to represent a function object with bound arguments. It |
23 // behaves as an existential type that is used by a corresponding | 22 // behaves as an existential type that is used by a corresponding |
24 // DoInvoke function to perform the function execution. This allows | 23 // DoInvoke function to perform the function execution. This allows |
25 // us to shield the Callback class from the types of the bound argument via | 24 // us to shield the Callback class from the types of the bound argument via |
26 // "type erasure." | 25 // "type erasure." |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 typedef const T* StorageType; | 103 typedef const T* StorageType; |
105 }; | 104 }; |
106 | 105 |
107 // See comment for CallbackParamTraits<T[n]>. | 106 // See comment for CallbackParamTraits<T[n]>. |
108 template <typename T> | 107 template <typename T> |
109 struct CallbackParamTraits<T[]> { | 108 struct CallbackParamTraits<T[]> { |
110 typedef const T* ForwardType; | 109 typedef const T* ForwardType; |
111 typedef const T* StorageType; | 110 typedef const T* StorageType; |
112 }; | 111 }; |
113 | 112 |
114 // Parameter traits for movable-but-not-copyable scopers. | |
115 // | |
116 // Callback<>/Bind() understands movable-but-not-copyable semantics where | |
117 // the type cannot be copied but can still have its state destructively | |
118 // transferred (aka. moved) to another instance of the same type by calling a | |
119 // helper function. When used with Bind(), this signifies transferal of the | |
120 // object's state to the target function. | |
121 // | |
122 // For these types, the ForwardType must not be a const reference, or a | |
123 // reference. A const reference is inappropriate, and would break const | |
124 // correctness, because we are implementing a destructive move. A non-const | |
125 // reference cannot be used with temporaries which means the result of a | |
126 // function or a cast would not be usable with Callback<> or Bind(). | |
127 // | |
128 // TODO(ajwong): We might be able to use SFINAE to search for the existence of | |
129 // a Pass() function in the type and avoid the whitelist in CallbackParamTraits | |
130 // and CallbackForward. | |
131 template <typename T> | |
132 struct CallbackParamTraits<scoped_ptr<T> > { | |
133 typedef scoped_ptr<T> ForwardType; | |
134 typedef scoped_ptr<T> StorageType; | |
135 }; | |
136 | |
137 template <typename T> | |
138 struct CallbackParamTraits<scoped_array<T> > { | |
139 typedef scoped_array<T> ForwardType; | |
140 typedef scoped_array<T> StorageType; | |
141 }; | |
142 | |
143 template <typename T> | |
144 struct CallbackParamTraits<scoped_ptr_malloc<T> > { | |
145 typedef scoped_ptr_malloc<T> ForwardType; | |
146 typedef scoped_ptr_malloc<T> StorageType; | |
147 }; | |
148 | |
149 // CallbackForward() is a very limited simulation of C++11's std::forward() | |
150 // used by the Callback/Bind system for a set of movable-but-not-copyable | |
151 // types. It is needed because forwarding a movable-but-not-copyable | |
152 // argument to another function requires us to invoke the proper move | |
153 // operator to create a rvalue version of the type. The supported types are | |
154 // whitelisted below as overloads of the CallbackForward() function. The | |
155 // default template compiles out to be a no-op. | |
156 // | |
157 // In C++11, std::forward would replace all uses of this function. However, it | |
158 // is impossible to implement a general std::forward with C++11 due to a lack | |
159 // of rvalue references. | |
160 template <typename T> | |
161 T& CallbackForward(T& t) { return t; } | |
162 | |
163 template <typename T> | |
164 scoped_ptr<T> CallbackForward(scoped_ptr<T>& p) { return p.Pass(); } | |
165 | |
166 template <typename T> | |
167 scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); } | |
168 | |
169 template <typename T> | |
170 scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); } | |
171 | |
172 } // namespace internal | 113 } // namespace internal |
173 } // namespace base | 114 } // namespace base |
174 | 115 |
175 #endif // BASE_CALLBACK_INTERNAL_H_ | 116 #endif // BASE_CALLBACK_INTERNAL_H_ |
OLD | NEW |