OLD | NEW |
(Empty) | |
| 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 WEBRTC_API_NOTIFIER_H_ |
| 12 #define WEBRTC_API_NOTIFIER_H_ |
| 13 |
| 14 #include <list> |
| 15 |
| 16 #include "webrtc/api/mediastreaminterface.h" |
| 17 #include "webrtc/base/common.h" |
| 18 |
| 19 namespace webrtc { |
| 20 |
| 21 // Implement a template version of a notifier. |
| 22 template <class T> |
| 23 class Notifier : public T { |
| 24 public: |
| 25 Notifier() { |
| 26 } |
| 27 |
| 28 virtual void RegisterObserver(ObserverInterface* observer) { |
| 29 ASSERT(observer != NULL); |
| 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 // WEBRTC_API_NOTIFIER_H_ |
OLD | NEW |