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

Side by Side Diff: base/bind_helpers.h

Issue 8209001: Callback API Change: Add in an Owned() wrapper to base::Bind(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix comment Created 9 years, 2 months 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 | « no previous file | base/bind_unittest.cc » ('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 defines a set of argument wrappers and related factory methods that 5 // This defines a set of argument wrappers and related factory methods that
6 // can be used specify the refcounting and reference semantics of arguments 6 // can be used specify the refcounting and reference semantics of arguments
7 // that are bound by the Bind() function in base/bind.h. 7 // that are bound by the Bind() function in base/bind.h.
8 // 8 //
9 // The public functions are base::Unretained(), base::ConstRef(), and 9 // The public functions are base::Unretained(), base::ConstRef(), and
10 // base::IgnoreReturn(). 10 // base::IgnoreReturn().
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 192
193 template <typename T> 193 template <typename T>
194 struct UnsafeBindtoRefCountedArg<T*> 194 struct UnsafeBindtoRefCountedArg<T*>
195 : UnsafeBindtoRefCountedArgHelper<is_class<T>::value, T> { 195 : UnsafeBindtoRefCountedArgHelper<is_class<T>::value, T> {
196 }; 196 };
197 197
198 198
199 template <typename T> 199 template <typename T>
200 class UnretainedWrapper { 200 class UnretainedWrapper {
201 public: 201 public:
202 explicit UnretainedWrapper(T* o) : obj_(o) {} 202 explicit UnretainedWrapper(T* o) : ptr_(o) {}
203 T* get() { return obj_; } 203 T* get() const { return ptr_; }
204 private: 204 private:
205 T* obj_; 205 T* ptr_;
206 }; 206 };
207 207
208 template <typename T> 208 template <typename T>
209 class ConstRefWrapper { 209 class ConstRefWrapper {
210 public: 210 public:
211 explicit ConstRefWrapper(const T& o) : ptr_(&o) {} 211 explicit ConstRefWrapper(const T& o) : ptr_(&o) {}
212 const T& get() { return *ptr_; } 212 const T& get() const { return *ptr_; }
213 private: 213 private:
214 const T* ptr_; 214 const T* ptr_;
215 }; 215 };
216 216
217 // An alternate implementation is to avoid the destructive copy, and instead
218 // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to
219 // a class that is essentially a scoped_ptr<>.
220 //
221 // This has the benefit though of leaving ParamTrais<> fully in
222 // callback_internal.h as well as avoiding type conversions during storage.
223 template <typename T>
224 class OwnedWrapper {
225 public:
226 explicit OwnedWrapper(T* o) : ptr_(o) {}
227 ~OwnedWrapper() { delete ptr_; }
228 T* get() const { return ptr_; }
229 OwnedWrapper(const OwnedWrapper& other) {
230 ptr_ = other.ptr_;
231 other.ptr_ = NULL;
232 }
233
234 private:
235 mutable T* ptr_;
236 };
237
217 238
218 // Unwrap the stored parameters for the wrappers above. 239 // Unwrap the stored parameters for the wrappers above.
219 template <typename T> 240 template <typename T>
220 T Unwrap(T o) { return o; } 241 T Unwrap(T o) { return o; }
221 242
222 template <typename T> 243 template <typename T>
223 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); } 244 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); }
224 245
225 template <typename T> 246 template <typename T>
226 const T& Unwrap(ConstRefWrapper<T> const_ref) { 247 const T& Unwrap(ConstRefWrapper<T> const_ref) {
227 return const_ref.get(); 248 return const_ref.get();
228 } 249 }
229 250
230 template <typename T> 251 template <typename T>
231 T* Unwrap(const scoped_refptr<T>& o) { return o.get(); } 252 T* Unwrap(const scoped_refptr<T>& o) { return o.get(); }
232 253
233 template <typename T> 254 template <typename T>
234 const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { return o; } 255 const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { return o; }
235 256
257 template <typename T>
258 T* Unwrap(const OwnedWrapper<T>& o) {
259 return o.get();
260 }
261
236 // Utility for handling different refcounting semantics in the Bind() 262 // Utility for handling different refcounting semantics in the Bind()
237 // function. 263 // function.
238 template <typename IsMethod, typename T> 264 template <typename IsMethod, typename T>
239 struct MaybeRefcount; 265 struct MaybeRefcount;
240 266
241 template <typename T> 267 template <typename T>
242 struct MaybeRefcount<base::false_type, T> { 268 struct MaybeRefcount<base::false_type, T> {
243 static void AddRef(const T&) {} 269 static void AddRef(const T&) {}
244 static void Release(const T&) {} 270 static void Release(const T&) {}
245 }; 271 };
246 272
247 template <typename T, size_t n> 273 template <typename T, size_t n>
248 struct MaybeRefcount<base::false_type, T[n]> { 274 struct MaybeRefcount<base::false_type, T[n]> {
249 static void AddRef(const T*) {} 275 static void AddRef(const T*) {}
250 static void Release(const T*) {} 276 static void Release(const T*) {}
251 }; 277 };
252 278
253 template <typename T> 279 template <typename T>
280 struct MaybeRefcount<base::true_type, T*> {
281 static void AddRef(T* o) { o->AddRef(); }
282 static void Release(T* o) { o->Release(); }
283 };
284
285 template <typename T>
254 struct MaybeRefcount<base::true_type, UnretainedWrapper<T> > { 286 struct MaybeRefcount<base::true_type, UnretainedWrapper<T> > {
255 static void AddRef(const UnretainedWrapper<T>&) {} 287 static void AddRef(const UnretainedWrapper<T>&) {}
256 static void Release(const UnretainedWrapper<T>&) {} 288 static void Release(const UnretainedWrapper<T>&) {}
257 }; 289 };
258 290
259 template <typename T> 291 template <typename T>
260 struct MaybeRefcount<base::true_type, T*> { 292 struct MaybeRefcount<base::true_type, OwnedWrapper<T> > {
261 static void AddRef(T* o) { o->AddRef(); } 293 static void AddRef(const OwnedWrapper<T>&) {}
262 static void Release(T* o) { o->Release(); } 294 static void Release(const OwnedWrapper<T>&) {}
263 }; 295 };
264 296
265 // No need to additionally AddRef() and Release() since we are storing a 297 // No need to additionally AddRef() and Release() since we are storing a
266 // scoped_refptr<> inside the storage object already. 298 // scoped_refptr<> inside the storage object already.
267 template <typename T> 299 template <typename T>
268 struct MaybeRefcount<base::true_type, scoped_refptr<T> > { 300 struct MaybeRefcount<base::true_type, scoped_refptr<T> > {
269 static void AddRef(const scoped_refptr<T>& o) {} 301 static void AddRef(const scoped_refptr<T>& o) {}
270 static void Release(const scoped_refptr<T>& o) {} 302 static void Release(const scoped_refptr<T>& o) {}
271 }; 303 };
272 304
(...skipping 19 matching lines...) Expand all
292 template <typename T> 324 template <typename T>
293 inline internal::UnretainedWrapper<T> Unretained(T* o) { 325 inline internal::UnretainedWrapper<T> Unretained(T* o) {
294 return internal::UnretainedWrapper<T>(o); 326 return internal::UnretainedWrapper<T>(o);
295 } 327 }
296 328
297 template <typename T> 329 template <typename T>
298 inline internal::ConstRefWrapper<T> ConstRef(const T& o) { 330 inline internal::ConstRefWrapper<T> ConstRef(const T& o) {
299 return internal::ConstRefWrapper<T>(o); 331 return internal::ConstRefWrapper<T>(o);
300 } 332 }
301 333
334 template <typename T>
335 inline internal::OwnedWrapper<T> Owned(T* o) {
darin (slow to review) 2011/10/11 05:13:50 how about some documentation? i'm concerned that
awong 2011/10/11 17:24:27 This is the start of concern? Or are you concerne
336 return internal::OwnedWrapper<T>(o);
337 }
338
302 template <typename R> 339 template <typename R>
303 Closure IgnoreReturn(Callback<R(void)> callback) { 340 Closure IgnoreReturn(Callback<R(void)> callback) {
304 return Bind(&internal::VoidReturnAdapter<R>, callback); 341 return Bind(&internal::VoidReturnAdapter<R>, callback);
305 } 342 }
306 343
307 } // namespace base 344 } // namespace base
308 345
309 #endif // BASE_BIND_HELPERS_H_ 346 #endif // BASE_BIND_HELPERS_H_
OLDNEW
« no previous file with comments | « no previous file | base/bind_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698