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/. */
7 #ifndef nsObserverList_h___
8 #define nsObserverList_h___
10 #include "nsISupports.h"
11 #include "nsCOMArray.h"
12 #include "nsIObserver.h"
13 #include "nsHashKeys.h"
14 #include "nsMaybeWeakPtr.h"
15 #include "nsSimpleEnumerator.h"
16 #include "mozilla/Attributes.h"
18 class nsObserverList
: public nsCharPtrHashKey
{
19 friend class nsObserverService
;
22 explicit nsObserverList(const char* aKey
) : nsCharPtrHashKey(aKey
) {
23 MOZ_COUNT_CTOR(nsObserverList
);
26 nsObserverList(nsObserverList
&& aOther
)
27 : nsCharPtrHashKey(std::move(aOther
)),
28 mObservers(std::move(aOther
.mObservers
)) {
29 MOZ_COUNT_CTOR(nsObserverList
);
32 MOZ_COUNTED_DTOR(nsObserverList
)
34 [[nodiscard
]] nsresult
AddObserver(nsIObserver
* aObserver
, bool aOwnsWeak
);
35 [[nodiscard
]] nsresult
RemoveObserver(nsIObserver
* aObserver
);
37 void NotifyObservers(nsISupports
* aSubject
, const char* aTopic
,
38 const char16_t
* aSomeData
);
39 void GetObserverList(nsISimpleEnumerator
** aEnumerator
);
41 // Clone an array with the observers of this category.
42 // The array is filled in last-added-first order.
43 nsCOMArray
<nsIObserver
> ReverseCloneObserverArray();
45 // Like FillObserverArray(), but only for strongly held observers.
46 void AppendStrongObservers(nsCOMArray
<nsIObserver
>& aArray
);
49 nsMaybeWeakPtrArray
<nsIObserver
> mObservers
;
52 class nsObserverEnumerator final
: public nsSimpleEnumerator
{
54 NS_DECL_NSISIMPLEENUMERATOR
56 explicit nsObserverEnumerator(nsObserverList
* aObserverList
);
58 const nsID
& DefaultInterface() override
{ return NS_GET_IID(nsIObserver
); }
61 ~nsObserverEnumerator() override
= default;
63 int32_t mIndex
; // Counts up from 0
64 nsCOMArray
<nsIObserver
> mObservers
;
67 #endif /* nsObserverList_h___ */