LCOV - code coverage report
Current view: top level - xpcom/ds - Observer.h (source / functions) Hit Total Coverage
Test: output.info Lines: 7 16 43.8 %
Date: 2017-07-14 16:53:18 Functions: 17 72 23.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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 mozilla_Observer_h
       8             : #define mozilla_Observer_h
       9             : 
      10             : #include "nsTArray.h"
      11             : 
      12             : namespace mozilla {
      13             : 
      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().
      19             :  *
      20             :  * @see ObserverList.
      21             :  */
      22             : template<class T>
      23          13 : class Observer
      24             : {
      25             : public:
      26           0 :   virtual ~Observer() {}
      27             :   virtual void Notify(const T& aParam) = 0;
      28             : };
      29             : 
      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().
      35             :  *
      36             :  * @see Observer.
      37             :  */
      38             : template<class T>
      39           5 : class ObserverList
      40             : {
      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.
      46             :    *
      47             :    * @see RemoveObserver()
      48             :    */
      49           6 :   void AddObserver(Observer<T>* aObserver)
      50             :   {
      51           6 :     mObservers.AppendElement(aObserver);
      52           6 :   }
      53             : 
      54             :   /**
      55             :    * Remove the observer from the observer list.
      56             :    * @return Whether the observer has been found in the list.
      57             :    */
      58           0 :   bool RemoveObserver(Observer<T>* aObserver)
      59             :   {
      60           0 :     return mObservers.RemoveElement(aObserver);
      61             :   }
      62             : 
      63           6 :   uint32_t Length()
      64             :   {
      65           6 :     return mObservers.Length();
      66             :   }
      67             : 
      68           0 :   void Broadcast(const T& aParam)
      69             :   {
      70           0 :     nsTArray<Observer<T>*> observersCopy(mObservers);
      71           0 :     uint32_t size = observersCopy.Length();
      72           0 :     for (uint32_t i = 0; i < size; ++i) {
      73           0 :       observersCopy[i]->Notify(aParam);
      74             :     }
      75           0 :   }
      76             : 
      77             : protected:
      78             :   nsTArray<Observer<T>*> mObservers;
      79             : };
      80             : 
      81             : } // namespace mozilla
      82             : 
      83             : #endif // mozilla_Observer_h

Generated by: LCOV version 1.13