Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: base/callback_internal.h

Issue 8897005: Redo r113722 - Add Pass(), which implements move semantics, to scoped_ptr, scoped_array.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix stupid C++98 warning. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/callback.h.pump ('k') | base/memory/scoped_ptr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
16 17
17 namespace base { 18 namespace base {
18 namespace internal { 19 namespace internal {
19 20
20 // BindStateBase is used to provide an opaque handle that the Callback 21 // BindStateBase is used to provide an opaque handle that the Callback
21 // class can use to represent a function object with bound arguments. It 22 // class can use to represent a function object with bound arguments. It
22 // behaves as an existential type that is used by a corresponding 23 // behaves as an existential type that is used by a corresponding
23 // DoInvoke function to perform the function execution. This allows 24 // DoInvoke function to perform the function execution. This allows
24 // us to shield the Callback class from the types of the bound argument via 25 // us to shield the Callback class from the types of the bound argument via
25 // "type erasure." 26 // "type erasure."
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 typedef const T* StorageType; 124 typedef const T* StorageType;
124 }; 125 };
125 126
126 // See comment for CallbackParamTraits<T[n]>. 127 // See comment for CallbackParamTraits<T[n]>.
127 template <typename T> 128 template <typename T>
128 struct CallbackParamTraits<T[]> { 129 struct CallbackParamTraits<T[]> {
129 typedef const T* ForwardType; 130 typedef const T* ForwardType;
130 typedef const T* StorageType; 131 typedef const T* StorageType;
131 }; 132 };
132 133
134 // Parameter traits for movable-but-not-copyable scopers.
135 //
136 // Callback<>/Bind() understands movable-but-not-copyable semantics where
137 // the type cannot be copied but can still have its state destructively
138 // transferred (aka. moved) to another instance of the same type by calling a
139 // helper function. When used with Bind(), this signifies transferal of the
140 // object's state to the target function.
141 //
142 // For these types, the ForwardType must not be a const reference, or a
143 // reference. A const reference is inappropriate, and would break const
144 // correctness, because we are implementing a destructive move. A non-const
145 // reference cannot be used with temporaries which means the result of a
146 // function or a cast would not be usable with Callback<> or Bind().
147 //
148 // TODO(ajwong): We might be able to use SFINAE to search for the existence of
149 // a Pass() function in the type and avoid the whitelist in CallbackParamTraits
150 // and CallbackForward.
151 template <typename T>
152 struct CallbackParamTraits<scoped_ptr<T> > {
153 typedef scoped_ptr<T> ForwardType;
154 typedef scoped_ptr<T> StorageType;
155 };
156
157 template <typename T>
158 struct CallbackParamTraits<scoped_array<T> > {
159 typedef scoped_array<T> ForwardType;
160 typedef scoped_array<T> StorageType;
161 };
162
163 template <typename T>
164 struct CallbackParamTraits<scoped_ptr_malloc<T> > {
165 typedef scoped_ptr_malloc<T> ForwardType;
166 typedef scoped_ptr_malloc<T> StorageType;
167 };
168
169 // CallbackForward() is a very limited simulation of C++11's std::forward()
170 // used by the Callback/Bind system for a set of movable-but-not-copyable
171 // types. It is needed because forwarding a movable-but-not-copyable
172 // argument to another function requires us to invoke the proper move
173 // operator to create a rvalue version of the type. The supported types are
174 // whitelisted below as overloads of the CallbackForward() function. The
175 // default template compiles out to be a no-op.
176 //
177 // In C++11, std::forward would replace all uses of this function. However, it
178 // is impossible to implement a general std::forward with C++11 due to a lack
179 // of rvalue references.
180 template <typename T>
181 T& CallbackForward(T& t) { return t; }
182
183 template <typename T>
184 scoped_ptr<T> CallbackForward(scoped_ptr<T>& p) { return p.Pass(); }
185
186 template <typename T>
187 scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); }
188
189 template <typename T>
190 scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); }
191
133 } // namespace internal 192 } // namespace internal
134 } // namespace base 193 } // namespace base
135 194
136 #endif // BASE_CALLBACK_INTERNAL_H_ 195 #endif // BASE_CALLBACK_INTERNAL_H_
OLDNEW
« no previous file with comments | « base/callback.h.pump ('k') | base/memory/scoped_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698