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 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() and base::ConstRef(). | 9 // The public functions are base::Unretained() and base::ConstRef(). |
10 // Unretained() allows Bind() to bind a non-refcounted class. | 10 // Unretained() allows Bind() to bind a non-refcounted class. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 // Note that because ConstRef() takes a reference on |n|, |n| must outlive all | 46 // Note that because ConstRef() takes a reference on |n|, |n| must outlive all |
47 // its bound callbacks. | 47 // its bound callbacks. |
48 // | 48 // |
49 | 49 |
50 #ifndef BASE_BIND_HELPERS_H_ | 50 #ifndef BASE_BIND_HELPERS_H_ |
51 #define BASE_BIND_HELPERS_H_ | 51 #define BASE_BIND_HELPERS_H_ |
52 #pragma once | 52 #pragma once |
53 | 53 |
54 #include "base/basictypes.h" | 54 #include "base/basictypes.h" |
55 #include "base/template_util.h" | 55 #include "base/template_util.h" |
56 #include "base/memory/weak_ptr.h" | |
willchan no longer on Chromium
2011/05/14 01:25:10
sort alphabetically :)
awong
2011/05/16 20:01:27
Done.
| |
56 | 57 |
57 namespace base { | 58 namespace base { |
58 namespace internal { | 59 namespace internal { |
59 | 60 |
60 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T | 61 // Use the Substitution Failure Is Not An Error (SFINAE) trick to inspect T |
61 // for the existence of AddRef() and Release() functions of the correct | 62 // for the existence of AddRef() and Release() functions of the correct |
62 // signature. | 63 // signature. |
63 // | 64 // |
64 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error | 65 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error |
65 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-templat e-to-check-for-a-functions-existence | 66 // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-templat e-to-check-for-a-functions-existence |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); } | 209 T* Unwrap(UnretainedWrapper<T> unretained) { return unretained.get(); } |
209 | 210 |
210 template <typename T> | 211 template <typename T> |
211 const T& Unwrap(ConstRefWrapper<T> const_ref) { | 212 const T& Unwrap(ConstRefWrapper<T> const_ref) { |
212 return const_ref.get(); | 213 return const_ref.get(); |
213 } | 214 } |
214 | 215 |
215 | 216 |
216 // Utility for handling different refcounting semantics in the Bind() | 217 // Utility for handling different refcounting semantics in the Bind() |
217 // function. | 218 // function. |
218 template <typename ref, typename T> | 219 template <typename IsMethod, typename T> |
219 struct MaybeRefcount; | 220 struct MaybeRefcount; |
220 | 221 |
221 template <typename T> | 222 template <typename T> |
222 struct MaybeRefcount<base::false_type, T> { | 223 struct MaybeRefcount<base::false_type, T> { |
223 static void AddRef(const T&) {} | 224 static void AddRef(const T&) {} |
224 static void Release(const T&) {} | 225 static void Release(const T&) {} |
225 }; | 226 }; |
226 | 227 |
227 template <typename T, size_t n> | 228 template <typename T, size_t n> |
228 struct MaybeRefcount<base::false_type, T[n]> { | 229 struct MaybeRefcount<base::false_type, T[n]> { |
(...skipping 12 matching lines...) Expand all Loading... | |
241 static void AddRef(T* o) { o->AddRef(); } | 242 static void AddRef(T* o) { o->AddRef(); } |
242 static void Release(T* o) { o->Release(); } | 243 static void Release(T* o) { o->Release(); } |
243 }; | 244 }; |
244 | 245 |
245 template <typename T> | 246 template <typename T> |
246 struct MaybeRefcount<base::true_type, const T*> { | 247 struct MaybeRefcount<base::true_type, const T*> { |
247 static void AddRef(const T* o) { o->AddRef(); } | 248 static void AddRef(const T* o) { o->AddRef(); } |
248 static void Release(const T* o) { o->Release(); } | 249 static void Release(const T* o) { o->Release(); } |
249 }; | 250 }; |
250 | 251 |
252 template <typename T> | |
253 struct MaybeRefcount<base::true_type, WeakPtr<T> > { | |
254 static void AddRef(const WeakPtr<T>&) {} | |
255 static void Release(const WeakPtr<T>&) {} | |
256 }; | |
257 | |
251 } // namespace internal | 258 } // namespace internal |
252 | 259 |
253 template <typename T> | 260 template <typename T> |
254 inline internal::UnretainedWrapper<T> Unretained(T* o) { | 261 inline internal::UnretainedWrapper<T> Unretained(T* o) { |
255 return internal::UnretainedWrapper<T>(o); | 262 return internal::UnretainedWrapper<T>(o); |
256 } | 263 } |
257 | 264 |
258 template <typename T> | 265 template <typename T> |
259 inline internal::ConstRefWrapper<T> ConstRef(const T& o) { | 266 inline internal::ConstRefWrapper<T> ConstRef(const T& o) { |
260 return internal::ConstRefWrapper<T>(o); | 267 return internal::ConstRefWrapper<T>(o); |
261 } | 268 } |
262 | 269 |
263 } // namespace base | 270 } // namespace base |
264 | 271 |
265 #endif // BASE_BIND_HELPERS_H_ | 272 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |