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