| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef API_NOTIFIER_H_ |
| 12 | #define API_NOTIFIER_H_ |
| 13 |
| 14 | #include <list> |
| 15 |
| 16 | #include "api/media_stream_interface.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 |
| 19 | namespace webrtc { |
| 20 |
| 21 | // Implements a template version of a notifier. |
| 22 | // TODO(deadbeef): This is an implementation detail; move out of api/. |
| 23 | template <class T> |
| 24 | class Notifier : public T { |
| 25 | public: |
| 26 | Notifier() {} |
| 27 |
| 28 | virtual void RegisterObserver(ObserverInterface* observer) { |
| 29 | RTC_DCHECK(observer != nullptr); |
| 30 | observers_.push_back(observer); |
| 31 | } |
| 32 |
| 33 | virtual void UnregisterObserver(ObserverInterface* observer) { |
| 34 | for (std::list<ObserverInterface*>::iterator it = observers_.begin(); |
| 35 | it != observers_.end(); it++) { |
| 36 | if (*it == observer) { |
| 37 | observers_.erase(it); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 |
| 43 | void FireOnChanged() { |
| 44 | // Copy the list of observers to avoid a crash if the observer object |
| 45 | // unregisters as a result of the OnChanged() call. If the same list is used |
| 46 | // UnregisterObserver will affect the list make the iterator invalid. |
| 47 | std::list<ObserverInterface*> observers = observers_; |
| 48 | for (std::list<ObserverInterface*>::iterator it = observers.begin(); |
| 49 | it != observers.end(); ++it) { |
| 50 | (*it)->OnChanged(); |
| 51 | } |
| 52 | } |
| 53 |
| 54 | protected: |
| 55 | std::list<ObserverInterface*> observers_; |
| 56 | }; |
| 57 |
| 58 | } // namespace webrtc |
| 59 |
| 60 | #endif // API_NOTIFIER_H_ |
| 61 |