Bumping manifests a=b2g-bump
[gecko.git] / xpcom / ds / nsObserverList.h
blob70bcee1fbf7e827aaecc84a6e45430fabdd723ea
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsObserverList_h___
7 #define nsObserverList_h___
9 #include "nsISupports.h"
10 #include "nsTArray.h"
11 #include "nsCOMPtr.h"
12 #include "nsCOMArray.h"
13 #include "nsIObserver.h"
14 #include "nsIWeakReference.h"
15 #include "nsHashKeys.h"
16 #include "nsISimpleEnumerator.h"
17 #include "mozilla/Attributes.h"
19 namespace mozilla {
20 class ObserverServiceReporter;
21 } // namespace mozilla
23 struct ObserverRef
25 ObserverRef(const ObserverRef& aO) : isWeakRef(aO.isWeakRef), ref(aO.ref) {}
26 explicit ObserverRef(nsIObserver* aObserver) : isWeakRef(false), ref(aObserver) {}
27 explicit ObserverRef(nsIWeakReference* aWeak) : isWeakRef(true), ref(aWeak) {}
29 bool isWeakRef;
30 nsCOMPtr<nsISupports> ref;
32 nsIObserver* asObserver()
34 NS_ASSERTION(!isWeakRef, "Isn't a strong ref.");
35 return static_cast<nsIObserver*>((nsISupports*)ref);
38 nsIWeakReference* asWeak()
40 NS_ASSERTION(isWeakRef, "Isn't a weak ref.");
41 return static_cast<nsIWeakReference*>((nsISupports*)ref);
44 bool operator==(nsISupports* aRhs) const { return ref == aRhs; }
47 class nsObserverList : public nsCharPtrHashKey
49 friend class nsObserverService;
51 public:
52 explicit nsObserverList(const char* aKey) : nsCharPtrHashKey(aKey)
54 MOZ_COUNT_CTOR(nsObserverList);
57 ~nsObserverList()
59 MOZ_COUNT_DTOR(nsObserverList);
62 nsresult AddObserver(nsIObserver* aObserver, bool aOwnsWeak);
63 nsresult RemoveObserver(nsIObserver* aObserver);
65 void NotifyObservers(nsISupports* aSubject,
66 const char* aTopic,
67 const char16_t* aSomeData);
68 nsresult GetObserverList(nsISimpleEnumerator** aEnumerator);
70 // Fill an array with the observers of this category.
71 // The array is filled in last-added-first order.
72 void FillObserverArray(nsCOMArray<nsIObserver>& aArray);
74 // Like FillObserverArray(), but only for strongly held observers.
75 void AppendStrongObservers(nsCOMArray<nsIObserver>& aArray);
77 private:
78 nsTArray<ObserverRef> mObservers;
81 class nsObserverEnumerator MOZ_FINAL : public nsISimpleEnumerator
83 public:
84 NS_DECL_ISUPPORTS
85 NS_DECL_NSISIMPLEENUMERATOR
87 explicit nsObserverEnumerator(nsObserverList* aObserverList);
89 private:
90 ~nsObserverEnumerator() {}
92 int32_t mIndex; // Counts up from 0
93 nsCOMArray<nsIObserver> mObservers;
96 #endif /* nsObserverList_h___ */