henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 09:53:12 | [diff] [blame] | 11 | #ifndef WEBRTC_API_NOTIFIER_H_ |
| 12 | #define WEBRTC_API_NOTIFIER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | |
Henrik Kjellander | 15583c1 | 2016-02-10 09:53:12 | [diff] [blame] | 16 | #include "webrtc/api/mediastreaminterface.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 | [diff] [blame] | 17 | #include "webrtc/base/common.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 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 | |
Henrik Kjellander | 15583c1 | 2016-02-10 09:53:12 | [diff] [blame] | 60 | #endif // WEBRTC_API_NOTIFIER_H_ |