Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mozilla/WeakPtr.h" |
michael@0 | 6 | |
michael@0 | 7 | using mozilla::SupportsWeakPtr; |
michael@0 | 8 | using mozilla::WeakPtr; |
michael@0 | 9 | |
michael@0 | 10 | // To have a class C support weak pointers, inherit from SupportsWeakPtr<C>. |
michael@0 | 11 | class C : public SupportsWeakPtr<C> |
michael@0 | 12 | { |
michael@0 | 13 | public: |
michael@0 | 14 | MOZ_DECLARE_REFCOUNTED_TYPENAME(C) |
michael@0 | 15 | int num; |
michael@0 | 16 | void act() {} |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | static void |
michael@0 | 20 | Example() |
michael@0 | 21 | { |
michael@0 | 22 | |
michael@0 | 23 | C* ptr = new C(); |
michael@0 | 24 | |
michael@0 | 25 | // Get weak pointers to ptr. The first time asWeakPtr is called |
michael@0 | 26 | // a reference counted WeakReference object is created that |
michael@0 | 27 | // can live beyond the lifetime of 'ptr'. The WeakReference |
michael@0 | 28 | // object will be notified of 'ptr's destruction. |
michael@0 | 29 | WeakPtr<C> weak = ptr->asWeakPtr(); |
michael@0 | 30 | WeakPtr<C> other = ptr->asWeakPtr(); |
michael@0 | 31 | |
michael@0 | 32 | // Test a weak pointer for validity before using it. |
michael@0 | 33 | if (weak) { |
michael@0 | 34 | weak->num = 17; |
michael@0 | 35 | weak->act(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | // Destroying the underlying object clears weak pointers to it. |
michael@0 | 39 | delete ptr; |
michael@0 | 40 | |
michael@0 | 41 | MOZ_RELEASE_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it."); |
michael@0 | 42 | MOZ_RELEASE_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it."); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | struct A : public SupportsWeakPtr<A> |
michael@0 | 46 | { |
michael@0 | 47 | MOZ_DECLARE_REFCOUNTED_TYPENAME(A) |
michael@0 | 48 | int data; |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | |
michael@0 | 52 | int |
michael@0 | 53 | main() |
michael@0 | 54 | { |
michael@0 | 55 | |
michael@0 | 56 | A* a = new A; |
michael@0 | 57 | |
michael@0 | 58 | // a2 is unused to test the case when we haven't initialized |
michael@0 | 59 | // the internal WeakReference pointer. |
michael@0 | 60 | A* a2 = new A; |
michael@0 | 61 | |
michael@0 | 62 | a->data = 5; |
michael@0 | 63 | WeakPtr<A> ptr = a->asWeakPtr(); |
michael@0 | 64 | { |
michael@0 | 65 | WeakPtr<A> ptr2 = a->asWeakPtr(); |
michael@0 | 66 | MOZ_RELEASE_ASSERT(ptr->data == 5); |
michael@0 | 67 | WeakPtr<A> ptr3 = a->asWeakPtr(); |
michael@0 | 68 | MOZ_RELEASE_ASSERT(ptr->data == 5); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | delete a; |
michael@0 | 72 | MOZ_RELEASE_ASSERT(!ptr); |
michael@0 | 73 | |
michael@0 | 74 | delete a2; |
michael@0 | 75 | |
michael@0 | 76 | Example(); |
michael@0 | 77 | |
michael@0 | 78 | return 0; |
michael@0 | 79 | } |