Bumping manifests a=b2g-bump
[gecko.git] / xpcom / glue / Observer.h
blobe8a3174151a72768ebf09cef508b47ea35bed36b
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 mozilla_Observer_h
8 #define mozilla_Observer_h
10 #include "nsTArray.h"
12 namespace mozilla {
14 /**
15 * Observer<T> provides a way for a class to observe something.
16 * When an event has to be broadcasted to all Observer<T>, Notify() method
17 * is called.
18 * T represents the type of the object passed in argument to Notify().
20 * @see ObserverList.
22 template<class T>
23 class Observer
25 public:
26 virtual ~Observer() {}
27 virtual void Notify(const T& aParam) = 0;
30 /**
31 * ObserverList<T> tracks Observer<T> and can notify them when Broadcast() is
32 * called.
33 * T represents the type of the object passed in argument to Broadcast() and
34 * sent to Observer<T> objects through Notify().
36 * @see Observer.
38 template<class T>
39 class ObserverList
41 public:
42 /**
43 * Note: When calling AddObserver, it's up to the caller to make sure the
44 * object isn't going to be release as long as RemoveObserver hasn't been
45 * called.
47 * @see RemoveObserver()
49 void AddObserver(Observer<T>* aObserver)
51 mObservers.AppendElement(aObserver);
54 /**
55 * Remove the observer from the observer list.
56 * @return Whether the observer has been found in the list.
58 bool RemoveObserver(Observer<T>* aObserver)
60 return mObservers.RemoveElement(aObserver);
63 uint32_t Length()
65 return mObservers.Length();
68 void Broadcast(const T& aParam)
70 uint32_t size = mObservers.Length();
71 for (uint32_t i = 0; i < size; ++i) {
72 mObservers[i]->Notify(aParam);
76 protected:
77 nsTArray<Observer<T>*> mObservers;
80 } // namespace mozilla
82 #endif // mozilla_Observer_h