The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_REF_BASE_H |
| 18 | #define ANDROID_REF_BASE_H |
| 19 | |
| 20 | #include <cutils/atomic.h> |
| 21 | #include <utils/TextOutput.h> |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | // --------------------------------------------------------------------------- |
| 28 | namespace android { |
| 29 | |
| 30 | template<typename T> class wp; |
| 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | |
Mathias Agopian | 951d3fe | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 34 | #define COMPARE_WEAK(_op_) \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | inline bool operator _op_ (const sp<T>& o) const { \ |
| 36 | return m_ptr _op_ o.m_ptr; \ |
| 37 | } \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | inline bool operator _op_ (const T* o) const { \ |
| 39 | return m_ptr _op_ o; \ |
| 40 | } \ |
| 41 | template<typename U> \ |
| 42 | inline bool operator _op_ (const sp<U>& o) const { \ |
| 43 | return m_ptr _op_ o.m_ptr; \ |
| 44 | } \ |
| 45 | template<typename U> \ |
Mathias Agopian | 951d3fe | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 46 | inline bool operator _op_ (const U* o) const { \ |
| 47 | return m_ptr _op_ o; \ |
| 48 | } |
| 49 | |
| 50 | #define COMPARE(_op_) \ |
| 51 | COMPARE_WEAK(_op_) \ |
| 52 | inline bool operator _op_ (const wp<T>& o) const { \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | return m_ptr _op_ o.m_ptr; \ |
| 54 | } \ |
| 55 | template<typename U> \ |
Mathias Agopian | 951d3fe | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 56 | inline bool operator _op_ (const wp<U>& o) const { \ |
| 57 | return m_ptr _op_ o.m_ptr; \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // --------------------------------------------------------------------------- |
| 61 | |
| 62 | class RefBase |
| 63 | { |
| 64 | public: |
| 65 | void incStrong(const void* id) const; |
| 66 | void decStrong(const void* id) const; |
| 67 | |
| 68 | void forceIncStrong(const void* id) const; |
| 69 | |
| 70 | //! DEBUGGING ONLY: Get current strong ref count. |
| 71 | int32_t getStrongCount() const; |
| 72 | |
| 73 | class weakref_type |
| 74 | { |
| 75 | public: |
| 76 | RefBase* refBase() const; |
| 77 | |
| 78 | void incWeak(const void* id); |
| 79 | void decWeak(const void* id); |
| 80 | |
| 81 | bool attemptIncStrong(const void* id); |
| 82 | |
| 83 | //! This is only safe if you have set OBJECT_LIFETIME_FOREVER. |
| 84 | bool attemptIncWeak(const void* id); |
| 85 | |
| 86 | //! DEBUGGING ONLY: Get current weak ref count. |
| 87 | int32_t getWeakCount() const; |
| 88 | |
| 89 | //! DEBUGGING ONLY: Print references held on object. |
| 90 | void printRefs() const; |
| 91 | |
| 92 | //! DEBUGGING ONLY: Enable tracking for this object. |
| 93 | // enable -- enable/disable tracking |
| 94 | // retain -- when tracking is enable, if true, then we save a stack trace |
| 95 | // for each reference and dereference; when retain == false, we |
| 96 | // match up references and dereferences and keep only the |
| 97 | // outstanding ones. |
| 98 | |
| 99 | void trackMe(bool enable, bool retain); |
| 100 | }; |
| 101 | |
| 102 | weakref_type* createWeak(const void* id) const; |
| 103 | |
| 104 | weakref_type* getWeakRefs() const; |
| 105 | |
| 106 | //! DEBUGGING ONLY: Print references held on object. |
| 107 | inline void printRefs() const { getWeakRefs()->printRefs(); } |
| 108 | |
| 109 | //! DEBUGGING ONLY: Enable tracking of object. |
| 110 | inline void trackMe(bool enable, bool retain) |
| 111 | { |
| 112 | getWeakRefs()->trackMe(enable, retain); |
| 113 | } |
| 114 | |
Mathias Agopian | afffa8f | 2011-06-12 18:05:53 -0700 | [diff] [blame] | 115 | // used to override the RefBase destruction. |
| 116 | class Destroyer { |
| 117 | friend class RefBase; |
Mathias Agopian | 913a5a3 | 2011-06-14 11:50:22 -0700 | [diff] [blame] | 118 | friend class weakref_type; |
Mathias Agopian | afffa8f | 2011-06-12 18:05:53 -0700 | [diff] [blame] | 119 | public: |
| 120 | virtual ~Destroyer(); |
| 121 | private: |
| 122 | virtual void destroy(RefBase const* base) = 0; |
| 123 | }; |
| 124 | |
| 125 | // Make sure to never acquire a strong reference from this function. The |
| 126 | // same restrictions than for destructors apply. |
| 127 | void setDestroyer(Destroyer* destroyer); |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | protected: |
| 130 | RefBase(); |
| 131 | virtual ~RefBase(); |
Mathias Agopian | 9e76366 | 2011-05-19 18:03:31 -0700 | [diff] [blame] | 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | //! Flags for extendObjectLifetime() |
| 134 | enum { |
| 135 | OBJECT_LIFETIME_WEAK = 0x0001, |
| 136 | OBJECT_LIFETIME_FOREVER = 0x0003 |
| 137 | }; |
| 138 | |
| 139 | void extendObjectLifetime(int32_t mode); |
| 140 | |
| 141 | //! Flags for onIncStrongAttempted() |
| 142 | enum { |
| 143 | FIRST_INC_STRONG = 0x0001 |
| 144 | }; |
| 145 | |
| 146 | virtual void onFirstRef(); |
| 147 | virtual void onLastStrongRef(const void* id); |
| 148 | virtual bool onIncStrongAttempted(uint32_t flags, const void* id); |
| 149 | virtual void onLastWeakRef(const void* id); |
| 150 | |
| 151 | private: |
| 152 | friend class weakref_type; |
| 153 | class weakref_impl; |
| 154 | |
| 155 | RefBase(const RefBase& o); |
| 156 | RefBase& operator=(const RefBase& o); |
| 157 | |
| 158 | weakref_impl* const mRefs; |
| 159 | }; |
| 160 | |
| 161 | // --------------------------------------------------------------------------- |
| 162 | |
| 163 | template <class T> |
| 164 | class LightRefBase |
| 165 | { |
| 166 | public: |
| 167 | inline LightRefBase() : mCount(0) { } |
| 168 | inline void incStrong(const void* id) const { |
| 169 | android_atomic_inc(&mCount); |
| 170 | } |
| 171 | inline void decStrong(const void* id) const { |
| 172 | if (android_atomic_dec(&mCount) == 1) { |
| 173 | delete static_cast<const T*>(this); |
| 174 | } |
| 175 | } |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 176 | //! DEBUGGING ONLY: Get current strong ref count. |
| 177 | inline int32_t getStrongCount() const { |
| 178 | return mCount; |
| 179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | |
| 181 | protected: |
| 182 | inline ~LightRefBase() { } |
| 183 | |
| 184 | private: |
| 185 | mutable volatile int32_t mCount; |
| 186 | }; |
| 187 | |
| 188 | // --------------------------------------------------------------------------- |
| 189 | |
| 190 | template <typename T> |
| 191 | class sp |
| 192 | { |
| 193 | public: |
| 194 | typedef typename RefBase::weakref_type weakref_type; |
| 195 | |
| 196 | inline sp() : m_ptr(0) { } |
| 197 | |
| 198 | sp(T* other); |
| 199 | sp(const sp<T>& other); |
| 200 | template<typename U> sp(U* other); |
| 201 | template<typename U> sp(const sp<U>& other); |
| 202 | |
| 203 | ~sp(); |
| 204 | |
| 205 | // Assignment |
| 206 | |
| 207 | sp& operator = (T* other); |
| 208 | sp& operator = (const sp<T>& other); |
| 209 | |
| 210 | template<typename U> sp& operator = (const sp<U>& other); |
| 211 | template<typename U> sp& operator = (U* other); |
| 212 | |
| 213 | //! Special optimization for use by ProcessState (and nobody else). |
| 214 | void force_set(T* other); |
| 215 | |
| 216 | // Reset |
| 217 | |
| 218 | void clear(); |
| 219 | |
| 220 | // Accessors |
| 221 | |
| 222 | inline T& operator* () const { return *m_ptr; } |
| 223 | inline T* operator-> () const { return m_ptr; } |
| 224 | inline T* get() const { return m_ptr; } |
| 225 | |
| 226 | // Operators |
| 227 | |
| 228 | COMPARE(==) |
| 229 | COMPARE(!=) |
| 230 | COMPARE(>) |
| 231 | COMPARE(<) |
| 232 | COMPARE(<=) |
| 233 | COMPARE(>=) |
| 234 | |
| 235 | private: |
| 236 | template<typename Y> friend class sp; |
| 237 | template<typename Y> friend class wp; |
| 238 | |
| 239 | // Optimization for wp::promote(). |
| 240 | sp(T* p, weakref_type* refs); |
| 241 | |
| 242 | T* m_ptr; |
| 243 | }; |
| 244 | |
| 245 | template <typename T> |
| 246 | TextOutput& operator<<(TextOutput& to, const sp<T>& val); |
| 247 | |
| 248 | // --------------------------------------------------------------------------- |
| 249 | |
| 250 | template <typename T> |
| 251 | class wp |
| 252 | { |
| 253 | public: |
| 254 | typedef typename RefBase::weakref_type weakref_type; |
| 255 | |
| 256 | inline wp() : m_ptr(0) { } |
| 257 | |
| 258 | wp(T* other); |
| 259 | wp(const wp<T>& other); |
| 260 | wp(const sp<T>& other); |
| 261 | template<typename U> wp(U* other); |
| 262 | template<typename U> wp(const sp<U>& other); |
| 263 | template<typename U> wp(const wp<U>& other); |
| 264 | |
| 265 | ~wp(); |
| 266 | |
| 267 | // Assignment |
| 268 | |
| 269 | wp& operator = (T* other); |
| 270 | wp& operator = (const wp<T>& other); |
| 271 | wp& operator = (const sp<T>& other); |
| 272 | |
| 273 | template<typename U> wp& operator = (U* other); |
| 274 | template<typename U> wp& operator = (const wp<U>& other); |
| 275 | template<typename U> wp& operator = (const sp<U>& other); |
| 276 | |
| 277 | void set_object_and_refs(T* other, weakref_type* refs); |
| 278 | |
| 279 | // promotion to sp |
| 280 | |
| 281 | sp<T> promote() const; |
| 282 | |
| 283 | // Reset |
| 284 | |
| 285 | void clear(); |
| 286 | |
| 287 | // Accessors |
| 288 | |
| 289 | inline weakref_type* get_refs() const { return m_refs; } |
| 290 | |
| 291 | inline T* unsafe_get() const { return m_ptr; } |
| 292 | |
| 293 | // Operators |
Mathias Agopian | 951d3fe | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 294 | |
| 295 | COMPARE_WEAK(==) |
| 296 | COMPARE_WEAK(!=) |
| 297 | COMPARE_WEAK(>) |
| 298 | COMPARE_WEAK(<) |
| 299 | COMPARE_WEAK(<=) |
| 300 | COMPARE_WEAK(>=) |
| 301 | |
| 302 | inline bool operator == (const wp<T>& o) const { |
| 303 | return (m_ptr == o.m_ptr) && (m_refs == o.m_refs); |
| 304 | } |
| 305 | template<typename U> |
| 306 | inline bool operator == (const wp<U>& o) const { |
| 307 | return m_ptr == o.m_ptr; |
| 308 | } |
| 309 | |
| 310 | inline bool operator > (const wp<T>& o) const { |
| 311 | return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr); |
| 312 | } |
| 313 | template<typename U> |
| 314 | inline bool operator > (const wp<U>& o) const { |
| 315 | return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr); |
| 316 | } |
| 317 | |
| 318 | inline bool operator < (const wp<T>& o) const { |
| 319 | return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); |
| 320 | } |
| 321 | template<typename U> |
| 322 | inline bool operator < (const wp<U>& o) const { |
| 323 | return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); |
| 324 | } |
| 325 | inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; } |
| 326 | template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); } |
| 327 | inline bool operator <= (const wp<T>& o) const { return !operator > (o); } |
| 328 | template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); } |
| 329 | inline bool operator >= (const wp<T>& o) const { return !operator < (o); } |
| 330 | template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | |
| 332 | private: |
| 333 | template<typename Y> friend class sp; |
| 334 | template<typename Y> friend class wp; |
| 335 | |
| 336 | T* m_ptr; |
| 337 | weakref_type* m_refs; |
| 338 | }; |
| 339 | |
| 340 | template <typename T> |
| 341 | TextOutput& operator<<(TextOutput& to, const wp<T>& val); |
| 342 | |
| 343 | #undef COMPARE |
Mathias Agopian | 951d3fe | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 344 | #undef COMPARE_WEAK |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | |
| 346 | // --------------------------------------------------------------------------- |
| 347 | // No user serviceable parts below here. |
| 348 | |
| 349 | template<typename T> |
| 350 | sp<T>::sp(T* other) |
| 351 | : m_ptr(other) |
| 352 | { |
| 353 | if (other) other->incStrong(this); |
| 354 | } |
| 355 | |
| 356 | template<typename T> |
| 357 | sp<T>::sp(const sp<T>& other) |
| 358 | : m_ptr(other.m_ptr) |
| 359 | { |
| 360 | if (m_ptr) m_ptr->incStrong(this); |
| 361 | } |
| 362 | |
| 363 | template<typename T> template<typename U> |
| 364 | sp<T>::sp(U* other) : m_ptr(other) |
| 365 | { |
| 366 | if (other) other->incStrong(this); |
| 367 | } |
| 368 | |
| 369 | template<typename T> template<typename U> |
| 370 | sp<T>::sp(const sp<U>& other) |
| 371 | : m_ptr(other.m_ptr) |
| 372 | { |
| 373 | if (m_ptr) m_ptr->incStrong(this); |
| 374 | } |
| 375 | |
| 376 | template<typename T> |
| 377 | sp<T>::~sp() |
| 378 | { |
| 379 | if (m_ptr) m_ptr->decStrong(this); |
| 380 | } |
| 381 | |
| 382 | template<typename T> |
| 383 | sp<T>& sp<T>::operator = (const sp<T>& other) { |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 384 | T* otherPtr(other.m_ptr); |
| 385 | if (otherPtr) otherPtr->incStrong(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | if (m_ptr) m_ptr->decStrong(this); |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 387 | m_ptr = otherPtr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | return *this; |
| 389 | } |
| 390 | |
| 391 | template<typename T> |
| 392 | sp<T>& sp<T>::operator = (T* other) |
| 393 | { |
| 394 | if (other) other->incStrong(this); |
| 395 | if (m_ptr) m_ptr->decStrong(this); |
| 396 | m_ptr = other; |
| 397 | return *this; |
| 398 | } |
| 399 | |
| 400 | template<typename T> template<typename U> |
| 401 | sp<T>& sp<T>::operator = (const sp<U>& other) |
| 402 | { |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 403 | U* otherPtr(other.m_ptr); |
| 404 | if (otherPtr) otherPtr->incStrong(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | if (m_ptr) m_ptr->decStrong(this); |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 406 | m_ptr = otherPtr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | return *this; |
| 408 | } |
| 409 | |
| 410 | template<typename T> template<typename U> |
| 411 | sp<T>& sp<T>::operator = (U* other) |
| 412 | { |
| 413 | if (other) other->incStrong(this); |
| 414 | if (m_ptr) m_ptr->decStrong(this); |
| 415 | m_ptr = other; |
| 416 | return *this; |
| 417 | } |
| 418 | |
| 419 | template<typename T> |
| 420 | void sp<T>::force_set(T* other) |
| 421 | { |
| 422 | other->forceIncStrong(this); |
| 423 | m_ptr = other; |
| 424 | } |
| 425 | |
| 426 | template<typename T> |
| 427 | void sp<T>::clear() |
| 428 | { |
| 429 | if (m_ptr) { |
| 430 | m_ptr->decStrong(this); |
| 431 | m_ptr = 0; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | template<typename T> |
| 436 | sp<T>::sp(T* p, weakref_type* refs) |
| 437 | : m_ptr((p && refs->attemptIncStrong(this)) ? p : 0) |
| 438 | { |
| 439 | } |
| 440 | |
| 441 | template <typename T> |
| 442 | inline TextOutput& operator<<(TextOutput& to, const sp<T>& val) |
| 443 | { |
| 444 | to << "sp<>(" << val.get() << ")"; |
| 445 | return to; |
| 446 | } |
| 447 | |
| 448 | // --------------------------------------------------------------------------- |
| 449 | |
| 450 | template<typename T> |
| 451 | wp<T>::wp(T* other) |
| 452 | : m_ptr(other) |
| 453 | { |
| 454 | if (other) m_refs = other->createWeak(this); |
| 455 | } |
| 456 | |
| 457 | template<typename T> |
| 458 | wp<T>::wp(const wp<T>& other) |
| 459 | : m_ptr(other.m_ptr), m_refs(other.m_refs) |
| 460 | { |
| 461 | if (m_ptr) m_refs->incWeak(this); |
| 462 | } |
| 463 | |
| 464 | template<typename T> |
| 465 | wp<T>::wp(const sp<T>& other) |
| 466 | : m_ptr(other.m_ptr) |
| 467 | { |
| 468 | if (m_ptr) { |
| 469 | m_refs = m_ptr->createWeak(this); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | template<typename T> template<typename U> |
| 474 | wp<T>::wp(U* other) |
| 475 | : m_ptr(other) |
| 476 | { |
| 477 | if (other) m_refs = other->createWeak(this); |
| 478 | } |
| 479 | |
| 480 | template<typename T> template<typename U> |
| 481 | wp<T>::wp(const wp<U>& other) |
| 482 | : m_ptr(other.m_ptr) |
| 483 | { |
| 484 | if (m_ptr) { |
| 485 | m_refs = other.m_refs; |
| 486 | m_refs->incWeak(this); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | template<typename T> template<typename U> |
| 491 | wp<T>::wp(const sp<U>& other) |
| 492 | : m_ptr(other.m_ptr) |
| 493 | { |
| 494 | if (m_ptr) { |
| 495 | m_refs = m_ptr->createWeak(this); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | template<typename T> |
| 500 | wp<T>::~wp() |
| 501 | { |
| 502 | if (m_ptr) m_refs->decWeak(this); |
| 503 | } |
| 504 | |
| 505 | template<typename T> |
| 506 | wp<T>& wp<T>::operator = (T* other) |
| 507 | { |
| 508 | weakref_type* newRefs = |
| 509 | other ? other->createWeak(this) : 0; |
| 510 | if (m_ptr) m_refs->decWeak(this); |
| 511 | m_ptr = other; |
| 512 | m_refs = newRefs; |
| 513 | return *this; |
| 514 | } |
| 515 | |
| 516 | template<typename T> |
| 517 | wp<T>& wp<T>::operator = (const wp<T>& other) |
| 518 | { |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 519 | weakref_type* otherRefs(other.m_refs); |
| 520 | T* otherPtr(other.m_ptr); |
| 521 | if (otherPtr) otherRefs->incWeak(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 523 | m_ptr = otherPtr; |
| 524 | m_refs = otherRefs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | return *this; |
| 526 | } |
| 527 | |
| 528 | template<typename T> |
| 529 | wp<T>& wp<T>::operator = (const sp<T>& other) |
| 530 | { |
| 531 | weakref_type* newRefs = |
| 532 | other != NULL ? other->createWeak(this) : 0; |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 533 | T* otherPtr(other.m_ptr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 535 | m_ptr = otherPtr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | m_refs = newRefs; |
| 537 | return *this; |
| 538 | } |
| 539 | |
| 540 | template<typename T> template<typename U> |
| 541 | wp<T>& wp<T>::operator = (U* other) |
| 542 | { |
| 543 | weakref_type* newRefs = |
| 544 | other ? other->createWeak(this) : 0; |
| 545 | if (m_ptr) m_refs->decWeak(this); |
| 546 | m_ptr = other; |
| 547 | m_refs = newRefs; |
| 548 | return *this; |
| 549 | } |
| 550 | |
| 551 | template<typename T> template<typename U> |
| 552 | wp<T>& wp<T>::operator = (const wp<U>& other) |
| 553 | { |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 554 | weakref_type* otherRefs(other.m_refs); |
| 555 | U* otherPtr(other.m_ptr); |
| 556 | if (otherPtr) otherRefs->incWeak(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 558 | m_ptr = otherPtr; |
| 559 | m_refs = otherRefs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | return *this; |
| 561 | } |
| 562 | |
| 563 | template<typename T> template<typename U> |
| 564 | wp<T>& wp<T>::operator = (const sp<U>& other) |
| 565 | { |
| 566 | weakref_type* newRefs = |
| 567 | other != NULL ? other->createWeak(this) : 0; |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 568 | U* otherPtr(other.m_ptr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 51a6aef | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 570 | m_ptr = otherPtr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | m_refs = newRefs; |
| 572 | return *this; |
| 573 | } |
| 574 | |
| 575 | template<typename T> |
| 576 | void wp<T>::set_object_and_refs(T* other, weakref_type* refs) |
| 577 | { |
| 578 | if (other) refs->incWeak(this); |
| 579 | if (m_ptr) m_refs->decWeak(this); |
| 580 | m_ptr = other; |
| 581 | m_refs = refs; |
| 582 | } |
| 583 | |
| 584 | template<typename T> |
| 585 | sp<T> wp<T>::promote() const |
| 586 | { |
| 587 | return sp<T>(m_ptr, m_refs); |
| 588 | } |
| 589 | |
| 590 | template<typename T> |
| 591 | void wp<T>::clear() |
| 592 | { |
| 593 | if (m_ptr) { |
| 594 | m_refs->decWeak(this); |
| 595 | m_ptr = 0; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | template <typename T> |
| 600 | inline TextOutput& operator<<(TextOutput& to, const wp<T>& val) |
| 601 | { |
| 602 | to << "wp<>(" << val.unsafe_get() << ")"; |
| 603 | return to; |
| 604 | } |
| 605 | |
| 606 | }; // namespace android |
| 607 | |
| 608 | // --------------------------------------------------------------------------- |
| 609 | |
| 610 | #endif // ANDROID_REF_BASE_H |