OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // May be called on any thread. | 107 // May be called on any thread. |
108 bool IsOnOwnerThread() const; | 108 bool IsOnOwnerThread() const; |
109 | 109 |
110 protected: | 110 protected: |
111 // May be destroyed on any thread. | 111 // May be destroyed on any thread. |
112 ~WeakHandleCoreBase(); | 112 ~WeakHandleCoreBase(); |
113 | 113 |
114 // May be called on any thread. | 114 // May be called on any thread. |
115 void PostToOwnerThread(const tracked_objects::Location& from_here, | 115 void PostToOwnerThread(const tracked_objects::Location& from_here, |
116 const base::Closure& fn) const; | 116 const base::Closure& fn) const; |
| 117 void PostWithReplyToOwnerThread(const tracked_objects::Location& from_here, |
| 118 const base::Closure& fn, |
| 119 const base::Closure& reply) const; |
117 | 120 |
118 private: | 121 private: |
119 // May be used on any thread. | 122 // May be used on any thread. |
120 const scoped_refptr<base::MessageLoopProxy> owner_loop_proxy_; | 123 const scoped_refptr<base::MessageLoopProxy> owner_loop_proxy_; |
121 | 124 |
122 DISALLOW_COPY_AND_ASSIGN(WeakHandleCoreBase); | 125 DISALLOW_COPY_AND_ASSIGN(WeakHandleCoreBase); |
123 }; | 126 }; |
124 | 127 |
125 // WeakHandleCore<T> contains all the logic for WeakHandle<T>. | 128 // WeakHandleCore<T> contains all the logic for WeakHandle<T>. |
126 template <typename T> | 129 template <typename T> |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 typename ParamTraits<A1>::ForwardType a1, | 191 typename ParamTraits<A1>::ForwardType a1, |
189 typename ParamTraits<A2>::ForwardType a2, | 192 typename ParamTraits<A2>::ForwardType a2, |
190 typename ParamTraits<A3>::ForwardType a3, | 193 typename ParamTraits<A3>::ForwardType a3, |
191 typename ParamTraits<A4>::ForwardType a4) const { | 194 typename ParamTraits<A4>::ForwardType a4) const { |
192 PostToOwnerThread( | 195 PostToOwnerThread( |
193 from_here, | 196 from_here, |
194 Bind(&WeakHandleCore::template DoCall4<U, A1, A2, A3, A4>, | 197 Bind(&WeakHandleCore::template DoCall4<U, A1, A2, A3, A4>, |
195 this, fn, a1, a2, a3, a4)); | 198 this, fn, a1, a2, a3, a4)); |
196 } | 199 } |
197 | 200 |
| 201 // Variants that allow for a reply callback. |
| 202 template <typename U> |
| 203 void CallWithReply(const tracked_objects::Location& from_here, |
| 204 void (U::*fn)(void), |
| 205 const base::Closure& reply) const { |
| 206 PostWithReplyToOwnerThread( |
| 207 from_here, |
| 208 Bind(&WeakHandleCore::template DoCall0<U>, this, fn), |
| 209 reply); |
| 210 } |
| 211 |
| 212 template <typename U, typename A1> |
| 213 void CallWithReply(const tracked_objects::Location& from_here, |
| 214 void (U::*fn)(A1), |
| 215 typename ParamTraits<A1>::ForwardType a1, |
| 216 const base::Closure& reply) const { |
| 217 PostWithReplyToOwnerThread( |
| 218 from_here, |
| 219 Bind(&WeakHandleCore::template DoCall1<U, A1>, |
| 220 this, fn, a1), |
| 221 reply); |
| 222 } |
| 223 |
| 224 template <typename U, typename A1, typename A2> |
| 225 void CallWithReply(const tracked_objects::Location& from_here, |
| 226 void (U::*fn)(A1, A2), |
| 227 typename ParamTraits<A1>::ForwardType a1, |
| 228 typename ParamTraits<A2>::ForwardType a2, |
| 229 const base::Closure& reply) const { |
| 230 PostWithReplyToOwnerThread( |
| 231 from_here, |
| 232 Bind(&WeakHandleCore::template DoCall2<U, A1, A2>, |
| 233 this, fn, a1, a2), |
| 234 reply); |
| 235 } |
| 236 |
| 237 template <typename U, typename A1, typename A2, typename A3> |
| 238 void CallWithReply(const tracked_objects::Location& from_here, |
| 239 void (U::*fn)(A1, A2, A3), |
| 240 typename ParamTraits<A1>::ForwardType a1, |
| 241 typename ParamTraits<A2>::ForwardType a2, |
| 242 typename ParamTraits<A3>::ForwardType a3, |
| 243 const base::Closure& reply) const { |
| 244 PostWithReplyToOwnerThread( |
| 245 from_here, |
| 246 Bind(&WeakHandleCore::template DoCall3<U, A1, A2, A3>, |
| 247 this, fn, a1, a2, a3), |
| 248 reply); |
| 249 } |
| 250 |
| 251 template <typename U, typename A1, typename A2, typename A3, typename A4> |
| 252 void CallWithReply(const tracked_objects::Location& from_here, |
| 253 void (U::*fn)(A1, A2, A3, A4), |
| 254 typename ParamTraits<A1>::ForwardType a1, |
| 255 typename ParamTraits<A2>::ForwardType a2, |
| 256 typename ParamTraits<A3>::ForwardType a3, |
| 257 typename ParamTraits<A4>::ForwardType a4, |
| 258 const base::Closure& reply) const { |
| 259 PostWithReplyToOwnerThread( |
| 260 from_here, |
| 261 Bind(&WeakHandleCore::template DoCall4<U, A1, A2, A3, A4>, |
| 262 this, fn, a1, a2, a3, a4), |
| 263 reply); |
| 264 } |
| 265 |
198 private: | 266 private: |
199 friend class base::RefCountedThreadSafe<WeakHandleCore<T> >; | 267 friend class base::RefCountedThreadSafe<WeakHandleCore<T> >; |
200 | 268 |
201 // May be destroyed on any thread. | 269 // May be destroyed on any thread. |
202 ~WeakHandleCore() {} | 270 ~WeakHandleCore() {} |
203 | 271 |
204 // GCC 4.2.1 on OS X gets confused if all the DoCall functions are | 272 // GCC 4.2.1 on OS X gets confused if all the DoCall functions are |
205 // named the same, so we distinguish them. | 273 // named the same, so we distinguish them. |
206 | 274 |
207 template <typename U> | 275 template <typename U> |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 void Call(const tracked_objects::Location& from_here, | 419 void Call(const tracked_objects::Location& from_here, |
352 void (U::*fn)(A1, A2, A3, A4), | 420 void (U::*fn)(A1, A2, A3, A4), |
353 typename internal::ParamTraits<A1>::ForwardType a1, | 421 typename internal::ParamTraits<A1>::ForwardType a1, |
354 typename internal::ParamTraits<A2>::ForwardType a2, | 422 typename internal::ParamTraits<A2>::ForwardType a2, |
355 typename internal::ParamTraits<A3>::ForwardType a3, | 423 typename internal::ParamTraits<A3>::ForwardType a3, |
356 typename internal::ParamTraits<A4>::ForwardType a4) const { | 424 typename internal::ParamTraits<A4>::ForwardType a4) const { |
357 CHECK(IsInitialized()); | 425 CHECK(IsInitialized()); |
358 core_->Call(from_here, fn, a1, a2, a3, a4); | 426 core_->Call(from_here, fn, a1, a2, a3, a4); |
359 } | 427 } |
360 | 428 |
| 429 // Variants that allow for a reply callback. |
| 430 template <typename U> |
| 431 void CallWithReply(const tracked_objects::Location& from_here, |
| 432 void (U::*fn)(void), |
| 433 const base::Closure& reply) const { |
| 434 CHECK(IsInitialized()); |
| 435 core_->CallWithReply(from_here, fn, reply); |
| 436 } |
| 437 |
| 438 template <typename U, typename A1> |
| 439 void CallWithReply(const tracked_objects::Location& from_here, |
| 440 void (U::*fn)(A1), |
| 441 typename internal::ParamTraits<A1>::ForwardType a1, |
| 442 const base::Closure& reply) const { |
| 443 CHECK(IsInitialized()); |
| 444 core_->CallWithReply(from_here, fn, a1, reply); |
| 445 } |
| 446 |
| 447 template <typename U, typename A1, typename A2> |
| 448 void CallWithReply(const tracked_objects::Location& from_here, |
| 449 void (U::*fn)(A1, A2), |
| 450 typename internal::ParamTraits<A1>::ForwardType a1, |
| 451 typename internal::ParamTraits<A2>::ForwardType a2, |
| 452 const base::Closure& reply) const { |
| 453 CHECK(IsInitialized()); |
| 454 core_->CallWithReply(from_here, fn, a1, a2, reply); |
| 455 } |
| 456 |
| 457 template <typename U, typename A1, typename A2, typename A3> |
| 458 void CallWithReply(const tracked_objects::Location& from_here, |
| 459 void (U::*fn)(A1, A2, A3), |
| 460 typename internal::ParamTraits<A1>::ForwardType a1, |
| 461 typename internal::ParamTraits<A2>::ForwardType a2, |
| 462 typename internal::ParamTraits<A3>::ForwardType a3, |
| 463 const base::Closure& reply) const { |
| 464 CHECK(IsInitialized()); |
| 465 core_->CallWithReply(from_here, fn, a1, a2, a3, reply); |
| 466 } |
| 467 |
| 468 template <typename U, typename A1, typename A2, typename A3, typename A4> |
| 469 void CallWithReply(const tracked_objects::Location& from_here, |
| 470 void (U::*fn)(A1, A2, A3, A4), |
| 471 typename internal::ParamTraits<A1>::ForwardType a1, |
| 472 typename internal::ParamTraits<A2>::ForwardType a2, |
| 473 typename internal::ParamTraits<A3>::ForwardType a3, |
| 474 typename internal::ParamTraits<A4>::ForwardType a4, |
| 475 const base::Closure& reply) const { |
| 476 CHECK(IsInitialized()); |
| 477 core_->CallWithReply(from_here, fn, a1, a2, a3, a4, reply); |
| 478 } |
| 479 |
| 480 |
361 private: | 481 private: |
362 FRIEND_TEST_ALL_PREFIXES(WeakHandleTest, | 482 FRIEND_TEST_ALL_PREFIXES(WeakHandleTest, |
363 TypeConversionConstructor); | 483 TypeConversionConstructor); |
364 FRIEND_TEST_ALL_PREFIXES(WeakHandleTest, | 484 FRIEND_TEST_ALL_PREFIXES(WeakHandleTest, |
365 TypeConversionConstructorAssignment); | 485 TypeConversionConstructorAssignment); |
366 | 486 |
367 scoped_refptr<internal::WeakHandleCore<T> > core_; | 487 scoped_refptr<internal::WeakHandleCore<T> > core_; |
368 }; | 488 }; |
369 | 489 |
370 // Makes a WeakHandle from a WeakPtr. | 490 // Makes a WeakHandle from a WeakPtr. |
371 template <typename T> | 491 template <typename T> |
372 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { | 492 WeakHandle<T> MakeWeakHandle(const base::WeakPtr<T>& ptr) { |
373 return WeakHandle<T>(ptr); | 493 return WeakHandle<T>(ptr); |
374 } | 494 } |
375 | 495 |
376 } // namespace syncer | 496 } // namespace syncer |
377 | 497 |
378 #endif // SYNC_UTIL_WEAK_HANDLE_H_ | 498 #endif // SYNC_UTIL_WEAK_HANDLE_H_ |
OLD | NEW |