1 /* 2 * Copyright 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 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: Notifier()26 Notifier() {} 27 RegisterObserver(ObserverInterface * observer)28 virtual void RegisterObserver(ObserverInterface* observer) { 29 RTC_DCHECK(observer != nullptr); 30 observers_.push_back(observer); 31 } 32 UnregisterObserver(ObserverInterface * observer)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 FireOnChanged()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