/src/mozilla-central/xpcom/ds/nsObserverList.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef nsObserverList_h___ |
8 | | #define nsObserverList_h___ |
9 | | |
10 | | #include "nsISupports.h" |
11 | | #include "nsTArray.h" |
12 | | #include "nsCOMPtr.h" |
13 | | #include "nsCOMArray.h" |
14 | | #include "nsIObserver.h" |
15 | | #include "nsIWeakReference.h" |
16 | | #include "nsHashKeys.h" |
17 | | #include "nsSimpleEnumerator.h" |
18 | | #include "mozilla/Attributes.h" |
19 | | |
20 | | struct ObserverRef |
21 | | { |
22 | 0 | ObserverRef(const ObserverRef& aO) : isWeakRef(aO.isWeakRef), ref(aO.ref) {} |
23 | 172 | explicit ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) {} |
24 | 166 | explicit ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) {} |
25 | | |
26 | | bool isWeakRef; |
27 | | nsCOMPtr<nsISupports> ref; |
28 | | |
29 | | nsIObserver* asObserver() |
30 | 0 | { |
31 | 0 | NS_ASSERTION(!isWeakRef, "Isn't a strong ref."); |
32 | 0 | return static_cast<nsIObserver*>((nsISupports*)ref); |
33 | 0 | } |
34 | | |
35 | | nsIWeakReference* asWeak() |
36 | 0 | { |
37 | 0 | NS_ASSERTION(isWeakRef, "Isn't a weak ref."); |
38 | 0 | return static_cast<nsIWeakReference*>((nsISupports*)ref); |
39 | 0 | } |
40 | | |
41 | 0 | bool operator==(nsISupports* aRhs) const { return ref == aRhs; } |
42 | | }; |
43 | | |
44 | | class nsObserverList : public nsCharPtrHashKey |
45 | | { |
46 | | friend class nsObserverService; |
47 | | |
48 | | public: |
49 | | explicit nsObserverList(const char* aKey) : nsCharPtrHashKey(aKey) |
50 | 159 | { |
51 | 159 | MOZ_COUNT_CTOR(nsObserverList); |
52 | 159 | } |
53 | | |
54 | | nsObserverList(nsObserverList&& aOther) |
55 | | : nsCharPtrHashKey(std::move(aOther)) |
56 | | , mObservers(std::move(aOther.mObservers)) |
57 | 0 | { |
58 | 0 | } |
59 | | |
60 | | ~nsObserverList() |
61 | 0 | { |
62 | 0 | MOZ_COUNT_DTOR(nsObserverList); |
63 | 0 | } |
64 | | |
65 | | MOZ_MUST_USE nsresult AddObserver(nsIObserver* aObserver, bool aOwnsWeak); |
66 | | MOZ_MUST_USE nsresult RemoveObserver(nsIObserver* aObserver); |
67 | | |
68 | | void NotifyObservers(nsISupports* aSubject, |
69 | | const char* aTopic, |
70 | | const char16_t* aSomeData); |
71 | | void GetObserverList(nsISimpleEnumerator** aEnumerator); |
72 | | |
73 | | // Fill an array with the observers of this category. |
74 | | // The array is filled in last-added-first order. |
75 | | void FillObserverArray(nsCOMArray<nsIObserver>& aArray); |
76 | | |
77 | | // Like FillObserverArray(), but only for strongly held observers. |
78 | | void AppendStrongObservers(nsCOMArray<nsIObserver>& aArray); |
79 | | |
80 | | private: |
81 | | nsTArray<ObserverRef> mObservers; |
82 | | }; |
83 | | |
84 | | class nsObserverEnumerator final : public nsSimpleEnumerator |
85 | | { |
86 | | public: |
87 | | NS_DECL_NSISIMPLEENUMERATOR |
88 | | |
89 | | explicit nsObserverEnumerator(nsObserverList* aObserverList); |
90 | | |
91 | 0 | const nsID& DefaultInterface() override { return NS_GET_IID(nsIObserver); } |
92 | | |
93 | | private: |
94 | 0 | ~nsObserverEnumerator() override = default; |
95 | | |
96 | | int32_t mIndex; // Counts up from 0 |
97 | | nsCOMArray<nsIObserver> mObservers; |
98 | | }; |
99 | | |
100 | | #endif /* nsObserverList_h___ */ |