| 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 // Weak handles provides a way to refer to weak pointers from another | 5 // Weak handles provides a way to refer to weak pointers from another |
| 6 // thread. This is useful because it is not safe to reference a weak | 6 // thread. This is useful because it is not safe to reference a weak |
| 7 // pointer from a thread other than the thread on which it was | 7 // pointer from a thread other than the thread on which it was |
| 8 // created. | 8 // created. |
| 9 // | 9 // |
| 10 // Weak handles can be passed across threads, so for example, you can | 10 // Weak handles can be passed across threads, so for example, you can |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 #ifndef CHROME_BROWSER_SYNC_UTIL_WEAK_HANDLE_H_ | 47 #ifndef CHROME_BROWSER_SYNC_UTIL_WEAK_HANDLE_H_ |
| 48 #define CHROME_BROWSER_SYNC_UTIL_WEAK_HANDLE_H_ | 48 #define CHROME_BROWSER_SYNC_UTIL_WEAK_HANDLE_H_ |
| 49 #pragma once | 49 #pragma once |
| 50 | 50 |
| 51 #include <cstddef> | 51 #include <cstddef> |
| 52 | 52 |
| 53 #include "base/basictypes.h" | 53 #include "base/basictypes.h" |
| 54 #include "base/bind.h" | 54 #include "base/bind.h" |
| 55 #include "base/callback.h" | 55 #include "base/callback.h" |
| 56 #include "base/compiler_specific.h" | 56 #include "base/compiler_specific.h" |
| 57 #include "base/gtest_prod_util.h" |
| 57 #include "base/location.h" | 58 #include "base/location.h" |
| 58 #include "base/logging.h" | 59 #include "base/logging.h" |
| 59 #include "base/memory/ref_counted.h" | 60 #include "base/memory/ref_counted.h" |
| 60 #include "base/memory/weak_ptr.h" | 61 #include "base/memory/weak_ptr.h" |
| 61 | 62 |
| 62 namespace base { | 63 namespace base { |
| 63 class MessageLoopProxy; | 64 class MessageLoopProxy; |
| 64 } // namespace base | 65 } // namespace base |
| 65 | 66 |
| 66 namespace tracked_objects { | 67 namespace tracked_objects { |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 template <typename T> | 274 template <typename T> |
| 274 class WeakHandle { | 275 class WeakHandle { |
| 275 public: | 276 public: |
| 276 // Creates an uninitialized WeakHandle. | 277 // Creates an uninitialized WeakHandle. |
| 277 WeakHandle() {} | 278 WeakHandle() {} |
| 278 | 279 |
| 279 // Creates an initialized WeakHandle from |ptr|. | 280 // Creates an initialized WeakHandle from |ptr|. |
| 280 explicit WeakHandle(const base::WeakPtr<T>& ptr) | 281 explicit WeakHandle(const base::WeakPtr<T>& ptr) |
| 281 : core_(new internal::WeakHandleCore<T>(ptr)) {} | 282 : core_(new internal::WeakHandleCore<T>(ptr)) {} |
| 282 | 283 |
| 284 // Allow conversion from WeakHandle<U> to WeakHandle<T> if U is |
| 285 // convertible to T, but we *must* be on |other|'s owner thread. |
| 286 // Note that this doesn't override the regular copy constructor, so |
| 287 // that one can be called on any thread. |
| 288 template <typename U> |
| 289 WeakHandle(const browser_sync::WeakHandle<U>& other) // NOLINT |
| 290 : core_( |
| 291 other.IsInitialized() ? |
| 292 new internal::WeakHandleCore<T>(other.Get()) : |
| 293 NULL) {} |
| 294 |
| 283 // Returns true iff this WeakHandle is initialized. Note that being | 295 // Returns true iff this WeakHandle is initialized. Note that being |
| 284 // initialized isn't a guarantee that the underlying object is still | 296 // initialized isn't a guarantee that the underlying object is still |
| 285 // alive. | 297 // alive. |
| 286 bool IsInitialized() const { | 298 bool IsInitialized() const { |
| 287 return core_.get() != NULL; | 299 return core_.get() != NULL; |
| 288 } | 300 } |
| 289 | 301 |
| 290 // Resets to an uninitialized WeakHandle. | 302 // Resets to an uninitialized WeakHandle. |
| 291 void Reset() { | 303 void Reset() { |
| 292 core_ = NULL; | 304 core_ = NULL; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 void (U::*fn)(A1, A2, A3, A4), | 353 void (U::*fn)(A1, A2, A3, A4), |
| 342 typename internal::ParamTraits<A1>::ForwardType a1, | 354 typename internal::ParamTraits<A1>::ForwardType a1, |
| 343 typename internal::ParamTraits<A2>::ForwardType a2, | 355 typename internal::ParamTraits<A2>::ForwardType a2, |
| 344 typename internal::ParamTraits<A3>::ForwardType a3, | 356 typename internal::ParamTraits<A3>::ForwardType a3, |
| 345 typename internal::ParamTraits<A4>::ForwardType a4) const { | 357 typename internal::ParamTraits<A4>::ForwardType a4) const { |
| 346 CHECK(IsInitialized()); | 358 CHECK(IsInitialized()); |
| 347 core_->Call(from_here, fn, a1, a2, a3, a4); | 359 core_->Call(from_here, fn, a1, a2, a3, a4); |
| 348 } | 360 } |
| 349 | 361 |
| 350 private: | 362 private: |
| 363 FRIEND_TEST_ALL_PREFIXES(WeakHandleTest, |
| 364 TypeConversionConstructor); |
| 365 FRIEND_TEST_ALL_PREFIXES(WeakHandleTest, |
| 366 TypeConversionConstructorAssignment); |
| 367 |
| 351 scoped_refptr<internal::WeakHandleCore<T> > core_; | 368 scoped_refptr<internal::WeakHandleCore<T> > core_; |
| 352 }; | 369 }; |
| 353 | 370 |
| 354 // Makes a WeakHandle from a WeakPtr. | 371 // Makes a WeakHandle from a WeakPtr. |
| 355 template <typename T> | 372 template <typename T> |
| 356 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { | 373 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { |
| 357 return WeakHandle<T>(ptr); | 374 return WeakHandle<T>(ptr); |
| 358 } | 375 } |
| 359 | 376 |
| 360 } // namespace browser_sync | 377 } // namespace browser_sync |
| 361 | 378 |
| 362 #endif // CHROME_BROWSER_SYNC_UTIL_WEAK_HANDLE_H_ | 379 #endif // CHROME_BROWSER_SYNC_UTIL_WEAK_HANDLE_H_ |
| OLD | NEW |