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 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsMaybeWeakPtr_h_ |
michael@0 | 7 | #define nsMaybeWeakPtr_h_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCOMPtr.h" |
michael@0 | 10 | #include "nsWeakReference.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "nsCycleCollectionNoteChild.h" |
michael@0 | 13 | |
michael@0 | 14 | // nsMaybeWeakPtr is a helper object to hold a strong-or-weak reference |
michael@0 | 15 | // to the template class. It's pretty minimal, but sufficient. |
michael@0 | 16 | |
michael@0 | 17 | class nsMaybeWeakPtr_base |
michael@0 | 18 | { |
michael@0 | 19 | protected: |
michael@0 | 20 | // Returns an addref'd pointer to the requested interface |
michael@0 | 21 | void* GetValueAs(const nsIID& iid) const; |
michael@0 | 22 | |
michael@0 | 23 | nsCOMPtr<nsISupports> mPtr; |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | template<class T> |
michael@0 | 27 | class nsMaybeWeakPtr : private nsMaybeWeakPtr_base |
michael@0 | 28 | { |
michael@0 | 29 | public: |
michael@0 | 30 | nsMaybeWeakPtr(nsISupports *ref) { mPtr = ref; } |
michael@0 | 31 | nsMaybeWeakPtr(const nsCOMPtr<nsIWeakReference> &ref) { mPtr = ref; } |
michael@0 | 32 | nsMaybeWeakPtr(const nsCOMPtr<T> &ref) { mPtr = ref; } |
michael@0 | 33 | |
michael@0 | 34 | bool operator==(const nsMaybeWeakPtr<T> &other) const { |
michael@0 | 35 | return mPtr == other.mPtr; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | operator const nsCOMPtr<T>() const { return GetValue(); } |
michael@0 | 39 | |
michael@0 | 40 | protected: |
michael@0 | 41 | const nsCOMPtr<T> GetValue() const { |
michael@0 | 42 | return nsCOMPtr<T>(dont_AddRef(static_cast<T*> |
michael@0 | 43 | (GetValueAs(NS_GET_TEMPLATE_IID(T))))); |
michael@0 | 44 | } |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | // nsMaybeWeakPtrArray is an array of MaybeWeakPtr objects, that knows how to |
michael@0 | 48 | // grab a weak reference to a given object if requested. It only allows a |
michael@0 | 49 | // given object to appear in the array once. |
michael@0 | 50 | |
michael@0 | 51 | typedef nsTArray< nsMaybeWeakPtr<nsISupports> > isupports_array_type; |
michael@0 | 52 | nsresult NS_AppendWeakElementBase(isupports_array_type *aArray, |
michael@0 | 53 | nsISupports *aElement, bool aWeak); |
michael@0 | 54 | nsresult NS_RemoveWeakElementBase(isupports_array_type *aArray, |
michael@0 | 55 | nsISupports *aElement); |
michael@0 | 56 | |
michael@0 | 57 | template<class T> |
michael@0 | 58 | class nsMaybeWeakPtrArray : public nsTArray< nsMaybeWeakPtr<T> > |
michael@0 | 59 | { |
michael@0 | 60 | public: |
michael@0 | 61 | nsresult AppendWeakElement(T *aElement, bool aOwnsWeak) |
michael@0 | 62 | { |
michael@0 | 63 | return NS_AppendWeakElementBase( |
michael@0 | 64 | reinterpret_cast<isupports_array_type*>(this), aElement, aOwnsWeak); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | nsresult RemoveWeakElement(T *aElement) |
michael@0 | 68 | { |
michael@0 | 69 | return NS_RemoveWeakElementBase( |
michael@0 | 70 | reinterpret_cast<isupports_array_type*>(this), aElement); |
michael@0 | 71 | } |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | template <typename T> |
michael@0 | 75 | inline void |
michael@0 | 76 | ImplCycleCollectionUnlink(nsMaybeWeakPtrArray<T>& aField) |
michael@0 | 77 | { |
michael@0 | 78 | aField.Clear(); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | template <typename E> |
michael@0 | 82 | inline void |
michael@0 | 83 | ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback, |
michael@0 | 84 | nsMaybeWeakPtrArray<E>& aField, |
michael@0 | 85 | const char* aName, |
michael@0 | 86 | uint32_t aFlags = 0) |
michael@0 | 87 | { |
michael@0 | 88 | aFlags |= CycleCollectionEdgeNameArrayFlag; |
michael@0 | 89 | size_t length = aField.Length(); |
michael@0 | 90 | for (size_t i = 0; i < length; ++i) { |
michael@0 | 91 | CycleCollectionNoteChild(aCallback, aField[i].get(), aName, aFlags); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Call a method on each element in the array, but only if the element is |
michael@0 | 96 | // non-null. |
michael@0 | 97 | |
michael@0 | 98 | #define ENUMERATE_WEAKARRAY(array, type, method) \ |
michael@0 | 99 | for (uint32_t array_idx = 0; array_idx < array.Length(); ++array_idx) { \ |
michael@0 | 100 | const nsCOMPtr<type> &e = array.ElementAt(array_idx); \ |
michael@0 | 101 | if (e) \ |
michael@0 | 102 | e->method; \ |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | #endif |